codec-0.1.1: First-class record construction and bidirectional serialization

Safe HaskellNone
LanguageHaskell2010

Data.Codec

Synopsis

Documentation

The main purpose of this package is to make the creation of Codecs as easy and painless as possible. If we have a data type such as:

data User = User
 { username :: Text
 , userEmail :: Text
 , userLanguages :: [ Text ]
 , userReferrer :: Maybe Text
 } deriving Show

we can use the genFields function to generate Fields for each record field:

genFields ''User

This will create Fields named f_username, f_userEmail, etc. These fields can be associated with an appropriate Codec with the >-< operator to specify the representation of the data structure. These associations can then be combined with the >>> operator in the order of serialization/deserialization. These associations can then be finalized into a Codec by providng the constructor to use. For example, using the JSON entry Codec that assigns a value to a JSON key, we could write a codec for User as:

 userCodec :: JSONCodec User
 userCodec = obj "user object' $
   User
     $>> f_username      >-< "user"
     >>> f_userEmail     >-< "email"
     >>> f_userLanguages >-< "languages"
     >>> f_userReferrer  >-< opt "referrer"

The type system ensures that every field is provided exactly once.