<div dir="ltr">Tired of writing complementary parseJSON/toJSON, peek/poke or Binary get/put functions?<br><br><a href="https://github.com/chpatrick/codec">codec</a> provides easy bidirectional serialization of plain Haskell records in any Applicative context. All you need to do is provide a de/serializer for every record field in any order you like, and you get a de/serializer for the whole structure. The type system ensures that you provide every record exactly once. It also includes a library for general record construction in an Applicative context, of which creating codecs is just one application.<br><br><b>JSON:</b><br><font face="monospace, monospace"><br>data User = User<br>  { username :: Text<br>  , userEmail :: Text<br>  , userLanguages :: [ Text ]<br>  , userReferrer :: Maybe User<br>  } deriving Show<br><br>genFields ''User<br><br>userCodec :: JSONCodec User<br>userCodec = obj "user object" $<br>  User<br>    $>> f_username      >-< "user"<br>    >>> f_userEmail     >-< "email"<br>    >>> f_userLanguages >-< "languages"<br>    >>> f_userReferrer  >-< opt "referrer"<br><br>instance FromJSON User where<br>  parseJSON = parseVal userCodec<br><br>instance ToJSON User where<br>  toJSON = produceVal userCodec</font><br><div><br></div><div><b>Bit fields:</b><br><br><font face="monospace, monospace">ipv4Codec :: BinaryCodec IPv4<br>ipv4Codec = toBytes $<br>  IPv4<br>    $>> f_version         >-< word8 4<br>    >>> f_ihl             >-< word8 4<br>    >>> f_dscp            >-< word8 6<br>    >>> f_ecn             >-< word8 2<br>    >>> f_totalLength     >-< word16be 16<br>    >>> f_identification  >-< word16be 16<br>    >>> f_flags           >-< word8 3<br>    >>> f_fragmentOffset  >-< word16be 13<br>    >>> f_timeToLive      >-< word8 8<br>    >>> f_protocol        >-< word8 8<br>    >>> f_headerChecksum  >-< word16be 16<br>    >>> f_sourceIP        >-< word32be 32<br>    >>> f_destIP          >-< word32be 32<br><br>instance Binary IPv4 where<br>  get = parse ipv4Codec<br>  put = produce ipv4Codec</font></div><div><font face="monospace, monospace"><br></font></div><div>The same types and combinators can be used to make Storable or Binary instances, or used with pretty much any de/serialization library. The implementation only uses a little bit of Template Haskell to generate the f_... structures for each field and the rest is very basic in terms of extensions (no DataKinds or type families needed).</div><div><br></div><div>Please take a look at the docs and the examples. I'd love to get some feedback before I put it on Hackage. :)</div><div><br></div><div>All the best,</div><div>Patrick</div></div>