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 documents1.
Take an example JSON document:
It will be used to generate the following Haskell2 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.
Other articles about JSON Autotype:
- 24 days of Hackage 2015
- 24 dias de Hackage 2015
- Aelve guide to JSON lists JSON Autotype as alternative instance generator.