Library
Module
Module type
Parameter
Class
Class type
The Yojson library provides runtime functions for reading and writing JSON data from OCaml. It addresses a few shortcomings of its predecessor json-wheel and is about twice as fast (2.7x reading, 1.3x writing; results may vary). The design goals of Yojson are the following:
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.
module Basic : sig ... end
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).
module Safe : sig ... end
This module supports a specific syntax for variants and tuples in addition to the standard JSON nodes. Arbitrary integers are supported and represented as a decimal string using `Intlit
when they cannot be represented using OCaml's int type.
module Raw : sig ... end
Ints, floats and strings literals are systematically preserved using `Intlit
, `Floatlit
and `Stringlit
. This module also supports the specific syntax for variants and tuples supported by Yojson.Safe
.
type json = [
| `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 * json) list
| `List of json list
| `Tuple of json list
| `Variant of string * json option
]
All possible cases defined in Yojson:
("abc", 123)
.<"Foo">
or <"Bar":123>
.type json_max = json
All possible cases defined in Yojson:
("abc", 123)
.<"Foo">
or <"Bar":123>
.JSON writers
val to_string : ?buf:Bi_outbuf.t -> ?len:int -> ?std:bool -> json -> string
Write a compact JSON value to a string.
val to_channel :
?buf:Bi_outbuf.t ->
?len:int ->
?std:bool ->
Pervasives.out_channel ->
json ->
unit
Write a compact JSON value to a channel.
val to_output :
?buf:Bi_outbuf.t ->
?len:int ->
?std:bool ->
< output : string -> int -> int -> int.. > ->
json ->
unit
Write a compact JSON value to an OO channel.
val to_file : ?len:int -> ?std:bool -> string -> json -> 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 -> json -> 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 ->
json 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 ->
Pervasives.out_channel ->
json 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.
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 -> json 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.
Sort object fields (stable sort, comparing field names and treating them as byte sequences)
val pretty_format : ?std:bool -> json -> Easy_format.t
Convert into a pretty-printable tree. See to_string
for the role of the optional std
argument.
val pretty_print : ?std:bool -> Format.formatter -> json -> unit
Pretty-print into a Format.formatter
. See to_string
for the role of the optional std
argument.
val pretty_to_string : ?std:bool -> json -> string
Pretty-print into a string. See to_string
for the role of the optional std
argument.
val pretty_to_channel : ?std:bool -> Pervasives.out_channel -> json -> unit
Pretty-print to a channel. See to_string
for the role of the optional std
argument.