Json of a data structure

As a general rule, this kind of function has to be written by someone. Because they are annoying to write by hand, you can use automated PPX derivers to write them for you.

For JSON, ppx_deriving_yojson can do that job, for instance. In terms of code, this consists in adding [@@deriving yojson] to the types for which you want JSON conversion.

Note that for a type defined in terms of other, non-primitive types, those other types need to have JSON derivers as well. For a big type like a parse tree, adding annotations to all types can be a big (and invasive) task. If rescript provides a generic fold over their parsetree, you might be better off writing the derivers “by hand” from these functions.

Off-topic, but that is why we need more libraries to export type representants :slight_smile:

2 Likes