ahrefs / atd

Static types for JSON APIs
Other
317 stars 53 forks source link

JSON to OCaml #337

Open ArulselvanMadhavan opened 1 year ago

ArulselvanMadhavan commented 1 year ago

Thanks for writing this library!

Has there been any work to infer the types directly from the JSON as opposed to having the user add type annotations?

Example: https://github.com/mholt/json-to-go

mjambon commented 1 year ago

I'm not aware of a tool like this that people could use today for ATD. I did similar work over ten years ago but it was never released. It's definitely a fun project that doesn't require knowledge of the atd internals. I imagine it could produce good guesses for the most common patterns. Off the top of my head:

Tuples are more exotic but could be inferred given enough data (e.g. [ [1, "a"], [2, "ddd"], [32, "qiwi"], [-999, "jsjs"] ](int * string) list).

Variants that are not just enums are more problematic as they don't have a standard representation outside of ATD.

If someone wants to start such a project in OCaml, let us know here! It would be a good fit in this repo. Alternatively, extending another project like https://github.com/mholt/json-to-go to use the ATD syntax could also work.

Khady commented 1 year ago

@cyberhuman wrote a tool that was doing that. But I don't think it's open source (yet?)

cyberhuman commented 1 year ago

I wrote a very basic POC long time ago but haven't worked on it ever since: https://github.com/cyberhuman/fmtcheck/blob/master/json/json.ml

When working on it, I realized that writing an initial atd is very easy, but what I really want is to be able to update it easily when the wire format is updated, or fine-tune auto-detected types. This would require the tool to take the existing atd as an input.