JSONPopular data formats
JSON is a very popular format that is used pretty much everywhere. It looks like this:
{
"name": "John Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100" },
"phoneNumbers": [
{ "type": "home",
"number": "212 555-1234" },
{ "type": "mobile",
"number": "123 456-7890" } ]
}
It supports objects (dictionaries), arrays, strings, numbers, booleans, and null
. JSON doesn't distinguish between integers and floating-point numbers, and doesn't provide any way to handle binary data.
Recommendations
Everyone uses aeson. All other libraries are either outdated or experimental.
You might have to use one of the alternative encoders if you really need performance and Aeson isn't enough for you, or you might have to use one of the alternative decoders if you really need constant-space decoding, but those are rare cases.
Aeson doesn't provide pretty-printed JSON out of the box, so use aeson-pretty for that.
TODO
Benchmark various encoders/decoders. Give examples for everything.
Not included
-
Alternative instance generators: json-autotype (automatically generates types/instances from JSON files), generic-aeson (generates more idiomatic instances than aeson's generic derivation),
-
Schemas: json-schema (unrelated to json-schema.org), hjsonschema (an implementation of the json-schema.org standard)
-
Bidirectional parsing (when the same declaration is used to convert to/from JSON): codec (there are other libraries, such as roundtrip-aeson, JsonGrammar, and aeson-applicative, but they all don't work with recent GHC)
-
Lens integration: lens-aeson and microlens-aeson (don't use aeson-lens, it's deprecated)
-
Alternative parsers: aeson-parsec-picky (an alternative parser that reports error positions; described further in this category), json-stream (an incremental fast parser that can skip what it doesn't need to parse)
-
Utils: aeson-casing (makes it easier to implement different casing variants like
snake_case
orPascalCase
for field names), aeson-extra (just a grab bag of utils), json-extra (another grab bag of utils), aeson-prefix (makes all keys hierarchical), aeson-filthy (working with booleans encoded as “on”/“yes”/etc, case-insensitive keys, and double-encoded JSON) -
Various stuff: aeson-pretty (pretty-prints JSON), aeson-qq (lets you embed JSON values in Haskell source more conveniently than you might've done with
object
and.=
), aeson-diff (diffs two JSON documents), aeson-better-errors (gives better conversion errors) -
Instances, etc: pipes-aeson, binary-orphans (instances for
Binary
)
<notes are empty>
-
As fast as Aeson (benchmarks).
-
The same declaration is used to convert to/from JSON – there's no need to write separate
ToJSON
andFromJSON
instances and worry about them accidentally going out of sync.
<notes are empty>
<notes are empty>
<notes are empty>
<notes are empty>
<notes are empty>
<notes are empty>