Module Jsont_codec

JSON codec.

According to RFC 8259.

See notes about layout preservation and behaviour on duplicate members.


val decode : ?ctx:Jsont.Context.t -> ?layout:bool -> ?locs:bool -> ?file:Jsont.Textloc.fpath -> 'a Jsont.t -> Bytesrw.Bytes.Reader.t -> ('a, string) Stdlib.result

decode t r decodes a value from r according to t.

  • If layout is true whitespace is preserved in Jsonit.Json.Meta.t values. Defaults to false.
  • If locs is true locations are preserved in Jsonit.Json.Meta.t values. Defaults to false.
  • file is the file path from which r is assumed to read (defaults to Jsont.Textloc.file_none) used in the text locations if locs is true.
val decode' : ?ctx:Jsont.Context.t -> ?layout:bool -> ?locs:bool -> ?file:Jsont.Textloc.fpath -> 'a Jsont.t -> Bytesrw.Bytes.Reader.t -> ('a, Jsont.Error.t) Stdlib.result

decode' is like decode but preserves the error structure.

val decode_string : ?ctx:Jsont.Context.t -> ?layout:bool -> ?locs:bool -> ?file:Jsont.Textloc.fpath -> 'a Jsont.t -> string -> ('a, string) Stdlib.result

decode_string is like decode' but decodes directly from a string.

val decode_string' : ?ctx:Jsont.Context.t -> ?layout:bool -> ?locs:bool -> ?file:Jsont.Textloc.fpath -> 'a Jsont.t -> string -> ('a, Jsont.Error.t) Stdlib.result

decode_string' is like decode' but decodes directly from a string.


val encode : ?buf:Stdlib.Bytes.t -> ?ctx:Jsont.Context.t -> ?format:Jsont.format -> ?number_format:Jsont.number_format -> 'a Jsont.t -> 'a -> eod:bool -> Bytesrw.Bytes.Writer.t -> (unit, string) Stdlib.result

encode t v w encodes value v according to t on w.

  • If buf is specified it is used as a buffer for the slices written on w. Defaults to Bytes.Slice.io_buffer_size. Its size must be at least 4 or Invalid_argument is raised.
  • format specifies how the JSON should be formatted, defaults to Jsont.Json.Minify.
  • number_format specifies the format string to format numbers. Defaults to Jsont.default_number_string.
  • eod indicates whether Bytesrw.Bytes.Slice.eod should be written on w.
val encode' : ?buf:Stdlib.Bytes.t -> ?ctx:Jsont.Context.t -> ?format:Jsont.format -> ?number_format:Jsont.number_format -> 'a Jsont.t -> 'a -> eod:bool -> Bytesrw.Bytes.Writer.t -> (unit, Jsont.Error.t) Stdlib.result

encode' is like encode but preserves the error structure.

val encode_string : ?buf:Stdlib.Bytes.t -> ?ctx:Jsont.Context.t -> ?format:Jsont.format -> ?number_format:Jsont.number_format -> 'a Jsont.t -> 'a -> (string, string) Stdlib.result

encode_string is like encode but writes to a string.

val encode_string' : ?buf:Stdlib.Bytes.t -> ?ctx:Jsont.Context.t -> ?format:Jsont.format -> ?number_format:Jsont.number_format -> 'a Jsont.t -> 'a -> (string, Jsont.Error.t) Stdlib.result

encode_string' is like encode' but writes to a string.


The defaults in these functions are those of decode and encode, except if layout is true, format defaults to Jsont.Layout and vice-versa.

val recode : ?ctx:Jsont.Context.t -> ?layout:bool -> ?locs:bool -> ?file:Jsont.Textloc.fpath -> ?buf:Stdlib.Bytes.t -> ?format:Jsont.format -> ?number_format:Jsont.number_format -> 'a Jsont.t -> Bytesrw.Bytes.Reader.t -> Bytesrw.Bytes.Writer.t -> eod:bool -> (unit, string) Stdlib.result

recode is decode followed by recode.

val recode' : ?ctx:Jsont.Context.t -> ?layout:bool -> ?locs:bool -> ?file:Jsont.Textloc.fpath -> ?buf:Stdlib.Bytes.t -> ?format:Jsont.format -> ?number_format:Jsont.number_format -> 'a Jsont.t -> Bytesrw.Bytes.Reader.t -> Bytesrw.Bytes.Writer.t -> eod:bool -> (unit, Jsont.Error.t) Stdlib.result

recode' is like recode but preserves the error structure.

val recode_string : ?ctx:Jsont.Context.t -> ?layout:bool -> ?locs:bool -> ?file:Jsont.Textloc.fpath -> ?buf:Stdlib.Bytes.t -> ?format:Jsont.format -> ?number_format:Jsont.number_format -> 'a Jsont.t -> string -> (string, string) Stdlib.result

recode is decode_string followed by recode_string.

val recode_string' : ?ctx:Jsont.Context.t -> ?layout:bool -> ?locs:bool -> ?file:Jsont.Textloc.fpath -> ?buf:Stdlib.Bytes.t -> ?format:Jsont.format -> ?number_format:Jsont.number_format -> 'a Jsont.t -> string -> (string, Jsont.Error.t) Stdlib.result

recode_string' is like recode_string but preserves the error structure.

Layout preservation

In order to simplify the implementation not all layout is preserved. In particular:

Duplicate object members

Duplicate object members are undefined behaviour in JSON. We follow the behaviour of JSON.parse and the last one takes over, however duplicate members all have to parse with the specified type as we error as soon as possible. Also case members are not allowed to duplicate.