Search code examples
jsonhaskellghctemplate-haskellaeson

Specification of default ToJson format used by Aeson


Does anybody know where I can find documentation on how ADTs are translated to Json by Aeson's ToJSON? I'm using Haskell for a backend application, and I'm trying to write the JSON decoder for another functional language on the front end, so I'd like to use the same JSON format so that I can send messages between them.


Solution

  • I don't think it's very well documented, but I've had a lot of success in similar situations by using a QuickCheck Arbitrary instance to generate large numbers of source types, encoding them to JSON, and then using them as a gold-standard tests for your front-end.

    You can also garner some amount of intuition for how Aeson's Template Haskell encoding works by peeking around at the configurable Options type in Data.Aeson.TH. In particular take a look at SumEncoding which will let me explain, for instance, that Either is encoded using ObjectWithSingleField, i.e. {"Left": 3} for Left 3.