json-autotype

Automatic type declaration for JSON input data

https://github.com/mgajda/json-autotype

Version on this page:0.2.5.9
LTS Haskell 14.27:3.0.1
Stackage Nightly 2019-09-21:3.0.1
Latest on Hackage:3.1.2

See all snapshots json-autotype appears in

BSD-3-Clause licensed by Michal J. Gajda
Maintained by [email protected]
This version can be pinned in stack with:json-autotype-0.2.5.9@sha256:6cccb5e3ff4b9adb18f3bf3c0ecd012f0fabe97478e275498b3c34735316774c,5278

json-autotype

Takes a JSON format input, and generates automatic Haskell type declarations.

Parser and printer instances are derived using Aeson.

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.

I should probably write a short paper to explain the methodology.

Build Status Hackage

Details on official releases are on Hackage

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.

Patches and suggestions are welcome.

EXAMPLES:

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
      }

Real-world use case examples are provided in the package source repository.

Changes

Changelog

0.2.5.8  Mar 2015

    * Updated tests and build config.

0.2.5.7  Mar 2015

    * Fixed documentation anchors, and unit test classification for failures.

0.2.5.6  Mar 2015

    * Relaxed upper bounds for lens 4.8.

0.2.5.5 Mar 2015

    * (Skipped this version number by mistake.)

0.2.5.4  Dec 2014

    * Relaxed upper bounds for new lens.

0.2.5.3  Dec 2014

    * Relaxed upper bounds again.

0.2.5.2  Dec 2014

    * Updated metainfo, relaxed upper bounds for GHC 7.10.

0.2.5.0  Nov 2014

    * Nicer union type syntax in Data.Aeson.AutoType.Alternative.

0.2.4.0  Nov 2014

    * To assure proper treatment of unions,
      I make them with Data.Aeson.AutoType.Alternative type instead of Either.

0.2.3.0  Nov 2014

    * Explicit JSON parser generation to avoid conflicts between Haskell keywords and field names.
    * Renaming of Haskell field names with a prefix of object name (data type.)

0.2.2.0  Nov 2014

    * GenerateJSONParser may now take multiple input samples to produce single parser.
    * Fixed automated testing for all example files.

0.2.1.4  Oct 2014

    * Added examples to the package distribution.

0.2.1.3  Oct 2014

    * Cleaned up package.
    * Changelog in markdown format.

0.2.1  Oct 2014

    * Added option to use it as a filter ('-' is accepted input name.)

0.2.0  Oct 2014

    * First release to Hackage.
    * Handling of proper unions, and most examples.
    * Automatically tested on a wide range of example documents (see
    tests/)
    * Initial documentation in README.md.

0.1.0  July 2014

* First experiments uploaded to GitHub, and discussed to
HackerSpace.SG.