Library
Module
Module type
Parameter
Class
Class type
ezjsonm-encoding
is an encoding combinators library for Ezjsonm
whose API is heavily inspired by data-encoding
.
The two main differences between ezjsonm-encoding
and the JSON support of data-encoding are:
objN
combinators accept JSON objects with _more_ fields than the one explicitely specified in the encoding.ezjsonm-encoding
does not have a dependency to Zarith
(which depends on GMP). As a consequence, `ezjsonm-encoding` does not provide support for big numbers out of the box.The examples of this documentation can be copy/pasted to an OCaml REPL.
val to_value_exn : 'a t -> 'a -> Ezjsonm.value
to_value_exn encoding value
encodes value
in JSON using encoding
. Will raise an exception in case encoding
cannot serialize value
.
open Ezjsonm_encoding
let encoding = obj1 (req "hello" string)
let json = to_value_exn encoding "world"
(* `O [("hello", `String "world")] *)
val to_value : 'a t -> 'a -> Ezjsonm.value option
to_value_exn encoding value
encodes value
in JSON using encoding
. Will return None
in case encoding
cannot serialize value
.
val to_string_exn : minify:bool -> 'a t -> 'a -> string
to_string_exn ~minify encoding value
encodes value
using encoding
into a JSON value serialized in a string. Will raise an exception in case encoding
cannot serialize value
.
val to_string : minify:bool -> 'a t -> 'a -> string option
to_string ~minify encoding value
encodes value
using encoding
into a JSON value serialized in a string. Will return None
in case encoding
cannot serialize value
.
val from_string_exn : 'a t -> string -> 'a
from_string_exn encoding str
decodes a JSON value from str
, then uses encoding
to construct an OCaml value. Will raise an exception if str
is not a valid JSON value, or if encoding
cannot construct an OCaml value from str
.
val from_string : 'a t -> string -> 'a option
from_string encoding str
decodes a JSON value from str
, then uses encoding
to construct an OCaml value. Will return None
if str
is not a valid JSON value, or if encoding
cannot construct an OCaml value from str
.
conv f g encoding
crafts a new encoding from encoding
. This is typically used to creates a JSON encoder/decoder for OCaml records by projecting them to tuples, and using objN
combinators.
open Ezjsonm_encoding
type t = { f1 : int; f2 : bool }
let encoding =
conv
(fun { f1; f2 } -> (f1, f2))
(fun (f1, f2) -> { f1; f2 })
(obj2 (req "f1" int) (req "f2" bool))
let json = to_value_exn encoding { f1 = 0; f2 = true }
(* `O [("f2", `Bool true); ("f1", `Float 0.)] *)
val string_enum : (string * 'a) list -> 'a t
string_enum
maps JSON strings to fixed OCaml values.
open Ezjsonm_encoding
type toggle = On | Off
let toggle_encoding = string_enum [ "on", On; "off", Off ]
let json = to_value_exn toggle_encoding On
(* `String "on" *)
let toggle = from_string_exn toggle_encoding {|"on"|}
(* On *)
val string : string t
The encoding which maps JSON strings and OCaml strings.
open Ezjsonm_encoding
let json = to_value_exn string "hello, world!"
(* `String "hello, world!" *)
let str = from_string_exn string {|"hello, world!"|}
(* "hello, world!" *)
val int64 : int64 t
The encoding which maps JSON ints and OCaml int64. As a reminder, Ezjsonm uses floats internally to encode integers.
open Ezjsonm_encoding
let json = to_value_exn int64 1L
(* `Float 1. *)
let str = from_string_exn int64 "1"
(* 1L *)
val int : int t
The encoding which maps JSON ints and OCaml ints. As a reminder, Ezjsonm uses floats internally to encode integers.
open Ezjsonm_encoding
let json = to_value_exn int 1
(* `Float 1. *)
let str = from_string_exn int "1"
(* 1 *)
val bool : bool t
The encoding which maps JSON booleans and OCaml booleans.
open Ezjsonm_encoding
let json = to_value_exn bool false
(* `Bool false *)
let str = from_string_exn bool "false"
(* false *)
list encoding
creates a encoding for a list of values based on the encoding of said values.
open Ezjsonm_encoding
let json = to_value_exn (list bool) [true; true; false]
(* `A [`Bool true; `Bool true; `Bool false] *)
let str = from_string_exn (list int) "[1, 2, 3]"
(* [1; 2; 3] *)
The description of one field of a JSON object. See req
and opt
to construct field
values, and obj1
to obj10
and merge_objs
to construct encoding for objects.
req field_name encoding
represents a required field. That is, the decoding will fail if provided an object lacking this field (and raises an exepction with from_string_exn
and from_string_exn
).
open Ezjsonm_encoding
let json = to_value_exn (obj1 (req "hello" string)) "world!"
(* `O [("hello", `String "world!")] *)
let str = from_string_exn (obj1 (req "hello" string)) {|{ "hello": "world!"}|}
(* "world!" *)
let str = from_string (obj1 (req "hello" string)) {|{ "bye": "world!"}|}
(* None *)
opt field_name encoding
represents an optional field (i.e., wrapped in an option
).
open Ezjsonm_encoding
let json = to_value_exn (obj1 (opt "hello" string)) (Some "world!")
(* `O [("hello", `String "world!")] *)
let json' = to_value_exn (obj1 (opt "hello" string)) None
(* `O [] *)
let str = from_string_exn (obj1 (opt "hello" string)) {|{ "hello": "world!"}|}
(* Some "world!" *)
let str = from_string (obj1 (opt "hello" string)) {|{ "bye": "world!"}|}
(* Some None *)
merg_objs obj1 obj2
represents an object characterized by at least the fields of obj1
and obj2
. This is useful when an object expects at least more than 10 fields. Note that it is expected that obj1
and obj2
do not have conflict wrt. field names. This is not checked by ezjsonm-encoding
, and is considered an undefined behavior (which may change in a future version of the library).
open Ezjsonm_encoding
let json =
to_value_exn
(merge_objs
(obj2 (req "foo" string) (req "bar" bool))
(obj1 (opt "foobar" int)))
(("hello", true), Some 1)
(* `O [("bar", `Bool true); ("foo", `String "hello"); ("foobar", `Float 1.)] *)
module Decoding : sig ... end