Shared types and functions
type lexer_state = {
buf : Bi_outbuf.t; | Buffer used to accumulate substrings |
mutable lnum : int; | Current line number (counting from 1) |
mutable bol : int; | Absolute position of the first character of the current line (counting from 0) |
mutable fname : string option; | Name referencing the input file in error messages |
}
module Lexer_state : sig ... end
val init_lexer : ?buf:Bi_outbuf.t -> ?fname:string -> ?lnum:int -> unit -> lexer_state
Create a fresh lexer_state record.
Basic JSON tree type
module Basic : sig ... end
Multipurpose JSON tree type
module Safe : sig ... end
JSON tree type with literal int/float/string leaves
module Raw : sig ... end
Supertype of all JSON tree types
Type of the JSON tree
type t = [
| `Null |
| `Bool of bool |
| `Int of int |
| `Intlit of string |
| `Float of float |
| `Floatlit of string |
| `String 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 -> unit
Pretty printer, useful for debugging
val show : t -> string
Convert value to string, useful for debugging
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.
type json_max = t
JSON writers
val to_string : ?buf:Bi_outbuf.t -> ?len:int -> ?std:bool -> t -> string
Write 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 -> unit
Write a compact JSON value to a channel.
- parameter buf
allows to reuse an existing buffer created with
Bi_outbuf.create_channel_writer
on the same channel.buf
is flushed right beforeto_channel
returns but theout_channel
is not flushed automatically.See
to_string
for the role of the other optional arguments.
val to_output : ?buf:Bi_outbuf.t -> ?len:int -> ?std:bool -> < output : string -> int -> int -> int; .. > -> t -> unit
Write a compact JSON value to an OO channel.
- parameter buf
allows to reuse an existing buffer created with
Bi_outbuf.create_output_writer
on the same channel.buf
is flushed right beforeto_output
returns but the channel itself is not flushed automatically.See
to_string
for the role of the other optional arguments.
val to_file : ?len:int -> ?std:bool -> string -> t -> unit
Write a compact JSON value to a file. See to_string
for the role of the optional arguments.
val to_outbuf : ?std:bool -> Bi_outbuf.t -> t -> unit
Write a compact JSON value to an existing buffer. See to_string
for the role of the optional argument.
val stream_to_string : ?buf:Bi_outbuf.t -> ?len:int -> ?std:bool -> t Stdlib.Stream.t -> string
Write a newline-separated sequence of compact one-line JSON values to a string. See to_string
for 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 -> unit
Write a newline-separated sequence of compact one-line JSON values to a channel. See to_channel
for the role of the optional arguments.
val stream_to_file : ?len:int -> ?std:bool -> string -> t Stdlib.Stream.t -> unit
Write a newline-separated sequence of compact one-line JSON values to a file. See to_string
for the role of the optional arguments.
val stream_to_outbuf : ?std:bool -> Bi_outbuf.t -> t Stdlib.Stream.t -> unit
Write a newline-separated sequence of compact one-line JSON values to an existing buffer. See to_string
for the role of the optional arguments.
val write_t : Bi_outbuf.t -> t -> unit
Write the given JSON value to the given buffer. Provided as a writer function for atdgen.
Miscellaneous
Sort object fields (stable sort, comparing field names and treating them as byte sequences)
JSON pretty-printing
val pretty_format : ?std:bool -> t -> Easy_format.t
Convert into a pretty-printable tree. See to_string
for the role of the optional std
argument.
- see http://martin.jambon.free.fr/easy-format.html
Easy-format
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.
- since
- 1.3.1
val pretty_to_string : ?std:bool -> t -> string
Pretty-print into a string. See to_string
for the role of the optional std
argument.
val pretty_to_channel : ?std:bool -> Stdlib.out_channel -> t -> unit
Pretty-print to a channel. See to_string
for the role of the optional std
argument.