Serialization

43.1 JSON with Yojson

let json = `Assoc [ ("name", `String "Ann"); ("age", `Int 30); ("tags", `List [`String "x"]) ]
let s = Yojson.Safe.to_string json
let pretty = Yojson.Safe.pretty_to_string json

let parsed = Yojson.Safe.from_string {|{"name":"Bob","age":25}|}
let name = Yojson.Safe.Util.(parsed |> member "name" |> to_string)

43.2 Deriving JSON converters

type user = { name : string; age : int; email : string option [@default None] }
[@@deriving yojson]
(* generates user_of_yojson, yojson_of_user *)

43.3 S-expressions (Sexplib / Jane Street)

type config = { host : string; port : int } [@@deriving sexp]
let c = config_of_sexp (Sexplib.Sexp.of_string "((host localhost) (port 80))")

43.4 Other formats

otoml, toml xmlm, markup Binary (fast, typed) data-encoding, repr