Module Jsont_bytesrw

JSON codec.

According to RFC 8259.

See notes about layout preservation and behaviour on duplicate members.

Tip. For maximal performance decode with ~layout:false and ~locs:false, this is the default. Howver using ~locs:true improves some error reports.

Decode

val decode : ?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 Jsont.Meta.t values. Defaults to false.
  • If locs is true locations are preserved in Jsont.Meta.t values and error messages are precisely located. Defaults to false.
  • file is the file path from which r is assumed to read. Defaults to Jsont.Textloc.file_none
val decode' : ?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 : ?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' : ?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.

Encode

val encode : ?buf:Bytesrw.Bytes.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 a buffer of length Bytes.Writer.slice_length w.
  • format specifies how the JSON should be formatted. Defaults to Jsont.format.Minify.
  • number_format specifies the format string to format numbers. Defaults to Jsont.default_number_format.
  • eod indicates whether Bytesrw.Bytes.Slice.eod should be written on w.
val encode' : ?buf:Bytesrw.Bytes.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:Bytesrw.Bytes.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:Bytesrw.Bytes.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.

Recode

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 : ?layout:bool -> ?locs:bool -> ?file:Jsont.Textloc.fpath -> ?buf:Bytesrw.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' : ?layout:bool -> ?locs:bool -> ?file:Jsont.Textloc.fpath -> ?buf:Bytesrw.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 : ?layout:bool -> ?locs:bool -> ?file:Jsont.Textloc.fpath -> ?buf:Bytesrw.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' : ?layout:bool -> ?locs:bool -> ?file:Jsont.Textloc.fpath -> ?buf:Bytesrw.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.