-
Notifications
You must be signed in to change notification settings - Fork 15
Json Driver
The Json driver maps to and from Yojson.Safe.t
.
The opam package for the driver is ppx_protocol_conv_json
.
Standard options mimics that of ppx_deriving_yojson
, except that
Constructors without arguments are serialized to a string rather than
a list.
To set options to create a driver compatible with ppx_deriving_yojson
use
open Protocol_conv_json
module Yojson = Json.Make(
struct
include Ppx_protocol_driver.Default_parameters
let singleton_constr_as_string = false
let omit_default_values = true
end : Ppx_protocol_driver.Parameters)
The module Json.Yojson
provides a module with the above parameters set for convenience.
For speed comparison, see the benchmarks page.
See Parameters for a description of possible options.
Ocaml type | Generates | Accepts |
---|---|---|
string, char,bytes | `String | `String |
int, int32, int64 | `Int | `Int |
nativeint | `Int | `Int |
float | `Float | `Float |
bool | `Bool | `Bool |
unit | `List [] | `List [] |
'a list, 'a array | `List 'a list | `List 'a list |
'a option | `Null or 'a | `Null or 'a |
'a ref, lazy 'a | 'a | 'a |
Json.t | Yojson.Safe.t | Yojson.Safe.t |
Serialization differs from ppx_deriving_yojson
when serializing in
that constructors without arguments are serialized to strings, rather
than a list. Constructors with arguments are serialized to lists.
This allows for deserialising a string directly into a ADT:
type country = Denmark | France
and t = {
name: string;
country: country;
} [@@deriving protocol ~driver:(module Json)]
{ name = "Anders"; country = Denmark } |> to_json |> Yojson.Safe.to_string
produces: { "name": "Anders", "country": "Denmark" }