Library
Module
Module type
Parameter
Class
Class type
type 'a encoding = 'a t
val null : unit encoding
Special value null
in JSON, nothing in binary.
val empty : unit encoding
Empty object (not included in binary, encoded as empty object in JSON).
val unit : unit encoding
Unit value, omitted in binary. Serialized as an empty object in JSON, accepts any object when deserializing.
val constant : string -> unit encoding
Constant string (data is not included in the binary data).
val int8 : int encoding
Signed 8 bit integer (data is encoded as a byte in binary and an integer in JSON).
val uint8 : int encoding
Unsigned 8 bit integer (data is encoded as a byte in binary and an integer in JSON).
val int16 : int encoding
Signed 16 bit integer (data is encoded as a short in binary and an integer in JSON).
val uint16 : int encoding
Unsigned 16 bit integer (data is encoded as a short in binary and an integer in JSON).
val int31 : int encoding
Signed 31 bit integer, which corresponds to type int on 32-bit OCaml systems (data is encoded as a 32 bit int in binary and an integer in JSON).
val int32 : int32 encoding
Signed 32 bit integer (data is encoded as a 32-bit int in binary and an integer in JSON).
val int64 : int64 encoding
Signed 64 bit integer (data is encoded as a 64-bit int in binary and a decimal string in JSON).
val ranged_int : int -> int -> int encoding
Integer with bounds in a given range. Both bounds are inclusive.
Big number In JSON, data is encoded as a decimal string. In binary, data is encoded as a variable length sequence of bytes, with a running unary size bit: the most significant bit of each byte tells is this is the last byte in the sequence (0) or if there is more to read (1). The second most significant bit of the first byte is reserved for the sign (positive if zero). Binary_size and sign bits ignored, data is then the binary representation of the absolute value of the number in little-endian order.
val float : float encoding
Encoding of floating point number (encoded as a floating point number in JSON and a double in binary).
val ranged_float : float -> float -> float encoding
Float with bounds in a given range. Both bounds are inclusive
val bool : bool encoding
Encoding of a boolean (data is encoded as a byte in binary and a boolean in JSON).
val string : string encoding
Encoding of a string
Encoding of arbitrary bytes (encoded via hex in JSON and directly as a sequence byte in binary).
Combinator to make an optional value (represented as a 1-byte tag followed by the data (or nothing) in binary and either the raw value or an empty object in JSON).
Combinator to make a result
value (represented as a 1-byte tag followed by the data of either type in binary, and either unwrapped value in JSON (the caller must ensure that both encodings do not collide)).
Array combinator.
If max_length
is passed and the encoding of elements has fixed size, a check_size
is automatically added for earlier rejection.
List combinator.
If max_length
is passed and the encoding of elements has fixed size, a check_size
is automatically added for earlier rejection.
val conv :
('a -> 'b) ->
('b -> 'a) ->
?schema:Json_schema.schema ->
'b encoding ->
'a encoding
Association list. An object in JSON, a list of pairs in binary.
An enriched encoding to represent a component in a structured type, augmenting the encoding with a name and whether it is a required or optional. Fields are used to encode OCaml tuples as objects in JSON, and as sequences in binary, using combinator obj1
and the like.
Optional field. Omitted entirely in JSON encoding if None. Omitted in binary if the only optional field in a `Variable
encoding, otherwise a 1-byte prefix (`0` or `255`) tells if the field is present or not.
Optional field of variable length. Only one can be present in a given object.
Required field with a default value. If the default value is passed, the field is omitted in JSON. The value is always serialized in binary.
These are serialized to binary by converting each internal object to binary and placing them in the order of the original object. These are serialized to JSON as a JSON object with the field names. An object might only contains one 'variable' field, typically the last one. If the encoding of more than one field are 'variable', the first ones should be wrapped with dynamic_size
.
Create a larger object from the encodings of two smaller ones.
These are serialized to binary by converting each internal object to binary and placing them in the order of the original object. These are serialized to JSON as JSON arrays/lists. Like objects, a tuple might only contains one 'variable' field, typically the last one. If the encoding of more than one field are 'variable', the first ones should be wrapped with dynamic_size
.
Create a large tuple encoding from two smaller ones.
A partial encoding to represent a case in a variant type. Hides the (existentially bound) type of the parameter to the specific case, providing its encoder, and converter functions to and from the union type.
val case :
title:string ->
?description:string ->
case_tag ->
'a encoding ->
('t -> 'a option) ->
('a -> 't) ->
't case
Encodes a variant constructor. Takes the encoding for the specific parameters, a recognizer function that will extract the parameters in case the expected case of the variant is being serialized, and a constructor function for deserialization.
The tag must be less than the tag size of the union in which you use the case. An optional tag gives a name to a case and should be used to maintain compatibility.
An optional name for the case can be provided, which is used in the binary documentation.
Create a single encoding from a series of cases.
In JSON, all cases are tried one after the other. The caller must check for collisions.
In binary, a prefix tag is added to discriminate quickly between cases. The default is `Uint8
and you must use a `Uint16
if you are going to have more than 256 cases.
val is_obj : 'a encoding -> bool
Is the given encoding serialized as a JSON object?
val is_tup : 'a encoding -> bool
Does the given encoding encode a tuple?
val classify : 'a encoding -> [ `Fixed of int | `Dynamic | `Variable ]
Classify the binary serialization of an encoding as explained in the preamble.
val string_enum : (string * 'a) list -> 'a encoding
Encode enumeration via association list
module Fixed : sig ... end
Create encodings that produce data of a fixed length when binary encoded. See the preamble for an explanation.
module Variable : sig ... end
Create encodings that produce data of a variable length when binary encoded. See the preamble for an explanation.
module Bounded : sig ... end
Mark an encoding as being of dynamic size. Forces the size to be stored alongside content when needed. Typically used to combine two variable encodings in a same objects or tuple, or to use a variable encoding in an array or a list.
check_size size encoding
ensures that the binary encoding of a value will not be allowed to exceed size
bytes. The reader and the writer fails otherwise. This function do not modify the JSON encoding.
Recompute the encoding definition each time it is used. Useful for dynamically updating the encoding of values of an extensible type via a global reference (e.g., exceptions).
Define different encodings for JSON and binary serialization.
val mu :
string ->
?title:string ->
?description:string ->
('a encoding -> 'a encoding) ->
'a encoding
Combinator for recursive encodings.
Give a name to an encoding and optionally add documentation to an encoding.
See lazy_encoding
below.
Combinator to have a part of the binary encoding lazily deserialized. This is transparent on the JSON side.
val force_decode : 'a lazy_t -> 'a option
Force the decoding (memoized for later calls), and return the value if successful.
Obtain the bytes without actually deserializing. Will serialize and memoize the result if the value is not the result of a lazy deserialization.
val apply_lazy :
fun_value:('a -> 'b) ->
fun_bytes:(Bytes.t -> 'b) ->
fun_combine:('b -> 'b -> 'b) ->
'a lazy_t ->
'b
Apply on structure of lazy value, and combine results
Create a Data_encoding.t
value which records knowledge of older versions of a given encoding as long as one can "upgrade" from an older version to the next (if upgrade is impossible one should consider that the encoding is completely different).
See the module Documented_example
in "./test/versioned.ml"
for a tutorial.