json-to-type: Automatic type declaration for JSON input data

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Warnings:

Generates datatype declarations with Aeson''s 'Data.Aeson.FromJSON' . instances from a set of example .json files. . . To get started you need to install the package, . and run json-to-type binary on an input .json file. . That will generate a new Aeson-based JSON parser. . . > $ json-to-type input.json -o JSONTypes.hs . . Feel free to tweak the by changing types of the fields . — any field type that is instance of 'Data.Aeson.FromJSON' should work. . . You may immediately test the parser by calling it as a script: . . > $ runghc JSONTypes.hs input.json . . One can now use multiple input files to generate better type description. . . Now with Elm code generation support! . (If you want your favourite programming language supported too — . name your price and mail the author.) . . See introduction on https://github.com/mgajda/json-to-type for details.' .


[Skip to Readme]

Properties

Versions 4.0.0, 4.0.0, 4.0.1
Change log changelog.md
Dependencies aeson, base (>=4.0.0 && <5.0), bytestring, containers, data-default, filepath, GenericPretty, hashable, json-to-type, lens, mtl, optparse-applicative, pretty, process, QuickCheck, run-haskell-module, scientific, smallcheck, template-haskell, text, uniplate, unordered-containers, vector, yaml [details]
License BSD-3-Clause
Copyright Copyright by Migamake '2014-'2020
Author Michal J. Gajda
Maintainer simons@cryp.to, mjgajda@gmail.com
Category Data, Tools
Home page https://github.com/jappeace/json-to-type.git#readme
Bug tracker https://github.com/jappeace/json-to-type/issues
Source repo head: git clone https://github.com/jappeace/json-to-type.git
Uploaded by Jappie at 2024-09-24T10:21:01Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for json-to-type-4.0.0

[back to package description]

json-to-type

Fork of json-autotype. It just builds and such.

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.) :|: alternatives (similar to Either) are used to assure that all JSON inputs seen in example input file are handled correctly.

Details on official releases are on Hackage We currently support code generation to Haskell, and Elm.

USAGE:

After installing with cabal install json-to-type, you might generate stub code for the parser:

    json-to-type input1.json ... inputN.json -o MyFormat.hs

Then you might test the parser by running it on an input file:

    runghc MyFormat.hs input.json

At this point you may see data structure generated automatically for you. The more input files you give to the inference engine json-to-type, the more precise type description will be.

Algorithm will also suggest which types look similar, based on a set of attribute names, and unify them unless specifically instructed otherwise.

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-to-type 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.

You can run with Docker:

docker run -it migamake/json-to-type

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 :: Bool :|: Int :|: Text,
        parameterParameterName :: Text
      }

    data TopLevel = TopLevel {
        topLevelParameter :: Parameter
      }

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

Methodology:

  1. Json-To-Type uses its own union type system to derive types from JSON documents as the first step.
  2. Then it finds all those records that have 90% of the same key names, and suggest them as similar enough to merit treating as instances of the same type. (Note that this is optional, and can be tuned manually.)
  3. Last step is to derive unique-ish type names - we currently do it by concatenating the name of the container and name of the key. (Please open PR, if you want something fancy about that - initial version used just key name, when it was unique.)
  4. Finally it generates Haskell or Elm code for the type.

Combination of robust union type system, and heuristic makes this system extremely reliable. Main test is QuickCheck-based generation of random JSON documents, and checking that they are all correctly parsed by resulting parser.

More details are described in Haskell.SG meetup presentation.

Other approaches: