Legend:
Library
Module
Module type
Parameter
Class
Class type
This module supports standard JSON nodes only, i.e. no special syntax for variants or tuples as supported by Yojson.Safe. Arbitrary integers are not supported as they must all fit within the standard OCaml int type (31 or 63 bits depending on the platform).
The main advantage of this module is its simplicity.
`Int of int: JSON number without decimal point or exponent.
`Intlit of string: JSON number without decimal point or exponent, preserved as a string.
`Float of float: JSON number, Infinity, -Infinity or NaN.
`Floatlit of string: JSON number, Infinity, -Infinity or NaN, preserved as a string.
`String of string: JSON string. Bytes in the range 128-255 are preserved as-is without encoding validation for both reading and writing.
`Stringlit of string: JSON string literal including the double quotes.
`Assoc of (string * json) list: JSON object.
`List of json list: JSON array.
`Tuple of json list: Tuple (non-standard extension of JSON). Syntax: ("abc", 123).
`Variant of (string * json option): Variant (non-standard extension of JSON). Syntax: <"Foo"> or <"Bar":123>.
JSON writers
val to_string :
?buf:Stdlib.Buffer.t ->?len:int ->?suf:string ->?std:bool ->t->
string
Write a compact JSON value to a string.
parameterbuf
allows to reuse an existing buffer created with Buffer.create. The buffer is cleared of all contents before starting and right before returning.
parameterlen
initial length of the output buffer.
parametersuf
appended to the output as a suffix, defaults to empty string.
parameterstd
use only standard JSON syntax, i.e. convert tuples and variants into standard JSON (if applicable), refuse to print NaN and infinities, require the root node to be either an object or an array. Default is false.
equal a b is the monomorphic equality. Determines whether two JSON values are considered equal. In the case of JSON objects, the order of the keys does not matter, except for duplicate keys which will be considered equal as long as they are in the same input order.
JSON pretty-printing
val pretty_print : ?std:bool ->Stdlib.Format.formatter ->t-> unit
Pretty-print into a Format.formatter. See to_string for the role of the optional std argument.
This alias is provided for backward compatibility. New code should refer to Yojson.lexer_state directly.
val init_lexer :
?buf:Stdlib.Buffer.t ->?fname:string ->?lnum:int ->unit ->lexer_state
This alias is provided for backward compatibility. New code should use Yojson.init_lexer directly.
val from_lexbuf : lexer_state->?stream:bool ->Stdlib.Lexing.lexbuf ->t
Read a JSON value from a lexbuf. A valid initial lexer_state can be created with init_lexer. See from_string for the meaning of the optional arguments and raised exceptions.
parameterstream
indicates whether more data may follow. The default value is false and indicates that only JSON whitespace can be found between the end of the JSON value and the end of the input.
val seq_from_string :
?buf:Stdlib.Buffer.t ->?fname:string ->?lnum:int ->string ->tStdlib.Seq.t
Input a sequence of JSON values from a string. Whitespace between JSON values is fine but not required. See from_string for the meaning of the optional arguments and raised exceptions.
val seq_from_channel :
?buf:Stdlib.Buffer.t ->?fin:(unit -> unit)->?fname:string ->?lnum:int ->Stdlib.in_channel ->tStdlib.Seq.t
Input a sequence of JSON values from a channel. Whitespace between JSON values is fine but not required.
parameterfin
finalization function executed once when the end of the sequence is reached either because there is no more input or because the input could not be parsed, raising an exception.
When the parsing and the finalizer both raised, Finally (exn, fin_exn) is raised, exn being the parsing exception and fin_exn the finalizer one.
See from_string for the meaning of the other optional arguments and other raised exceptions.
val seq_from_file :
?buf:Stdlib.Buffer.t ->?fname:string ->?lnum:int ->string ->tStdlib.Seq.t
Input a sequence of JSON values from a file. Whitespace between JSON values is fine but not required.
See from_string for the meaning of the optional arguments and raised exceptions.
val seq_from_lexbuf :
lexer_state->?fin:(unit -> unit)->Stdlib.Lexing.lexbuf ->tStdlib.Seq.t
Input a sequence of JSON values from a lexbuf. A valid initial lexer_state can be created with init_lexer. Whitespace between JSON values is fine but not required.
The type of values resulting from a parsing attempt of a JSON value.
val lineseq_from_channel :
?buf:Stdlib.Buffer.t ->?fin:(unit -> unit)->?fname:string ->?lnum:int ->Stdlib.in_channel ->json_lineStdlib.Seq.t
Input a sequence of JSON values, one per line, from a channel. Exceptions raised when reading malformed lines are caught and represented using `Exn.
See seq_from_channel for the meaning of the optional fin argument. See from_string for the meaning of the other optional arguments and raised exceptions.
val lineseq_from_file :
?buf:Stdlib.Buffer.t ->?fname:string ->?lnum:int ->string ->json_lineStdlib.Seq.t
Input a sequence of JSON values, one per line, from a file. Exceptions raised when reading malformed lines are caught and represented using `Exn.
See seq_from_channel for the meaning of the optional fin argument. See from_string for the meaning of the other optional arguments and raised exceptions.
val read_t : lexer_state->Stdlib.Lexing.lexbuf ->t
Read a JSON value from the given lexer_state and lexing buffer and return it. Provided as a reader function for atdgen.