[Haskell-cafe] [ANN]json-autotype: Tool generating JSON parsers and type declarations from example documents...

Mateusz Kowalczyk fuuzetsu at fuuzetsu.co.uk
Tue Nov 4 04:01:26 UTC 2014


On 11/04/2014 01:13 AM, Michal J Gajda wrote:
>  Hi Roman, fellow Haskellers,
> 
> On 04/11/2014 05:35, Roman Cheplyaka wrote:
> 
> Sounds interesting. Can you show (and include in the README) some
> examples of recognized json inputs and code generated for them?
> 
>  There are rather extensive examples provides as unit tests in the source
> code repository, which I do not distribute with the package, since they may
> be covered by copyright of whatever APIs they were produced with (like
> Twitter, YouTube, Jenkins etc.). Thanks for pointing out that there should
> be some examples provided along the package!
> 
> The most simple example:
> {
>     "colorsArray":[{
>             "colorName":"red",
>             "hexValue":"#f00"
>         },
>         {
>             "colorName":"green",
>             "hexValue":"#0f0"
>         },
>         {
>             "colorName":"blue",
>             "hexValue":"#00f"
>         }
>     ]
> }
> 
> It will produce the module with the following datatypes and TH calls for
> JSON parser derivations:
> data ColorsArray = ColorsArray {
>     colorsArrayHexValue    :: Text,
>     colorsArrayColorName :: Text
>   } deriving (Show,Eq)
> 
> data TopLevel = TopLevel {
>     topLevelColorsArray :: ColorsArray
>   } deriving (Show,Eq)
> 
> Note that attribute names match the names of JSON dictionary keys.
> 
> Another example with ambiguous types:
> {
>     "parameter":[{
>             "parameterName":"apiVersion",
>             "parameterValue":1
>         },
>         {
>             "parameterName":"failOnWarnings",
>             "parameterValue":false
>         },
>         {
>             "parameterName":"caller",
>             "parameterValue":"site API"
>         }]
> }
> 
> It will produce quite intuitive result (plus extra parentheses, and class
> derivations):
> 
> data Parameter = Parameter {
>     parameterParameterValue :: Either Bool (Either Int Text),
>     parameterParameterName :: Text
>   }
> 
> data TopLevel = TopLevel {
>     topLevelParameter :: Parameter
>   }
> 
> I will add these examples to the README.
> 
> For real world use cases you might look at the current unit test directory.
> All of .json files there generate the correct parsers:
> https://github.com/mgajda/json-autotype/tree/master/test
> --
>   Best regards
>     Michal
> 

I imagine unpacking N layers of ‘Either’ would get really boring really
fast. Did you consider rolling your own sum types once it gets past
certain depth? I think even doubly nested Either is already getting
inconvenient.


-- 
Mateusz K.


More information about the Haskell-Cafe mailing list