Type Error
exception Type_error of string * tRaised when a value has unexpected type. Type_error (expected, actual_value)
Constructors
val null : tThe null value.
val bool : bool -> tCreate a boolean value.
val int : int -> tCreate an integer value (stored as float).
val float : float -> tCreate a float value.
val string : string -> tCreate a string value.
val strings : string list -> tCreate a list of strings.
Type Predicates
val is_null : t -> boolCheck if value is null.
val is_bool : t -> boolCheck if value is a boolean.
val is_number : t -> boolCheck if value is a number.
val is_string : t -> boolCheck if value is a string.
val is_list : t -> boolCheck if value is a list.
val is_obj : t -> boolCheck if value is an object.
Safe Accessors
These return None if the value has the wrong type.
val as_null : t -> unit optionGet unit if value is null.
val as_bool : t -> bool optionGet boolean value.
val as_float : t -> float optionGet float value.
val as_string : t -> string optionGet string value.
val as_int : t -> int optionGet 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 -> unitGet unit or raise Type_error.
val get_bool : t -> boolGet boolean or raise Type_error.
val get_float : t -> floatGet float or raise Type_error.
val get_string : t -> stringGet string or raise Type_error.
Get list or raise Type_error.
Get object or raise Type_error.
val get_int : t -> intGet integer or raise Type_error.
Object Operations
val mem : string -> t -> boolmem key obj checks if key exists in object obj. Returns false if obj is not an object.
find key obj looks up key in object obj. Returns None if key not found or if obj is not an object.
get key obj looks up key in object obj. Raises Not_found if key not found.
val keys : t -> string listGet all keys from an object.
update key value obj sets key to value in obj. Adds the key if it doesn't exist.
List Operations
nth n lst gets element at index n. Returns None if lst is not a list or index out of bounds.
val length : t -> intGet the length of a list or object. Returns 0 for other types.
Path Operations
get_path ["a"; "b"; "c"] obj looks up nested path obj.a.b.c. Returns None if any key is not found.
Iteration
iter_obj f obj calls f key value for each pair in obj.
fold_obj f init obj folds over object key-value pairs.
Mapping
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 -> boolGet boolean or return default.
val to_int : ?default:int -> t -> intGet integer or return default.
val to_float : ?default:float -> t -> floatGet float or return default.
val to_string : ?default:string -> t -> stringGet string or return default.