Ppx deriver for serializing OCaml value to Json
The deriver is provided by the js_of_ocaml-ppx_deriving_json opam package.
The serialisation format follows js_of_ocaml's internal memory representation of OCaml values. It is not meant to be used to interact with 3rd party system. In practice, it can be used to exchange data between a js_of_ocaml program and a server side OCaml application.
The serialisation format allows a js_of_ocaml program to encode/decode OCaml value efficiently by leveraging JavaScript native Json support. See [Js_of_ocaml.Json.output] and [Js_of_ocaml.Json.output].
Syntax
Generate serialisation from type definitions
One can derive serialisation functions from type definitions using the [@@deriving ..] attribute.
type t = A | B
[@@deriving json]
val t_of_json : Deriving_Json_lexer.lexbuf -> t = <fun>
val t_to_json : Buffer.t -> t -> unit = <fun>
val t_json : t Deriving_Json.t = <abstr>
Generate serialisation from types
One can generate an expression to serialise/deserialise value of an arbitrary type with the following syntax:
let (_ : t Deriving_json.t) = [%json: t]
let (_ : string -> t) = [%of_json: t]
let (_ : t -> string) = [%to_json: t]