Json of a data structure

Hey everyone.

I am new to OCaml and was wondering can i get the JSON of any data structure in OCaml? So i was looking at the Rescript compiler code and there is a data structure called Parsetree. I wanted to view its json format. Is that possible?

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:


@otini Please look into this issue…

I am facing while using ppx_deriving_yojson.
can you please help me here.

The only issue I have is with the open Yojson which is not used. An open! Yojson fix my issue.

You should check your dune file, and verify the presence of a

(preprocess (pps ppx_deriving_yojson)) stanza.