Module Json

module Json: sig .. end

Json Library

Remarks:


type t = 
| Null
| True
| False
| String of string
| Number of string
| Int of int
| Float of float
| Array of t list
| Assoc of (string * t) list

Json Objects

val equal : t -> t -> bool

Pervasives

val compare : t -> t -> int

Pervasives

val pp : Format.formatter -> t -> unit
val pp_dump : Format.formatter -> t -> unit

without formatting

exception Error of string * int * string

file, line, message

Constructors

val of_bool : bool -> t
val of_int : int -> t
val of_string : string -> t
val of_float : float -> t
val of_list : t list -> t
val of_array : t array -> t
val of_fields : (string * t) list -> t

Parsers

Parsing raise Error in case of error.

val load_lexbuf : Lexing.lexbuf -> t

Consumes the entire buffer.

val load_channel : ?file:string -> Pervasives.in_channel -> t

Parses the stream until EOF.

val load_string : string -> t

Parses the Json in the string.

val load_file : string -> t

May also raise system exception.

Printers

Printers use formatting unless ~pretty:false.

val save_string : ?pretty:bool -> t -> string
val save_buffer : ?pretty:bool -> Buffer.t -> t -> unit
val save_channel : ?pretty:bool -> Pervasives.out_channel -> t -> unit
val save_formatter : ?pretty:bool -> Format.formatter -> t -> unit
val save_file : ?pretty:bool -> string -> t -> unit

Accessors

Accessors raise exception Invalid_argument in case of wrong format.

val bool : t -> bool

Extract True and False only.

val int : t -> int

Convert Null, Int, Float, Number and String to an int. Floats are truncated with int_of_float and Null to 0.

val string : t -> string

Convert Null, Int, Float, Number and String to a string. Floats are truncated with string_of_float and Null to "".

val float : t -> float

Convert Null, Int, Float, Number and String to float and Null to 0.0.

val array : t -> t array

Extract the array of an Array object. Null is considered an empty array.

val list : t -> t list

Extract the list of an Array object. Null is considered an empty list.

val fold : (string -> t -> 'a -> 'a) -> t -> 'a -> 'a

Fold over all fields of the object. Null is considered an empty object. Typical usage is fold M.add t M.empty where M=Map.Make(String).

val field : string -> t -> t

Lookup a field in an object. Null is considered an empty object.