ahrefs / atd

Static types for JSON APIs
Other
309 stars 52 forks source link

atdpy: Add support for partial parsing #344

Open mjambon opened 1 year ago

mjambon commented 1 year ago

For atdpy or other code generators, we would like to add the option to skip malformed elements and report them as errors, as it's done by tree-sitter parsers and ocaml-tree-sitter.

Partial parsing is based on removing malformed elements from list and option containers. This results in a well-formed result and a list of malformed JSON nodes rather than failing completely.

Applications

The goal is for a JSON consumer - typically a client reading a response from a server - to make the best of what the server produces. This happens when the server response is a list of items of which some declare a new kind:

type flavor = [
| Vanilla
| Chocolate
| Mango
| Pear (* new! *)
]

type available_flavors = flavor list

The following JSON data is of type available_flavors:

[ "Vanilla", "Pear", "Chocolate" ]

An older client that doesn't know about the latest Pear flavor can't read this list correctly but it can ignore the unknown flavor. The generated from_json function would read the JSON input as if it were

[ "Vanilla", "Chocolate" ]

and would report the node "Pear" as an error where the type available_flavors (older version) was expected.