aeson 0.4: easier, faster, better

After months of work, and a number of great contributions from other developers, I just released version 0.4 of aeson, the de facto standard Haskell JSON library. This is a major release, with a number of improvements. Enjoy!

Ease of use

The new decode function complements the longstanding encode function, and makes the API simpler.

New examples make it easier to learn to use the package.

Generics support

aeson’s support for data-type generic programming makes it possible to use JSON encodings of most data types without writing any boilerplate instances.

Thanks to Bas Van Dijk, aeson now supports the two major schemes for doing datatype-generic programming:

  • the modern mechanism, built into GHC itself

  • the older mechanism, based on SYB (aka "scrap your boilerplate")

The modern GHC-based generic mechanism is fast and terse: in fact, its performance is generally comparable in performance to hand-written and TH-derived ToJSON and FromJSON instances. To see how to use GHC generics, refer to examples/Generic.hs.

The SYB-based generics support lives in Data.Aeson.Generic, and is provided mainly for users of GHC older than 7.2. SYB is far slower (by about 10x) than the more modern generic mechanism. To see how to use SYB generics, refer to examples/GenericSYB.hs.

Improved performance

  • We switched the intermediate representation of JSON objects from Data.Map to Data.HashMap, which has improved type conversion performance.

  • Instances of ToJSON and FromJSON for tuples are between 45% and 70% faster than in 0.3.

Evaluation control

This version of aeson makes explicit the decoupling between identifying an element of a JSON document and converting it to Haskell. See the Data.Aeson.Parser documentation for details.

The normal aeson decode function performs identification strictly, but defers conversion until needed. This can result in improved performance (e.g. if the results of some conversions are never needed), but at a cost in increased memory consumption.

The new decode' function performs identification and conversion immediately. This incurs an up-front cost in CPU cycles, but reduces reduce memory consumption.

Posted in haskell, open source
2 comments on “aeson 0.4: easier, faster, better
  1. Paul R says:

    Great, thank you for this very useful piece of code. BTW the package name is mispelled in the title of your article. (please feel free to delete this not-so-interesting comment after reading it, as it was mainly to let you know about the title …).

  2. Alexander says:

    I think you may add a more simple example. A function to search json for a key. Like this one:

    parseTextValueToString :: String -> String -> Maybe Text
    parseTextValueToString s key =
    let bs = BS.pack s
    bp = pack key
    in case parse json bs of
    (Done rest (Object r)) -> let o = (\v -> v .: bp)
    in (T.parseMaybe o r)
    _ -> Nothing

    I need to search for many keys, but it is tedious to write so many instances of FromJSON.
    It takes a couple of hours for me to write this function. I think such example could be helpfull for someone.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>