Module Data_encoding.Json

val schema : ?definitions_path:string -> 'a encoding -> json_schema
val construct : ?include_default_fields:[ `Always | `Auto | `Never ] -> 't encoding -> 't -> json

Construct a JSON object from an encoding.

val destruct : ?bson_relaxation:bool -> 't encoding -> json -> 't

Destruct a JSON object into a value. Fail with an exception if the JSON object and encoding do not match.

  • parameter [bson_relaxation]

    (default to false) works around a limitation of the BSON format. Specifically, in BSON, top-level arrays are represented as number-indexed objects. When bson_relaxation is true, then the destructor attempts to automatically translate the indistinguishable values as guided by the encoding.

JSON Error

type path = path_item list
and path_item = [
  1. | `Field of string
    (*

    A field in an object.

    *)
  2. | `Index of int
    (*

    An index in an array.

    *)
  3. | `Star
    (*

    Any / every field or index.

    *)
  4. | `Next
    (*

    The next element after an array.

    *)
]

A set of accessors that point to a location in a JSON object.

exception Cannot_destruct of path * exn

Exception raised by destructors, with the location in the original JSON structure and the specific error.

exception Unexpected of string * string

Unexpected kind of data encountered, with the expectation.

exception No_case_matched of exn list

Some union couldn't be destructed, with the reasons for each case.

exception Bad_array_size of int * int

Array of unexpected size encountered, with the expectation.

exception Missing_field of string

Missing field in an object.

exception Unexpected_field of string

Supernumerary field in an object.

val print_error : ?print_unknown:(Format.formatter -> exn -> unit) -> Format.formatter -> exn -> unit
val cannot_destruct : ('a, Format.formatter, unit, 'b) Pervasives.format4 -> 'a

Helpers for writing encoders.

val wrap_error : ('a -> 'b) -> 'a -> 'b
val pp : Format.formatter -> json -> unit