package yojson
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=656fc65f794186274f8b961dc38daba9e2de2fc993829291defbda2186812cc6
md5=b89d39ca3f8c532abe5f547ad3b8f84d
doc/yojson/Yojson/index.html
Module Yojson
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:
- Reducing inter-package dependencies by the use of polymorphic variants for the JSON tree type.
- Allowing type-aware serializers/deserializers to read and write directly without going through a generic JSON tree, for efficiency purposes. Readers and writers of all JSON syntaxic elements are provided but are undocumented and meant to be used by generated OCaml code.
- Distinguishing between ints and floats.
- Providing optional extensions of the JSON syntax. These extensions include comments, arbitrary strings, optional quotes around field names, tuples and variants.
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 ... endval init_lexer :
?buf:Bi_outbuf.t ->
?fname:string ->
?lnum:int ->
unit ->
lexer_stateCreate a fresh lexer_state record.
Basic JSON tree type
module Basic : sig ... endThis 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).
Multipurpose JSON tree type
module Safe : sig ... endThis 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.
JSON tree type with literal int/float/string leaves
module Raw : sig ... endInts, 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.
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 : Format.formatter -> t -> unitPretty printer, useful for debugging
val show : t -> stringConvert 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 = tJSON writers
val to_string : ?buf:Bi_outbuf.t -> ?len:int -> ?std:bool -> t -> stringWrite a compact JSON value to a string.
val to_channel :
?buf:Bi_outbuf.t ->
?len:int ->
?std:bool ->
out_channel ->
t ->
unitWrite a compact JSON value to a channel.
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 ->
unitWrite a compact JSON value to an OO channel.
See to_string for 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_string for 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_string for the role of the optional argument.
val stream_to_string :
?buf:Bi_outbuf.t ->
?len:int ->
?std:bool ->
t Stream.t ->
stringWrite 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 ->
out_channel ->
t Stream.t ->
unitWrite 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 -> t Stream.t -> unitWrite 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 -> unitWrite 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.tConvert into a pretty-printable tree. See to_string for the role of the optional std argument.
val pretty_print : ?std:bool -> Format.formatter -> t -> unitPretty-print into a Format.formatter. See to_string for the role of the optional std argument.
val pretty_to_string : ?std:bool -> t -> stringPretty-print into a string. See to_string for the role of the optional std argument.
val pretty_to_channel : ?std:bool -> out_channel -> t -> unitPretty-print to a channel. See to_string for the role of the optional std argument.