[Haskell-cafe] ANN: codec: easy bidirectional serialization and first-class record construction

Patrick Chilton chpatrick at gmail.com
Tue May 12 02:47:25 UTC 2015


Tired of writing complementary parseJSON/toJSON, peek/poke or Binary
get/put functions?

codec <https://github.com/chpatrick/codec> 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.

*JSON:*

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

genFields ''User

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

instance FromJSON User where
  parseJSON = parseVal userCodec

instance ToJSON User where
  toJSON = produceVal userCodec

*Bit fields:*

ipv4Codec :: BinaryCodec IPv4
ipv4Codec = toBytes $
  IPv4
    $>> f_version         >-< word8 4
    >>> f_ihl             >-< word8 4
    >>> f_dscp            >-< word8 6
    >>> f_ecn             >-< word8 2
    >>> f_totalLength     >-< word16be 16
    >>> f_identification  >-< word16be 16
    >>> f_flags           >-< word8 3
    >>> f_fragmentOffset  >-< word16be 13
    >>> f_timeToLive      >-< word8 8
    >>> f_protocol        >-< word8 8
    >>> f_headerChecksum  >-< word16be 16
    >>> f_sourceIP        >-< word32be 32
    >>> f_destIP          >-< word32be 32

instance Binary IPv4 where
  get = parse ipv4Codec
  put = produce ipv4Codec

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).

Please take a look at the docs and the examples. I'd love to get some
feedback before I put it on Hackage. :)

All the best,
Patrick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150511/91054a54/attachment.html>


More information about the Haskell-Cafe mailing list