2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Type-safe serialization and deserialization of data structures. *)
|
|
|
|
|
|
|
|
(** {1 Data Encoding} *)
|
|
|
|
|
|
|
|
(** This module provides type-safe serialization and deserialization of
|
|
|
|
data structures. Backends are provided to both binary and JSON.
|
|
|
|
|
|
|
|
This works by writing type descriptors by hand, using the provided
|
|
|
|
combinators. These combinators can fine-tune the binary
|
|
|
|
representation to be compact and efficient, but also provide
|
|
|
|
proper field names and meta information, so the API of Tezos can
|
|
|
|
be automatically introspected and documented.
|
|
|
|
|
|
|
|
Here is an example encoding for type [(int * string)].
|
|
|
|
|
|
|
|
[let enc = obj2 (req "code" uint16) (req "message" string)]
|
|
|
|
|
|
|
|
In JSON, this encoding maps values of type [int * string] to JSON
|
|
|
|
objects with a field [code] whose value is a number and a field
|
|
|
|
[message] whose value is a string.
|
|
|
|
|
|
|
|
In binary, this encoding maps to two raw bytes for the [int]
|
|
|
|
followed by the size of the string in bytes, and finally the raw
|
|
|
|
contents of the string. This binary format is mostly tagless,
|
|
|
|
meaning that serialized data cannot be interpreted without the
|
|
|
|
encoding that was used for serialization.
|
|
|
|
|
|
|
|
Regarding binary serialization, encodings are classified as either:
|
|
|
|
- fixed size (booleans, integers, numbers)
|
|
|
|
data is always the same size for that type ;
|
|
|
|
- dynamically sized (arbitrary strings and bytes)
|
|
|
|
data is of unknown size and requires an explicit length field ;
|
|
|
|
- variable size (special case of strings, bytes, and arrays)
|
|
|
|
data makes up the remainder of an object of known size,
|
|
|
|
thus its size is given by the context, and does not
|
|
|
|
have to be serialized.
|
|
|
|
|
|
|
|
JSON operations are delegated to [ocplib-json-typed]. *)
|
|
|
|
|
|
|
|
(** The type for serializing or deserializing a value of type ['a]. *)
|
|
|
|
type 'a encoding = 'a t
|
|
|
|
and 'a t
|
|
|
|
|
|
|
|
(** In memory JSON data, compatible with [Ezjsonm]. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
type json =
|
|
|
|
[ `O of (string * json) list
|
|
|
|
| `Bool of bool
|
|
|
|
| `Float of float
|
|
|
|
| `A of json list
|
|
|
|
| `Null
|
|
|
|
| `String of string ]
|
2017-12-09 08:24:05 +04:00
|
|
|
type bson = Json_repr_bson.bson
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
type json_schema = Json_schema.schema
|
|
|
|
|
|
|
|
exception No_case_matched
|
|
|
|
exception Unexpected_tag of int
|
|
|
|
exception Duplicated_tag of int
|
2016-11-15 04:36:06 +04:00
|
|
|
exception Invalid_tag of int * [ `Uint8 | `Uint16 ]
|
2016-09-08 21:13:10 +04:00
|
|
|
exception Unexpected_enum of string * string list
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Special value [null] in JSON, nothing in binary. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val null : unit encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Empty object (not included in binary, encoded as empty object in JSON). *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val empty : unit encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Unit value, ommitted in binary.
|
|
|
|
Serialized as an empty object in JSON, accepts any object when deserializing. *)
|
2017-02-25 21:01:21 +04:00
|
|
|
val unit : unit encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Constant string (data is not included in the binary data). *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val constant : string -> unit encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Signed 8 bit integer
|
2017-11-13 19:34:00 +04:00
|
|
|
(data is encoded as a byte in binary and an integer in JSON). *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val int8 : int encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Unsigned 8 bit integer
|
2017-11-13 19:34:00 +04:00
|
|
|
(data is encoded as a byte in binary and an integer in JSON). *)
|
2016-11-15 04:36:06 +04:00
|
|
|
val uint8 : int encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Signed 16 bit integer
|
|
|
|
(data is encoded as a short in binary and an integer in JSON). *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val int16 : int encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Unsigned 16 bit integer
|
2017-11-13 19:34:00 +04:00
|
|
|
(data is encoded as a short in binary and an integer in JSON). *)
|
2016-11-15 04:36:06 +04:00
|
|
|
val uint16 : int encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** 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). *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val int31 : int encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Signed 32 bit integer
|
|
|
|
(data is encoded as a 32-bit int in binary and an integer in JSON). *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val int32 : int32 encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Signed 64 bit integer
|
2017-11-13 19:34:00 +04:00
|
|
|
(data is encodedas a 64-bit int in binary and a decimal string in JSON). *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val int64 : int64 encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
2018-04-24 10:26:00 +04:00
|
|
|
(** Integer with bounds in a given range. Both bounds are inclusive.
|
|
|
|
|
|
|
|
Raises [Invalid_argument] if the bounds are beyond the interval
|
|
|
|
[-2^30; 2^30-1]. These bounds are chosen to be compatible with all versions
|
|
|
|
of OCaml.
|
|
|
|
*)
|
2017-12-08 14:59:09 +04:00
|
|
|
val ranged_int : int -> int -> int encoding
|
|
|
|
|
|
|
|
(** Float with bounds in a given range. Both bounds are inclusive *)
|
|
|
|
val ranged_float : float -> float -> float encoding
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Encoding of a boolean
|
|
|
|
(data is encoded as a byte in binary and a boolean in JSON). *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val bool : bool encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Encoding of a string
|
|
|
|
- default variable in width
|
|
|
|
- encoded as a byte sequence in binary
|
|
|
|
- encoded as a string in JSON. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val string : string encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Encoding of arbitrary bytes
|
|
|
|
(encoded via hex in JSON and directly as a sequence byte in binary). *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val bytes : MBytes.t encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Encoding of floating point number
|
|
|
|
(encoded as a floating point number in JSON and a double in binary). *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val float : float encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** 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). *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val option : 'a encoding -> 'a option encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** 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)). *)
|
2017-03-06 21:25:00 +04:00
|
|
|
val result : 'a encoding -> 'b encoding -> ('a, 'b) result encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Encode enumeration via association list
|
|
|
|
(represented as a string in JSON and binary). *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val string_enum : (string * 'a) list -> 'a encoding
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
|
2017-12-04 15:10:20 +04:00
|
|
|
(** Is the given encoding serialized as a JSON object? *)
|
|
|
|
val is_obj : 'a encoding -> bool
|
|
|
|
|
|
|
|
(** Does the given encoding encode a tuple? *)
|
|
|
|
val is_tup : 'a encoding -> bool
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Create encodings that produce data of a fixed length when binary encoded.
|
|
|
|
See the preamble for an explanation. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
module Fixed : sig
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Encode a fixed length string *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val string : int -> string encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Encode a fixed length byte sequence *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val bytes : int -> MBytes.t encoding
|
|
|
|
end
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Create encodings that produce data of a variable length when binary encoded.
|
|
|
|
See the preamble for an explanation. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
module Variable : sig
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Encode a string *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val string : string encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Encode a byte sequence *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val bytes : MBytes.t encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Array encoding combinator *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val array : 'a encoding -> 'a array encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** List encoding combinator *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val list : 'a encoding -> 'a list encoding
|
|
|
|
end
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Mark an encoding as being of dynamic size.
|
|
|
|
Forces the size to be stored alongside content when needed.
|
|
|
|
Usually used to fix errors from combining two encodings. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val dynamic_size : 'a encoding -> 'a encoding
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** 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). *)
|
2017-10-27 20:41:59 +04:00
|
|
|
val delayed : (unit -> 'a encoding) -> 'a encoding
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Encodes raw JSON data (BSON is used for binary). *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val json : json encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Encodes a JSON schema (BSON encoded for binary). *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val json_schema : json_schema encoding
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** 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. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
type 'a field
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Required field. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val req :
|
|
|
|
?title:string -> ?description:string ->
|
|
|
|
string -> 't encoding -> 't field
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** 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 `1`) tells if the
|
|
|
|
field is present or not. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val opt :
|
|
|
|
?title:string -> ?description:string ->
|
|
|
|
string -> 't encoding -> 't option field
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Optional field of variable length.
|
|
|
|
Only one can be present in a given object. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val varopt :
|
|
|
|
?title:string -> ?description:string ->
|
|
|
|
string -> 't encoding -> 't option field
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** 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. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val dft :
|
|
|
|
?title:string -> ?description:string ->
|
|
|
|
string -> 't encoding -> 't -> 't field
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** {2 Constructors for objects with N fields} *)
|
|
|
|
|
|
|
|
(** 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. *)
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
val obj1 :
|
|
|
|
'f1 field -> 'f1 encoding
|
|
|
|
val obj2 :
|
|
|
|
'f1 field -> 'f2 field -> ('f1 * 'f2) encoding
|
|
|
|
val obj3 :
|
|
|
|
'f1 field -> 'f2 field -> 'f3 field -> ('f1 * 'f2 * 'f3) encoding
|
|
|
|
val obj4 :
|
|
|
|
'f1 field -> 'f2 field -> 'f3 field -> 'f4 field ->
|
|
|
|
('f1 * 'f2 * 'f3 * 'f4) encoding
|
|
|
|
val obj5 :
|
|
|
|
'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field ->
|
|
|
|
('f1 * 'f2 * 'f3 * 'f4 * 'f5) encoding
|
|
|
|
val obj6 :
|
|
|
|
'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field ->
|
|
|
|
'f6 field ->
|
|
|
|
('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6) encoding
|
|
|
|
val obj7 :
|
|
|
|
'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field ->
|
|
|
|
'f6 field -> 'f7 field ->
|
|
|
|
('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7) encoding
|
|
|
|
val obj8 :
|
|
|
|
'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field ->
|
|
|
|
'f6 field -> 'f7 field -> 'f8 field ->
|
|
|
|
('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7 * 'f8) encoding
|
|
|
|
val obj9 :
|
|
|
|
'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field ->
|
|
|
|
'f6 field -> 'f7 field -> 'f8 field -> 'f9 field ->
|
|
|
|
('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7 * 'f8 * 'f9) encoding
|
|
|
|
val obj10 :
|
|
|
|
'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field ->
|
|
|
|
'f6 field -> 'f7 field -> 'f8 field -> 'f9 field -> 'f10 field ->
|
|
|
|
('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7 * 'f8 * 'f9 * 'f10) encoding
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** {2 Constructors for tuples with N fields} *)
|
|
|
|
|
|
|
|
(** 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. *)
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
val tup1 :
|
|
|
|
'f1 encoding ->
|
|
|
|
'f1 encoding
|
|
|
|
val tup2 :
|
|
|
|
'f1 encoding -> 'f2 encoding ->
|
|
|
|
('f1 * 'f2) encoding
|
|
|
|
val tup3 :
|
|
|
|
'f1 encoding -> 'f2 encoding -> 'f3 encoding ->
|
|
|
|
('f1 * 'f2 * 'f3) encoding
|
|
|
|
val tup4 :
|
|
|
|
'f1 encoding -> 'f2 encoding -> 'f3 encoding -> 'f4 encoding ->
|
|
|
|
('f1 * 'f2 * 'f3 * 'f4) encoding
|
|
|
|
val tup5 :
|
|
|
|
'f1 encoding -> 'f2 encoding -> 'f3 encoding -> 'f4 encoding ->
|
|
|
|
'f5 encoding ->
|
|
|
|
('f1 * 'f2 * 'f3 * 'f4 * 'f5) encoding
|
|
|
|
val tup6 :
|
|
|
|
'f1 encoding -> 'f2 encoding -> 'f3 encoding -> 'f4 encoding ->
|
|
|
|
'f5 encoding -> 'f6 encoding ->
|
|
|
|
('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6) encoding
|
|
|
|
val tup7 :
|
|
|
|
'f1 encoding -> 'f2 encoding -> 'f3 encoding -> 'f4 encoding ->
|
|
|
|
'f5 encoding -> 'f6 encoding -> 'f7 encoding ->
|
|
|
|
('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7) encoding
|
|
|
|
val tup8 :
|
|
|
|
'f1 encoding -> 'f2 encoding -> 'f3 encoding -> 'f4 encoding ->
|
|
|
|
'f5 encoding -> 'f6 encoding -> 'f7 encoding -> 'f8 encoding ->
|
|
|
|
('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7 * 'f8) encoding
|
|
|
|
val tup9 :
|
|
|
|
'f1 encoding -> 'f2 encoding -> 'f3 encoding -> 'f4 encoding ->
|
|
|
|
'f5 encoding -> 'f6 encoding -> 'f7 encoding -> 'f8 encoding ->
|
|
|
|
'f9 encoding ->
|
|
|
|
('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7 * 'f8 * 'f9) encoding
|
|
|
|
val tup10 :
|
|
|
|
'f1 encoding -> 'f2 encoding -> 'f3 encoding -> 'f4 encoding ->
|
|
|
|
'f5 encoding -> 'f6 encoding -> 'f7 encoding -> 'f8 encoding ->
|
|
|
|
'f9 encoding -> 'f10 encoding ->
|
|
|
|
('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7 * 'f8 * 'f9 * 'f10) encoding
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** {2 Combinators} *)
|
|
|
|
|
|
|
|
(** Create a larger object from the encodings of two smaller ones.
|
|
|
|
@raise invalid_arg if both arguments are not objects. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val merge_objs : 'o1 encoding -> 'o2 encoding -> ('o1 * 'o2) encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Create a large tuple encoding from two smaller ones.
|
|
|
|
@raise invalid_arg if both values are not tuples. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val merge_tups : 'a1 encoding -> 'a2 encoding -> ('a1 * 'a2) encoding
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Array combinator. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val array : 'a encoding -> 'a array encoding
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** List combinator. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val list : 'a encoding -> 'a list encoding
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Association list.
|
|
|
|
An object in JSON, a list of pairs in binary. *)
|
2016-09-09 19:02:43 +04:00
|
|
|
val assoc : 'a encoding -> (string * 'a) list encoding
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** 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. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
type 't case
|
2017-08-28 22:20:06 +04:00
|
|
|
|
2017-11-24 20:19:38 +04:00
|
|
|
type case_tag = Tag of int | Json_only
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** 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.
|
|
|
|
|
|
|
|
If no tag is specified, tags are assigned by the union combinator. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val case :
|
2017-11-24 20:19:38 +04:00
|
|
|
case_tag ->
|
2017-02-24 20:17:53 +04:00
|
|
|
'a encoding -> ('t -> 'a option) -> ('a -> 't) -> 't case
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** 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.
|
|
|
|
|
|
|
|
This function will raise an exception if it is given the empty list
|
|
|
|
or if there are more cases than can fit in the tag size. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val union :
|
2016-11-15 04:36:06 +04:00
|
|
|
?tag_size:[ `Uint8 | `Uint16 ] -> 't case list -> 't encoding
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Add documentation to an encoding. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val describe :
|
|
|
|
?title:string -> ?description:string ->
|
|
|
|
't encoding ->'t encoding
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Give a name to an encoding. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val def : string -> 'a encoding -> 'a encoding
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Provide a transformer from one encoding to a different one.
|
|
|
|
|
|
|
|
Used to simplify nested encodings or to change the generic tuples
|
|
|
|
built by {obj1}, {tup1} and the like into proper records.
|
|
|
|
|
|
|
|
A schema may optionally be provided as documentation of the new encoding. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val conv :
|
|
|
|
('a -> 'b) -> ('b -> 'a) ->
|
|
|
|
?schema:Json_schema.schema ->
|
|
|
|
'b encoding -> 'a encoding
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Combinator for recursive encodings. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val mu : string -> ('a encoding -> 'a encoding) -> 'a encoding
|
|
|
|
|
|
|
|
module Json : sig
|
|
|
|
|
2017-12-09 08:24:05 +04:00
|
|
|
type t = json
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Create a {!Json_encoding.encoding} from an {encoding}. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val convert : 'a encoding -> 'a Json_encoding.encoding
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Generate a schema from an {!encoding}. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val schema : 'a encoding -> json_schema
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
|
|
|
|
(** Construct a JSON object from an encoding. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val construct : 't encoding -> 't -> json
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** Destruct a JSON object into a value.
|
|
|
|
Fail with an exception if the JSON object and encoding do not match.. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
val destruct : 't encoding -> json -> 't
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** JSON Error. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
type path = path_item list
|
2017-08-28 22:20:06 +04:00
|
|
|
|
|
|
|
(** A set of accessors that point to a location in a JSON object. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
and path_item =
|
|
|
|
[ `Field of string
|
|
|
|
(** A field in an object. *)
|
|
|
|
| `Index of int
|
|
|
|
(** An index in an array. *)
|
|
|
|
| `Star
|
|
|
|
(** Any / every field or index. *)
|
|
|
|
| `Next
|
|
|
|
(** The next element after an array. *) ]
|
|
|
|
|
|
|
|
(** Exception raised by destructors, with the location in the original
|
|
|
|
JSON structure and the specific error. *)
|
|
|
|
exception Cannot_destruct of (path * exn)
|
|
|
|
|
|
|
|
(** Unexpected kind of data encountered (w/ the expectation). *)
|
|
|
|
exception Unexpected of string * string
|
|
|
|
|
|
|
|
(** Some {!union} couldn't be destructed, w/ the reasons for each {!case}. *)
|
|
|
|
exception No_case_matched of exn list
|
|
|
|
|
|
|
|
(** Array of unexpected size encountered (w/ the expectation). *)
|
|
|
|
exception Bad_array_size of int * int
|
|
|
|
|
|
|
|
(** Missing field in an object. *)
|
|
|
|
exception Missing_field of string
|
|
|
|
|
|
|
|
(** Supernumerary field in an object. *)
|
|
|
|
exception Unexpected_field of string
|
|
|
|
|
|
|
|
val print_error :
|
|
|
|
?print_unknown: (Format.formatter -> exn -> unit) ->
|
|
|
|
Format.formatter -> exn -> unit
|
|
|
|
|
|
|
|
(** Helpers for writing encoders. *)
|
|
|
|
val cannot_destruct : ('a, Format.formatter, unit, 'b) format4 -> 'a
|
|
|
|
val wrap_error : ('a -> 'b) -> 'a -> 'b
|
|
|
|
|
2018-02-08 13:51:01 +04:00
|
|
|
(** Read a JSON document from a string. *)
|
|
|
|
val from_string : string -> (json, string) result
|
|
|
|
|
|
|
|
(** Read a stream of JSON documents from a stream of strings.
|
|
|
|
A single JSON document may be represented in multiple consecutive
|
|
|
|
strings. But only the first document of a string is considered. *)
|
|
|
|
val from_stream : string Lwt_stream.t -> (json, string) result Lwt_stream.t
|
|
|
|
|
|
|
|
(** Write a JSON document to a string. This goes via an intermediate
|
|
|
|
buffer and so may be slow on large documents. *)
|
|
|
|
val to_string : ?minify:bool -> json -> string
|
|
|
|
|
|
|
|
val pp : Format.formatter -> json -> unit
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
end
|
|
|
|
|
2017-12-09 08:24:05 +04:00
|
|
|
module Bson : sig
|
|
|
|
|
|
|
|
type t = Json_repr_bson.bson
|
|
|
|
|
2018-01-25 17:51:31 +04:00
|
|
|
(** Construct a BSON object from an encoding. *)
|
2017-12-09 08:24:05 +04:00
|
|
|
val construct : 't encoding -> 't -> bson
|
|
|
|
|
|
|
|
(** Destruct a BSON object into a value.
|
|
|
|
Fail with an exception if the JSON object and encoding do not match.. *)
|
|
|
|
val destruct : 't encoding -> bson -> 't
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-08-28 22:20:06 +04:00
|
|
|
(** Classify an encoding wrt. its binary serialization as explained in the preamble. *)
|
|
|
|
val classify : 'a encoding -> [ `Fixed of int | `Dynamic | `Variable ]
|
|
|
|
|
|
|
|
(** Define different encodings for JSON and binary serialization. *)
|
|
|
|
val splitted : json:'a encoding -> binary:'a encoding -> 'a encoding
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
module Binary : sig
|
|
|
|
|
|
|
|
val length : 'a encoding -> 'a -> int
|
|
|
|
val read : 'a encoding -> MBytes.t -> int -> int -> (int * 'a) option
|
|
|
|
val write : 'a encoding -> 'a -> MBytes.t -> int -> int option
|
|
|
|
val to_bytes : 'a encoding -> 'a -> MBytes.t
|
|
|
|
val of_bytes : 'a encoding -> MBytes.t -> 'a option
|
2017-04-19 21:21:23 +04:00
|
|
|
val of_bytes_exn : 'a encoding -> MBytes.t -> 'a
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-03-30 19:34:00 +04:00
|
|
|
(** [to_bytes_list ?copy_blocks blocks_size encod data] encode the
|
|
|
|
given data as a list of successive blocks of length
|
|
|
|
'blocks_size' at most.
|
|
|
|
|
|
|
|
NB. If 'copy_blocks' is false (default), the blocks of the list
|
|
|
|
can be garbage-collected only when all the blocks are
|
|
|
|
unreachable (because of the 'optimized' implementation of
|
|
|
|
MBytes.sub used internally *)
|
2017-08-28 22:20:06 +04:00
|
|
|
val to_bytes_list : ?copy_blocks:bool -> int -> 'a encoding -> 'a -> MBytes.t list
|
2017-03-30 19:34:00 +04:00
|
|
|
|
2017-04-13 20:05:41 +04:00
|
|
|
(** This type is used when decoding binary data incrementally.
|
|
|
|
- In case of 'Success', the decoded data, the size of used data
|
|
|
|
to decode the result, and the remaining data are returned
|
|
|
|
- In case of error, 'Error' is returned
|
|
|
|
- 'Await' status embeds a function that waits for additional data
|
|
|
|
to continue decoding, when given data are not sufficient *)
|
|
|
|
type 'a status =
|
|
|
|
| Success of { res : 'a ; res_len : int ; remaining : MBytes.t list }
|
|
|
|
| Await of (MBytes.t -> 'a status)
|
|
|
|
| Error
|
|
|
|
|
|
|
|
(** This function allows to decode (or to initialize decoding) a
|
|
|
|
stream of 'MByte.t'. The given data encoding should have a
|
|
|
|
'Fixed' or a 'Dynamic' size, otherwise an exception
|
|
|
|
'Invalid_argument "streaming data with variable size"' is
|
|
|
|
raised *)
|
2017-08-28 22:20:06 +04:00
|
|
|
val read_stream_of_bytes : ?init:MBytes.t list -> 'a encoding -> 'a status
|
2017-04-13 20:05:41 +04:00
|
|
|
|
|
|
|
(** Like read_stream_of_bytes, but only checks that the stream can
|
|
|
|
be read. Note that this is an approximation because failures
|
|
|
|
that may come from conversion functions present in encodings are
|
|
|
|
not checked *)
|
2017-08-28 22:20:06 +04:00
|
|
|
val check_stream_of_bytes : ?init:MBytes.t list -> 'a encoding -> unit status
|
2017-04-13 20:05:41 +04:00
|
|
|
|
2017-01-14 16:13:41 +04:00
|
|
|
val fixed_length : 'a encoding -> int option
|
|
|
|
val fixed_length_exn : 'a encoding -> int
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
end
|