OCaml Library Collection
/
type t = Value.t

Alias for value.

Type Error

exception Type_error of string * t

Raised when a value has unexpected type. Type_error (expected, actual_value)

Constructors

val null : t

The null value.

val bool : bool -> t

Create a boolean value.

val int : int -> t

Create an integer value (stored as float).

val float : float -> t

Create a float value.

val string : string -> t

Create a string value.

val strings : string list -> t

Create a list of strings.

val list : t list -> t

Create a list value.

val obj : (string * t) list -> t

Create an object value from key-value pairs.

Type Predicates

val is_null : t -> bool

Check if value is null.

val is_bool : t -> bool

Check if value is a boolean.

val is_number : t -> bool

Check if value is a number.

val is_string : t -> bool

Check if value is a string.

val is_list : t -> bool

Check if value is a list.

val is_obj : t -> bool

Check if value is an object.

Safe Accessors

These return None if the value has the wrong type.

val as_null : t -> unit option

Get unit if value is null.

val as_bool : t -> bool option

Get boolean value.

val as_float : t -> float option

Get float value.

val as_string : t -> string option

Get string value.

val as_list : t -> t list option

Get list value.

val as_obj : t -> (string * t) list option

Get object as association list.

val as_int : t -> int option

Get integer value if float is an exact integer.

Unsafe Accessors

These raise Type_error if the value has the wrong type.

val get_null : t -> unit

Get unit or raise Type_error.

val get_bool : t -> bool

Get boolean or raise Type_error.

val get_float : t -> float

Get float or raise Type_error.

val get_string : t -> string

Get string or raise Type_error.

val get_list : t -> t list

Get list or raise Type_error.

val get_obj : t -> (string * t) list

Get object or raise Type_error.

val get_int : t -> int

Get integer or raise Type_error.

Object Operations

val mem : string -> t -> bool

mem key obj checks if key exists in object obj. Returns false if obj is not an object.

val find : string -> t -> t option

find key obj looks up key in object obj. Returns None if key not found or if obj is not an object.

val get : string -> t -> t

get key obj looks up key in object obj. Raises Not_found if key not found.

val keys : t -> string list

Get all keys from an object.

val values : t -> t list

Get all values from an object.

val update : string -> t -> t -> t

update key value obj sets key to value in obj. Adds the key if it doesn't exist.

val remove : string -> t -> t

remove key obj removes key from obj.

val combine : t -> t -> t

combine obj1 obj2 merges two objects, with obj2 values taking precedence.

  • raises Type_error

    if either argument is not an object

List Operations

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

map f lst applies f to each element of list lst.

val mapi : (int -> t -> t) -> t -> t

mapi f lst applies f i x to each element x at index i.

val filter : (t -> bool) -> t -> t

filter pred lst keeps elements satisfying pred.

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

fold f init lst folds f over list lst.

val nth : int -> t -> t option

nth n lst gets element at index n. Returns None if lst is not a list or index out of bounds.

val length : t -> int

Get the length of a list or object. Returns 0 for other types.

val flatten : t -> t

Flatten a list of lists into a single list. Non-list elements are kept as-is.

Path Operations

val get_path : string list -> t -> t option

get_path ["a"; "b"; "c"] obj looks up nested path obj.a.b.c. Returns None if any key is not found.

val get_path_exn : string list -> t -> t

Like get_path but raises Not_found if path not found.

Iteration

val iter_obj : (string -> t -> unit) -> t -> unit

iter_obj f obj calls f key value for each pair in obj.

val iter_list : (t -> unit) -> t -> unit

iter_list f lst calls f on each element of lst.

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

fold_obj f init obj folds over object key-value pairs.

Mapping

val map_obj : (string -> t -> t) -> t -> t

map_obj f obj maps f key value over each pair in obj.

val filter_obj : (string -> t -> bool) -> t -> t

filter_obj pred obj keeps pairs satisfying pred key value.

Conversion Helpers

Get values with optional defaults. If no default is provided and the type doesn't match, these raise Type_error.

val to_bool : ?default:bool -> t -> bool

Get boolean or return default.

  • raises Type_error

    if type doesn't match and no default provided

val to_int : ?default:int -> t -> int

Get integer or return default.

  • raises Type_error

    if type doesn't match and no default provided

val to_float : ?default:float -> t -> float

Get float or return default.

  • raises Type_error

    if type doesn't match and no default provided

val to_string : ?default:string -> t -> string

Get string or return default.

  • raises Type_error

    if type doesn't match and no default provided

val to_list : ?default:t list -> t -> t list

Get list or return default.

  • raises Type_error

    if type doesn't match and no default provided