package jsont
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha512=6206f73a66cb170b560a72e58f70b9fb2c20397b9ab819dceba49b6602b9b79e47ba307e6910e61ca4694555c66fdcd7a17490afb99548e8f43845a5a88913e7
doc/jsont.bytesrw/Jsont_bytesrw/index.html
Module Jsont_bytesrw
Source
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) result
decode t r
decodes a value from r
according to t
.
- If
layout
istrue
whitespace is preserved inJsont.Meta.t
values. Defaults tofalse
. - If
locs
istrue
locations are preserved inJsont.Meta.t
values and error messages are precisely located. Defaults tofalse
. file
is the file path from whichr
is assumed to read. Defaults toJsont.Textloc.file_none
val decode' :
?layout:bool ->
?locs:bool ->
?file:Jsont.Textloc.fpath ->
'a Jsont.t ->
Bytesrw.Bytes.Reader.t ->
('a, Jsont.Error.t) 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) 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) 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) 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 onw
. Defaults to a buffer of lengthBytes.Writer.slice_length
w
. format
specifies how the JSON should be formatted. Defaults toJsont.format.Minify
.number_format
specifies the format string to format numbers. Defaults toJsont.default_number_format
.eod
indicates whetherBytesrw.Bytes.Slice.eod
should be written onw
.
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) 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) 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) 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) result
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) 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) 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) 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:
- White space in empty arrays and objects is dropped.
- Unicode escapes are replaced by their UTF-8 encoding.
- The format of numbers is not preserved.
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.