ahrefs / atd

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

atdgen: mixing ATD with raw Yojson.Basic.t objects #398

Closed Karmaki closed 6 months ago

Karmaki commented 6 months ago

I have a large object built from Yojson.Basic elements and I want to add a new section in it, but I would prefer to use ATD features to specify and generate it. However, I must miss something, because I do not find how to do that: is it possible? I do not find any function that would translate my object into a Yojson.Basic.t object that I can integrate in the existing code...

mjambon commented 6 months ago

Hi! You're correct that there's no function to convert from OCaml to a Yojson tree directly. The way to do this is to convert from OCaml to a string using the string_of_xxx function generated by atdgen and then parsing the string into a Yojson tree with Yojson.Basic.from_string. It's pretty fast since the OCaml-to-string conversion done by atdgen doesn't create an intermediate tree and writes the string directly.

Karmaki commented 6 months ago

Thanks a lot for your answer. So, I finally did not miss anything if it does not exist ;-) Also thanks for the suggestion: I'll use it if I keep the idea of using ATDgen.