package ocplib-json-typed

  1. Overview
  2. Docs

Representations of JSON documents

Abstraction over JSON representations

type 'a view = [
  1. | `O of (string * 'a) list
    (*

    An associative table (object).

    *)
  2. | `A of 'a list
    (*

    An (integer indexed) array.

    *)
  3. | `Bool of bool
    (*

    A JS boolean true or false.

    *)
  4. | `Float of float
    (*

    A floating point number (double precision).

    *)
  5. | `String of string
    (*

    An UTF-8 encoded string.

    *)
  6. | `Null
    (*

    The null constant.

    *)
]

The internal format used by the library. A common format to view JSON structures from different representations. It only shows the head of structures, hiding the contents of fields, so that the conversion from another format or a stream can be done lazily.

type 'a repr_uid

Each representation must provide a unique identifier, obtained via the repr_uid function. This identifier is used when converting between representations, to optimize out a copy when converting from a representation to itself. Beware that this optimization relies only on this uid token. Converting between values of the same type using two different representation modules with different uids will perform a copy. A practical way to ensure that the optimization is made is to write your representations as toplevel modules, and not inside functors.

val repr_uid : unit -> 'a repr_uid
module type Repr = sig ... end

A view over a given implementation.

val convert : (module Repr with type value = 'tf) -> (module Repr with type value = 'tt) -> 'tf -> 'tt

Convert a JSON value from one representation to another.

val pp : ?compact:bool -> ?pp_string:(Format.formatter -> string -> unit) -> (module Repr with type value = 'tf) -> Format.formatter -> 'tf -> unit

Generic pretty-printer. If compact is set (by default), then the output is not really pretty (no space is output). Ascii-compatible string encoding is expected, as printing only escapes double quotes and control characters. Use pp_string for more advanced escaping. This function does not claim to be the best JSON pretty printer, it is mostly a small utility.

Third party in-memory JSON document representations

type ezjsonm = [
  1. | `O of (string * ezjsonm) list
    (*

    An associative table (object).

    *)
  2. | `A of ezjsonm list
    (*

    An (integer indexed) array.

    *)
  3. | `Bool of bool
    (*

    A JS boolean true or false.

    *)
  4. | `Float of float
    (*

    A floating point number (double precision).

    *)
  5. | `String of string
    (*

    An UTF-8 encoded string.

    *)
  6. | `Null
    (*

    The null constant.

    *)
]

A JSON value compatible with Ezjsonm.value.

module Ezjsonm : Repr with type value = ezjsonm

A view over the ezjsonm representation.

type yojson = [
  1. | `Bool of bool
    (*

    A JS boolean true of false.

    *)
  2. | `Assoc of (string * yojson) list
    (*

    JSON object.

    *)
  3. | `Float of float
    (*

    A floating point number (double precision).

    *)
  4. | `Int of int
    (*

    A number without decimal point or exponent.

    *)
  5. | `Intlit of string
    (*

    A number without decimal point or exponent, preserved as string.

    *)
  6. | `List of yojson list
    (*

    A JS array.

    *)
  7. | `Null
    (*

    The null constant.

    *)
  8. | `String of string
    (*

    An UTF-8 encoded string.

    *)
  9. | `Tuple of yojson list
    (*

    A tuple (non-standard). Syntax: ("abc", 123).

    *)
  10. | `Variant of string * yojson option
    (*

    A variant (non-standard). Syntax: <"Foo"> or <"Bar": 123>.

    *)
]

A JSON value compatible with Yojson.Safe.json.

module Yojson : Repr with type value = yojson

A view over the yojson representation.

Representation-agnostic JSON format

type any = private
  1. | Value_with_repr : (module Repr with type value = 'a) * 'a -> any

A meta-representation for JSON values that can unify values of different representations by boxing them with their corresponding Repr modules.

val any_to_repr : (module Repr with type value = 'a) -> any -> 'a

Converts a boxed value from its intrinsic representation to the one of the given Repr module. Optimized if the internal representation of the value actually is the requested one.

val repr_to_any : (module Repr with type value = 'a) -> 'a -> any

Boxes a value with a compatible Repr module.

val pp_any : ?compact:bool -> ?pp_string:(Format.formatter -> string -> unit) -> unit -> Format.formatter -> any -> unit

Pretty-printer for values of type any. See pp for details.

Predefined converters for ezjsonm

val from_yojson : [< yojson ] -> [> ezjsonm ]

Conversion helper.

val to_yojson : [< ezjsonm ] -> [> yojson ]

Conversion helper.

val from_any : any -> [> ezjsonm ]

Converts a boxed value from its representation to ezjsonm.

val to_any : [< ezjsonm ] -> any

Boxes as ezjsonm value.