All Projects → migamake → Json Autotype

migamake / Json Autotype

Licence: other
Automatic Haskell type inference from JSON input

Programming Languages

haskell
3896 projects
elm
856 projects

Projects that are alternatives of or similar to Json Autotype

Barely json
A Python parser for data that only looks like JSON
Stars: ✭ 56 (-59.71%)
Mutual labels:  json, parser
Go
A high-performance 100% compatible drop-in replacement of "encoding/json"
Stars: ✭ 10,248 (+7272.66%)
Mutual labels:  json, parser
Cssparser.js
cssparser.js is a parser that generate json from css with matched orders & structures.
Stars: ✭ 61 (-56.12%)
Mutual labels:  json, parser
Algebra Latex
Parse and calculate latex formatted math
Stars: ✭ 20 (-85.61%)
Mutual labels:  parser, parse
Forge
Functional style JSON parsing in Kotlin
Stars: ✭ 106 (-23.74%)
Mutual labels:  json, parser
Parson
Lightweight JSON library written in C.
Stars: ✭ 965 (+594.24%)
Mutual labels:  json, parser
Internettools
XPath/XQuery 3.1 interpreter for Pascal with compatibility modes for XPath 2.0/XQuery 1.0/3.0, custom and JSONiq extensions, XML/HTML parsers and classes for HTTP/S requests
Stars: ✭ 82 (-41.01%)
Mutual labels:  json, parser
Librini
Rini is a tiny, non-libc dependant, .ini file parser programmed from scratch in C99.
Stars: ✭ 25 (-82.01%)
Mutual labels:  parser, parse
Swurg
Parse OpenAPI documents into Burp Suite for automating OpenAPI-based APIs security assessments (approved by PortSwigger for inclusion in their official BApp Store).
Stars: ✭ 94 (-32.37%)
Mutual labels:  json, parser
Postcss Less
PostCSS Syntax for parsing LESS
Stars: ✭ 93 (-33.09%)
Mutual labels:  parser, parse
Xml Js
Converter utility between XML text and Javascript object / JSON text.
Stars: ✭ 874 (+528.78%)
Mutual labels:  json, parser
Alembic
⚗️ Functional JSON Parser - Linux Ready 🐧
Stars: ✭ 115 (-17.27%)
Mutual labels:  json, parser
Html React Parser
📝 HTML to React parser.
Stars: ✭ 846 (+508.63%)
Mutual labels:  parser, parse
Fast Xml Parser
Validate XML, Parse XML to JS/JSON and vise versa, or parse XML to Nimn rapidly without C/C++ based libraries and no callback
Stars: ✭ 1,021 (+634.53%)
Mutual labels:  json, parser
Parse Code Context
Parse code context in a single line of javascript, for functions, variable declarations, methods, prototype properties, prototype methods etc.
Stars: ✭ 7 (-94.96%)
Mutual labels:  parser, parse
Parser
Generate a JSON documentation for a SFC Vue component. Contribute: https://gitlab.com/vuedoc/parser#contribute
Stars: ✭ 74 (-46.76%)
Mutual labels:  parser, parse
Dasel
Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.
Stars: ✭ 759 (+446.04%)
Mutual labels:  json, parser
Jkt
Simple helper to parse JSON based on independent schema
Stars: ✭ 22 (-84.17%)
Mutual labels:  json, parser
Java
jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go
Stars: ✭ 1,308 (+841.01%)
Mutual labels:  json, parser
Netcopa
Network Configuration Parser
Stars: ✭ 112 (-19.42%)
Mutual labels:  parser, parse

json-autotype

The project moved to https://gitlab.com/migamake/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.) :|: alternatives (similar to Either) are 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.

Release build Hackage Hackage Dependencies Docker Automated build Docker image size

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

Please volunteer help or provide financial support, if you want your favourite language supported too! Expression of interest in particular feature may be filed as GitHub issue.

USAGE:

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

    json-autotype 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-autotype, 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-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.

You can run with Docker:

docker run -it migamake/json-autotype

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-Autotype 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:

  • There is a TypeScript type provider, and PLDI 2016 paper on solving this problem using preferred type shapes instead of union types. One can think about it as a alternative theory that gives very similar results, with more complicated exposition. It also does not tackle the problem of tagged records. It also does not attempt to guess unification candidates in order to reduce type complexity.

  • There was a json-sampler that allows to make simpler data structure from JSON examples, but doesn't seem to perform unification, nor is it suitable for big APIs.

  • PADS project is another attempt to automatically infer types to treat arbitrary data formats (not just JSON). It mixes type declarations, with parsing/printing information in order to have a consistent view of both. It does not handle automatic type inference though.

  • JSON Schema generator uses .NET types to generate JSON Schema instead (in opposite direction.) Similar schema generation is used here

  • Microsoft Developer Network advocates use of Data Contracts instead to constrain possible input data.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].