Module Yojson.Raw
Type of the JSON tree
type t=[|`Null|`Bool of bool|`Intlit of string|`Floatlit of string|`Stringlit of string|`Assoc of (string * t) list|`List of t list|`Tuple of t list|`Variant of string * t option]All possible cases defined in Yojson:
- `Null: JSON null
- `Bool of bool: JSON boolean
- `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>.
type json= t* Compatibility type alias for type `t`
val pp : Stdlib.Format.formatter -> t -> unitPretty printer, useful for debugging
val show : t -> stringConvert value to string, useful for debugging
JSON writers
val to_string : ?buf:Bi_outbuf.t -> ?len:int -> ?std:bool -> t -> stringWrite a compact JSON value to a string.
- parameter buf
allows to reuse an existing buffer created with
Bi_outbuf.create. The buffer is cleared of all contents before starting and right before returning.
- parameter len
initial length of the output buffer.
- parameter std
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.
val to_channel : ?buf:Bi_outbuf.t -> ?len:int -> ?std:bool -> Stdlib.out_channel -> t -> unitWrite a compact JSON value to a channel.
- parameter buf
allows to reuse an existing buffer created with
Bi_outbuf.create_channel_writeron the same channel.bufis flushed right beforeto_channelreturns but theout_channelis not flushed automatically.See
to_stringfor the role of the other optional arguments.
val to_output : ?buf:Bi_outbuf.t -> ?len:int -> ?std:bool -> < output : string -> int -> int -> int; .. > -> t -> unitWrite a compact JSON value to an OO channel.
- parameter buf
allows to reuse an existing buffer created with
Bi_outbuf.create_output_writeron the same channel.bufis flushed right beforeto_outputreturns but the channel itself is not flushed automatically.See
to_stringfor the role of the other optional arguments.
val to_file : ?len:int -> ?std:bool -> string -> t -> unitWrite a compact JSON value to a file. See
to_stringfor the role of the optional arguments.
val to_outbuf : ?std:bool -> Bi_outbuf.t -> t -> unitWrite a compact JSON value to an existing buffer. See
to_stringfor the role of the optional argument.
val stream_to_string : ?buf:Bi_outbuf.t -> ?len:int -> ?std:bool -> t Stdlib.Stream.t -> stringWrite a newline-separated sequence of compact one-line JSON values to a string. See
to_stringfor the role of the optional arguments.
val stream_to_channel : ?buf:Bi_outbuf.t -> ?len:int -> ?std:bool -> Stdlib.out_channel -> t Stdlib.Stream.t -> unitWrite a newline-separated sequence of compact one-line JSON values to a channel. See
to_channelfor the role of the optional arguments.
val stream_to_file : ?len:int -> ?std:bool -> string -> t Stdlib.Stream.t -> unitWrite a newline-separated sequence of compact one-line JSON values to a file. See
to_stringfor the role of the optional arguments.
val stream_to_outbuf : ?std:bool -> Bi_outbuf.t -> t Stdlib.Stream.t -> unitWrite a newline-separated sequence of compact one-line JSON values to an existing buffer. See
to_stringfor the role of the optional arguments.
val write_t : Bi_outbuf.t -> t -> unitWrite the given JSON value to the given buffer. Provided as a writer function for atdgen.
Miscellaneous
JSON pretty-printing
val pretty_format : ?std:bool -> t -> Easy_format.tConvert into a pretty-printable tree. See
to_stringfor the role of the optionalstdargument.- see http://martin.jambon.free.fr/easy-format.html
Easy-format
val pretty_print : ?std:bool -> Stdlib.Format.formatter -> t -> unitPretty-print into a
Format.formatter. Seeto_stringfor the role of the optionalstdargument.- since
- 1.3.1
val pretty_to_string : ?std:bool -> t -> stringPretty-print into a string. See
to_stringfor the role of the optionalstdargument.
val pretty_to_channel : ?std:bool -> Stdlib.out_channel -> t -> unitPretty-print to a channel. See
to_stringfor the role of the optionalstdargument.
JSON readers
val from_string : ?buf:Bi_outbuf.t -> ?fname:string -> ?lnum:int -> string -> tRead a JSON value from a string.
- parameter buf
use this buffer at will during parsing instead of creating a new one.
- parameter fname
data file name to be used in error messages. It does not have to be a real file.
- parameter lnum
number of the first line of input. Default is 1.
val from_channel : ?buf:Bi_outbuf.t -> ?fname:string -> ?lnum:int -> Stdlib.in_channel -> tRead a JSON value from a channel. See
from_stringfor the meaning of the optional arguments.
val from_file : ?buf:Bi_outbuf.t -> ?fname:string -> ?lnum:int -> string -> tRead a JSON value from a file. See
from_stringfor the meaning of the optional arguments.
type lexer_state= Lexer_state.t={buf : Bi_outbuf.t;mutable lnum : int;mutable bol : int;mutable fname : string option;}This alias is provided for backward compatibility. New code should refer to
Yojson.lexer_statedirectly.
val init_lexer : ?buf:Bi_outbuf.t -> ?fname:string -> ?lnum:int -> unit -> lexer_stateThis alias is provided for backward compatibility. New code should use
Yojson.init_lexerdirectly.
val from_lexbuf : lexer_state -> ?stream:bool -> Stdlib.Lexing.lexbuf -> tRead a JSON value from a lexbuf. A valid initial
lexer_statecan be created withinit_lexer. Seefrom_stringfor the meaning of the optional arguments.- parameter stream
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 stream_from_string : ?buf:Bi_outbuf.t -> ?fname:string -> ?lnum:int -> string -> t Stdlib.Stream.tInput a sequence of JSON values from a string. Whitespace between JSON values is fine but not required. See
from_stringfor the meaning of the optional arguments.
val stream_from_channel : ?buf:Bi_outbuf.t -> ?fin:(unit -> unit) -> ?fname:string -> ?lnum:int -> Stdlib.in_channel -> t Stdlib.Stream.tInput a sequence of JSON values from a channel. Whitespace between JSON values is fine but not required.
- parameter fin
finalization function executed once when the end of the stream is reached either because there is no more input or because the input could not be parsed, raising an exception.
- raises Finally
When the parsing and the finalizer both raised,
Finally (exn, fin_exn)is raised,exnbeing the parsing exception andfin_exnthe finalizer one.See
from_stringfor the meaning of the other optional arguments.
val stream_from_file : ?buf:Bi_outbuf.t -> ?fname:string -> ?lnum:int -> string -> t Stdlib.Stream.tInput a sequence of JSON values from a file. Whitespace between JSON values is fine but not required.
See
from_stringfor the meaning of the optional arguments.
val stream_from_lexbuf : lexer_state -> ?fin:(unit -> unit) -> Stdlib.Lexing.lexbuf -> t Stdlib.Stream.tInput a sequence of JSON values from a lexbuf. A valid initial
lexer_statecan be created withinit_lexer. Whitespace between JSON values is fine but not required.- raises Finally
When the parsing and the finalizer both raised,
Finally (exn, fin_exn)is raised,exnbeing the parsing exception andfin_exnthe finalizer one.See
stream_from_channelfor the meaning of the optionalfinargument.
type json_line=[|`Json of t|`Exn of exn]The type of values resulting from a parsing attempt of a JSON value.
val linestream_from_channel : ?buf:Bi_outbuf.t -> ?fin:(unit -> unit) -> ?fname:string -> ?lnum:int -> Stdlib.in_channel -> json_line Stdlib.Stream.tInput a sequence of JSON values, one per line, from a channel. Exceptions raised when reading malformed lines are caught and represented using
`Exn.See
stream_from_channelfor the meaning of the optionalfinargument. Seefrom_stringfor the meaning of the other optional arguments.
val linestream_from_file : ?buf:Bi_outbuf.t -> ?fname:string -> ?lnum:int -> string -> json_line Stdlib.Stream.tInput a sequence of JSON values, one per line, from a file. Exceptions raised when reading malformed lines are caught and represented using
`Exn.See
stream_from_channelfor the meaning of the optionalfinargument. Seefrom_stringfor the meaning of the other optional arguments.
val read_t : lexer_state -> Stdlib.Lexing.lexbuf -> tRead a JSON value from the given lexer_state and lexing buffer and return it. Provided as a reader function for atdgen.