Static typing promises safer code, and easier scaling to large projects. Polymorphic types with classes promise high level of abstraction typical of scripting languages, and at the same time strong discipline.
However not all our data is initially typed. Most common data format used by web apps is dynamically typed JSON format. Web API descriptions are usually just free form text with examples given as JSON documents.
How one is supposed to type that?
JSON-Autotype project promises to solve this problem automatically. It takes a couple of JSON documents and produces valid type declaration. It also provides a parser and printer for this JSON format, and is guaranteed that sample inputs will always parse.
It is also much faster than inferring the structure of JSON documents by your own human eyes. Some of our users have used it on megabytes and gigabytes of documents[^gigabytes].
Take an example JSON document:
It will be used to generate the following Haskell[^languages] type declaration:
Consider a case of ambiguous types, like the following:
:|: union type (similar to
Either but without a tag):
Theory behind the machine
Then it uses
TUnion of a set of types to reconcile values that sometimes have different primitive JSON types. (
TObj represents dictionary object.) Then it uses unification to reconcile types between different values. Another successful use of mathematical theory to simplify practical software problem.
[^gigabytes] Please contact us, if you are interested in funding work on scaling JSON parsing to large documents without compromising type safety, and with minimal change of interface.
[^languages] Or Elm. Please let us know if you are interested to fund work on other languages like PureScript, Scala, or F#.
Other articles about JSON Autotype: