Module Yojson

The Yojson library provides several types for representing JSON values, with different use cases.

Each of these different types have their own module.

Shared types and functions

val version : string
exception Json_error of string
val json_error : string -> 'a
type lexer_state = {
  1. buf : Stdlib.Buffer.t;
  2. mutable lnum : int;
  3. mutable bol : int;
  4. mutable fname : string option;
}
module Lexer_state : sig ... end
val init_lexer : ?buf:Stdlib.Buffer.t -> ?fname:string -> ?lnum:int -> unit -> lexer_state
exception End_of_array
exception End_of_object
exception End_of_tuple
exception End_of_input
type t = [
  1. | `Assoc of (string * t) list
  2. | `Bool of bool
  3. | `Float of float
  4. | `Floatlit of string
  5. | `Int of int
  6. | `Intlit of string
  7. | `List of t list
  8. | `Null
  9. | `String of string
  10. | `Stringlit of string
  11. | `Tuple of t list
  12. | `Variant of string * t option
]
val pp : Stdlib.Format.formatter -> t -> unit
val show : t -> string
val equal : t -> t -> bool
val to_string : ?buf:Stdlib.Buffer.t -> ?len:int -> ?suf:string -> ?std:bool -> t -> string
val to_channel : ?buf:Stdlib.Buffer.t -> ?len:int -> ?suf:string -> ?std:bool -> Stdlib.out_channel -> t -> unit
val to_output : ?buf:Stdlib.Buffer.t -> ?len:int -> ?suf:string -> ?std:bool -> < output : string -> int -> int -> int.. > -> t -> unit
val to_file : ?len:int -> ?std:bool -> ?suf:string -> string -> t -> unit
val to_buffer : ?suf:string -> ?std:bool -> Stdlib.Buffer.t -> t -> unit
val seq_to_string : ?buf:Stdlib.Buffer.t -> ?len:int -> ?suf:string -> ?std:bool -> t Stdlib.Seq.t -> string
val seq_to_channel : ?buf:Stdlib.Buffer.t -> ?len:int -> ?suf:string -> ?std:bool -> Stdlib.out_channel -> t Stdlib.Seq.t -> unit
val seq_to_file : ?len:int -> ?suf:string -> ?std:bool -> string -> t Stdlib.Seq.t -> unit
val seq_to_buffer : ?suf:string -> ?std:bool -> Stdlib.Buffer.t -> t Stdlib.Seq.t -> unit
val write_t : Stdlib.Buffer.t -> t -> unit
val sort : t -> t
val write_null : Stdlib.Buffer.t -> unit -> unit
val write_bool : Stdlib.Buffer.t -> bool -> unit
val write_int : Stdlib.Buffer.t -> int -> unit
val write_float : Stdlib.Buffer.t -> float -> unit
val write_std_float : Stdlib.Buffer.t -> float -> unit
val write_float_prec : int -> Stdlib.Buffer.t -> float -> unit
val write_std_float_prec : int -> Stdlib.Buffer.t -> float -> unit
val write_string : Stdlib.Buffer.t -> string -> unit
val write_intlit : Stdlib.Buffer.t -> string -> unit
val write_floatlit : Stdlib.Buffer.t -> string -> unit
val write_stringlit : Stdlib.Buffer.t -> string -> unit
val write_assoc : Stdlib.Buffer.t -> (string * t) list -> unit
val write_list : Stdlib.Buffer.t -> t list -> unit
val write_tuple : Stdlib.Buffer.t -> t list -> unit
val write_std_tuple : Stdlib.Buffer.t -> t list -> unit
val write_variant : Stdlib.Buffer.t -> string -> t option -> unit
val write_std_variant : Stdlib.Buffer.t -> string -> t option -> unit
val write_json : Stdlib.Buffer.t -> t -> unit
val write_std_json : Stdlib.Buffer.t -> t -> unit
val pretty_print : ?std:bool -> Stdlib.Format.formatter -> t -> unit
val pretty_to_string : ?std:bool -> t -> string
val pretty_to_channel : ?std:bool -> Stdlib.out_channel -> t -> unit

Basic JSON tree type

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).

Multipurpose JSON tree type

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 (31 or 63 bits depending on the platform).

JSON tree type with literal int/float/string leaves

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.

Supertype of all JSON tree types