package openai

  1. Overview
  2. Docs
include module type of struct include Yojson.Safe end

Type of the JSON tree

type t = [
  1. | `Null
  2. | `Bool of bool
  3. | `Int of int
  4. | `Intlit of string
  5. | `Float of float
  6. | `String of string
  7. | `Assoc of (string * t) list
  8. | `List of t list
  9. | `Tuple of t list
  10. | `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>.
val pp : Format.formatter -> t -> unit

Pretty printer, useful for debugging

val show : t -> string

Convert value to string, useful for debugging

val equal : t -> t -> bool

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.

val to_basic : t -> Yojson.Basic.t

Tuples are converted to JSON arrays, Variants are converted to JSON strings or arrays of a string (constructor) and a json value (argument). Long integers are converted to JSON strings.

Examples:

`Tuple [ `Int 1; `Float 2.3 ]   ->    `List [ `Int 1; `Float 2.3 ]
`Variant ("A", None)            ->    `String "A"
`Variant ("B", Some x)          ->    `List [ `String "B", x ]
`Intlit "12345678901234567890"  ->    `String "12345678901234567890"

JSON writers

val to_channel : ?buf:Buffer.t -> ?len:int -> ?suf:string -> ?std:bool -> out_channel -> t -> unit

Write a compact JSON value to a channel. Note: the out_channel is not flushed by this function.

See to_string for the role of the optional arguments and raised exceptions.

val to_output : ?buf:Buffer.t -> ?len:int -> ?suf:string -> ?std:bool -> < output : string -> int -> int -> int.. > -> t -> unit

Write a compact JSON value to an OO channel.

See to_string for the role of the optional arguments and raised exceptions.

val to_file : ?len:int -> ?std:bool -> ?suf:string -> string -> t -> unit

Write a compact JSON value to a file. See to_string for the role of the optional arguments and raised exceptions.

  • parameter suf

    is a suffix appended to the output Newline by default for POSIX compliance.

val to_buffer : ?suf:string -> ?std:bool -> Buffer.t -> t -> unit

Write a compact JSON value to an existing buffer. See to_string for the role of the optional argument and raised exceptions.

val seq_to_string : ?buf:Buffer.t -> ?len:int -> ?suf:string -> ?std:bool -> t Seq.t -> string

Write a sequence of suf-suffixed compact one-line JSON values to a string.

  • parameter suf

    is the suffix ouf each value written. Newline by default. See to_string for the role of the optional arguments and raised exceptions.

val seq_to_channel : ?buf:Buffer.t -> ?len:int -> ?suf:string -> ?std:bool -> out_channel -> t Seq.t -> unit

Write a sequence of suf-suffixed compact one-line JSON values to a channel.

  • parameter suf

    is the suffix of each value written. Newline by default. See to_channel for the role of the optional arguments and raised exceptions.

val seq_to_file : ?len:int -> ?suf:string -> ?std:bool -> string -> t Seq.t -> unit

Write a sequence of suf-suffixed compact one-line JSON values to a file.

  • parameter suf

    is the suffix of each value written. Newline by default. See to_string for the role of the optional arguments and raised exceptions.

val seq_to_buffer : ?suf:string -> ?std:bool -> Buffer.t -> t Seq.t -> unit

Write a sequence of suf-suffixed compact one-line JSON values to an existing buffer.

  • parameter suf

    is the suffix of each value written. Newline by default. See to_string for the role of the optional arguments and raised exceptions.

val write_t : Buffer.t -> t -> unit

Write the given JSON value to the given buffer. Provided as a writer function for atdgen.

Miscellaneous

val sort : t -> t

Sort object fields (stable sort, comparing field names and treating them as byte sequences)

JSON pretty-printing

val pretty_print : ?std:bool -> Format.formatter -> t -> unit

Pretty-print into a Format.formatter. See to_string for the role of the optional std argument.

  • raises Json_error

    if float value is not allowed in standard JSON.

  • 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. See pretty_print for raised exceptions.

val pretty_to_channel : ?std:bool -> out_channel -> t -> unit

Pretty-print to a channel. See to_string for the role of the optional std argument. See pretty_print for raised exceptions.

val prettify : ?std:bool -> string -> string

Combined parser and pretty-printer. See to_string for the role of the optional std argument and raised exceptions.

val compact : ?std:bool -> string -> string

Combined parser and printer. See to_string for the role of the optional std argument and raised exceptions.

JSON readers

exception Finally of exn * exn

Exception describing a failure in both finalizer and parsing.

val from_string : ?buf:Buffer.t -> ?fname:string -> ?lnum:int -> string -> t

Read a JSON value from a string.

  • parameter buf

    use this buffer at will during parsing instead of creating a new one.

  • parameter fname

    data file name to be used in error messages. It does not have to be a real file.

  • parameter lnum

    number of the first line of input. Default is 1.

  • raises Json_error

    if parsing fails.

val from_channel : ?buf:Buffer.t -> ?fname:string -> ?lnum:int -> in_channel -> t

Read a JSON value from a channel. See from_string for the meaning of the optional arguments and raised exceptions.

val from_file : ?buf:Buffer.t -> ?fname:string -> ?lnum:int -> string -> t

Read a JSON value from a file. See from_string for the meaning of the optional arguments and raised exceptions.

type lexer_state = {
  1. buf : Buffer.t;
  2. mutable lnum : int;
  3. mutable bol : int;
  4. mutable fname : string option;
}

This alias is provided for backward compatibility. New code should refer to Yojson.lexer_state directly.

val init_lexer : ?buf:Buffer.t -> ?fname:string -> ?lnum:int -> unit -> lexer_state

This alias is provided for backward compatibility. New code should use Yojson.init_lexer directly.

val from_lexbuf : lexer_state -> ?stream:bool -> Lexing.lexbuf -> t

Read a JSON value from a lexbuf. A valid initial lexer_state can be created with init_lexer. See from_string for the meaning of the optional arguments and raised exceptions.

  • parameter stream

    indicates whether more data may follow. The default value is false and indicates that only JSON whitespace can be found between the end of the JSON value and the end of the input.

val seq_from_string : ?buf:Buffer.t -> ?fname:string -> ?lnum:int -> string -> t Seq.t

Input a sequence of JSON values from a string. Whitespace between JSON values is fine but not required. See from_string for the meaning of the optional arguments and raised exceptions.

val seq_from_channel : ?buf:Buffer.t -> ?fin:(unit -> unit) -> ?fname:string -> ?lnum:int -> in_channel -> t Seq.t

Input a sequence of JSON values from a channel. Whitespace between JSON values is fine but not required.

  • parameter fin

    finalization function executed once when the end of the sequence is reached either because there is no more input or because the input could not be parsed, raising an exception.

  • raises Finally

    When the parsing and the finalizer both raised, Finally (exn, fin_exn) is raised, exn being the parsing exception and fin_exn the finalizer one.

    See from_string for the meaning of the other optional arguments and other raised exceptions.

val seq_from_file : ?buf:Buffer.t -> ?fname:string -> ?lnum:int -> string -> t Seq.t

Input a sequence of JSON values from a file. Whitespace between JSON values is fine but not required.

See from_string for the meaning of the optional arguments and raised exceptions.

val seq_from_lexbuf : lexer_state -> ?fin:(unit -> unit) -> Lexing.lexbuf -> t Seq.t

Input a sequence of JSON values from a lexbuf. A valid initial lexer_state can be created with init_lexer. Whitespace between JSON values is fine but not required.

  • raises Finally

    When the parsing and the finalizer both raised, Finally (exn, fin_exn) is raised, exn being the parsing exception and fin_exn the finalizer one.

    See seq_from_channel for the meaning of the optional fin argument and other raised exceptions.

type json_line = [
  1. | `Json of t
  2. | `Exn of exn
]

The type of values resulting from a parsing attempt of a JSON value.

val lineseq_from_channel : ?buf:Buffer.t -> ?fin:(unit -> unit) -> ?fname:string -> ?lnum:int -> in_channel -> json_line Seq.t

Input a sequence of JSON values, one per line, from a channel. Exceptions raised when reading malformed lines are caught and represented using `Exn.

See seq_from_channel for the meaning of the optional fin argument. See from_string for the meaning of the other optional arguments and raised exceptions.

val lineseq_from_file : ?buf:Buffer.t -> ?fname:string -> ?lnum:int -> string -> json_line Seq.t

Input a sequence of JSON values, one per line, from a file. Exceptions raised when reading malformed lines are caught and represented using `Exn.

See seq_from_channel for the meaning of the optional fin argument. See from_string for the meaning of the other optional arguments and raised exceptions.

val read_t : lexer_state -> Lexing.lexbuf -> t

Read a JSON value from the given lexer_state and lexing buffer and return it. Provided as a reader function for atdgen.

module Util = Yojson.Safe.Util

This module provides combinators for extracting fields from JSON values.

include module type of struct include Util end

This module provides combinators for extracting fields from JSON values.

exception Type_error of string * Yojson__Safe.t

Raised when the JSON value is not of the correct type to support an operation, e.g. member on an `Int. The string message explains the mismatch.

exception Undefined of string * Yojson__Safe.t

Raised when the equivalent JavaScript operation on the JSON value would return undefined. Currently this only happens when an array index is out of bounds.

val keys : Yojson__Safe.t -> string list

Returns all the key names in the given JSON object.

  • raises Type_error

    if argument is not a JSON object.

val values : Yojson__Safe.t -> Yojson__Safe.t list

Return all the value in the given JSON object.

  • raises Type_error

    if argument is not a JSON object.

val combine : Yojson__Safe.t -> Yojson__Safe.t -> Yojson__Safe.t

Combine two JSON objects together.

val member : string -> Yojson__Safe.t -> Yojson__Safe.t

member k obj returns the value associated with the key k in the JSON object obj, or `Null if k is not present in obj.

val path : string list -> Yojson__Safe.t -> Yojson__Safe.t option
val index : int -> Yojson__Safe.t -> Yojson__Safe.t

index i arr returns the value at index i in the JSON array arr. Negative indices count from the end of the list (so -1 is the last element).

val map : (Yojson__Safe.t -> Yojson__Safe.t) -> Yojson__Safe.t -> Yojson__Safe.t

map f arr calls the function f on each element of the JSON array arr, and returns a JSON array containing the results.

val to_assoc : Yojson__Safe.t -> (string * Yojson__Safe.t) list

Extract the items of a JSON object.

  • raises Type_error

    if argument is not a JSON object.

val to_option : (Yojson__Safe.t -> 'a) -> Yojson__Safe.t -> 'a option

Return None if the JSON value is null or map the JSON value to Some value using the provided function.

val to_bool : Yojson__Safe.t -> bool

Extract a boolean value.

  • raises Type_error

    if argument is not a JSON boolean.

val to_bool_option : Yojson__Safe.t -> bool option

Extract Some boolean value, return None if the value is null.

val to_number : Yojson__Safe.t -> float

Extract a number.

  • raises Type_error

    if argument is not a JSON number.

val to_number_option : Yojson__Safe.t -> float option

Extract Some number, return None if the value is null.

val to_float : Yojson__Safe.t -> float

Extract a float value. to_number is generally preferred as it also works with int literals.

  • raises Type_error

    if argument is not a JSON float.

val to_float_option : Yojson__Safe.t -> float option

Extract Some float value, return None if the value is null. to_number_option is generally preferred as it also works with int literals.

val to_int : Yojson__Safe.t -> int

Extract an int from a JSON int.

val to_int_option : Yojson__Safe.t -> int option

Extract Some int from a JSON int, return None if the value is null.

val to_list : Yojson__Safe.t -> Yojson__Safe.t list

Extract a list from JSON array.

  • raises Type_error

    if argument is not a JSON array.

val to_string : Yojson__Safe.t -> string

Extract a string from a JSON string.

  • raises Type_error

    if argument is not a JSON string.

val to_string_option : Yojson__Safe.t -> string option

Extract Some string from a JSON string, return None if the value is null.

val convert_each : (Yojson__Safe.t -> 'a) -> Yojson__Safe.t -> 'a list

The conversion functions above cannot be used with map, because they do not return JSON values. This convenience function convert_each to_f arr is equivalent to List.map to_f (to_list arr).

Exception-free filters

The following functions operate on lists of JSON nodes. None of them raises an exception when a certain kind of node is expected but no node or the wrong kind of node is found. Instead of raising an exception, nodes that are not as expected are simply ignored.

val filter_map : ('a -> 'b option) -> 'a list -> 'b list

filter_map f l maps each element of the list l to an optional value using function f and unwraps the resulting values.

val flatten : Yojson__Safe.t list -> Yojson__Safe.t list

Expects JSON arrays and returns all their elements as a single list. flatten l is equivalent to List.flatten (filter_list l).

val filter_index : int -> Yojson__Safe.t list -> Yojson__Safe.t list

Expects JSON arrays and returns all their elements existing at the given position.

val filter_list : Yojson__Safe.t list -> Yojson__Safe.t list list

Expects JSON arrays and unwraps them.

val filter_member : string -> Yojson__Safe.t list -> Yojson__Safe.t list

Expects JSON objects and returns all the fields of the given name (at most one field per object).

val filter_assoc : Yojson__Safe.t list -> (string * Yojson__Safe.t) list list

Expects JSON objects and unwraps them.

val filter_bool : Yojson__Safe.t list -> bool list

Expects JSON booleans and unwraps them.

val filter_int : Yojson__Safe.t list -> int list

Expects JSON integers (`Int nodes) and unwraps them.

val filter_float : Yojson__Safe.t list -> float list

Expects JSON floats (`Float nodes) and unwraps them.

val filter_number : Yojson__Safe.t list -> float list

Expects JSON numbers (`Int or `Float) and unwraps them. Ints are converted to floats.

val filter_string : Yojson__Safe.t list -> string list

Expects JSON strings and unwraps them.

val to_field_opt : 'a -> ('b -> [> `Null ] as 'c) -> 'b option -> 'a * 'c
OCaml

Innovation. Community. Security.