[Haskell-cafe] [ANN]json-autotype: Tool generating JSON parsers and type declarations from example documents...
Michal J Gajda
mgajda at mimuw.edu.pl
Tue Nov 4 01:13:01 UTC 2014
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
On 03/11/14 18:59, Michal J Gajda wrote:
Dear Fellow Haskellers,
I've just published a beta version of a tool facilitating generation of
Haskell type declarations from example JSON documents. It is supposed to
simplify implementing large JSON-based interfaces, without necessity of
writing any boilerplate code.
USAGE:
======
After installing with `cabal install json-autotype`, you might generate
stub code for the parser:
$ json-autotype input.json -o MyFormat.hs
Then you might test the parser by running it on an input file:
$ runghc MyFormat.hs input.json
If everything is correct, then feel free to inspect the data structure
generated automatically for you!
The goal of this program is to make it easy for users of big JSON APIs to
generate entries from
example data.
Occasionally you might find a valid JSON for which `json-autotype` doesn't
generate a correct parser.
You may either edit the resulting file _and_ send it to the author as a
test case for future release.
HOW IT WORKS:
==============
The program uses union type unification to trim output declarations. The
types of same attribute tag and similar attribute set, are automatically
unified using recognition by attribute set matching. (This option can be
optionally turned off, or a set of unified types may be given explicitly.)
Either alternatives is used to assure that all JSON inputs seen in example
input file are handled correctly.
Details on official releases are on Hackage [1]
1. https://hackage.haskell.org/package/json-autotype
Patches and suggestions are most welcome.
_______________________________________________
Haskell-Cafe mailing
listHaskell-Cafe at haskell.orghttp://www.haskell.org/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141104/b70b3df2/attachment.html>
More information about the Haskell-Cafe
mailing list