Simdjsont.Json

simdjsont · API reference

Dynamic JSON value representation used by this library.

Use this when the shape is not known statically:

let open Simdjsont.Json in

let json =
  Object [ ("ok", Bool true); ("items", Array [ Int 1L; Int 2L ]) ]

let text = Simdjsont.Json.to_string json

JSON value tree used by the high-level codec API.

This is a small JSON-compatible AST. It is useful when the shape of a value is not known statically, or when bridging between JSON and CBOR without defining a record codec first.

Integers are represented as int64 so that values decoded from simdjson's signed and unsigned integer paths can share one constructor.

Example

let open Simdjsont.Json in

let value =
  Object
    [
      ("name", String "Ada");
      ("active", Bool true);
      ("scores", Array [ Int 10L; Int 20L ]);
    ]

let json = to_string value
type t = 
  | Null (* JSON null. *)
  | Bool of bool (* JSON true or false. *)
  | Int of int64 (* Integer JSON number. *)
  | Float of float (* Floating-point JSON number. Non-finite values are written as null. *)
  | String of string (* JSON string. The writer escapes control characters, quotes, and backslashes. *)
  | Array of t list (* JSON array. *)
  | Object of (string * t) list (* JSON object. Field order is preserved when writing. *)
val write_null : Simdjsont__.Simdjsont_obuf.t -> unit

Write null to an output buffer.

val write_bool : Simdjsont__.Simdjsont_obuf.t -> bool -> unit

Write a JSON boolean.

val write_int : Simdjsont__.Simdjsont_obuf.t -> int64 -> unit

Write an integer JSON number.

val write_float : Simdjsont__.Simdjsont_obuf.t -> float -> unit

Write a floating-point JSON number. Non-finite values are written as null, matching the float codec.

val write_string : Simdjsont__.Simdjsont_obuf.t -> string -> unit

Write a JSON string, including surrounding quotes and escaping.

val write : Simdjsont__.Simdjsont_obuf.t -> t -> unit

Write any JSON value to an existing output buffer.

val to_string : t -> string

Serialize to an OCaml string.

val to_bigstring : 
  t ->
  (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t * int

Serialize directly into a parse-ready bigstring, returning it together with the number of valid bytes. No intermediate string is allocated.

val to_buffer : ?capacity:int -> unit -> Simdjsont__.Simdjsont_obuf.t

Create a JSON output buffer. Prefer create_buffer; this name is kept as a convenience alias.

val write_to_buffer : Simdjsont__.Simdjsont_obuf.t -> t -> unit

Alias for write.

val buffer_contents : Simdjsont__.Simdjsont_obuf.t -> string

Alias for the output buffer contents operation.

val create_buffer : ?capacity:int -> unit -> Simdjsont__.Simdjsont_obuf.t

Create a growable output buffer suitable for JSON writing.

val contents : Simdjsont__.Simdjsont_obuf.t -> string

Return the current contents of an output buffer as a string.

val write_array_start : Simdjsont__.Simdjsont_obuf.t -> unit

Write the opening array delimiter. Use with write_array_sep and write_array_end for manual streaming-style construction.

let buf = Simdjsont.Json.create_buffer () in
Simdjsont.Json.write_array_start buf;
Simdjsont.Json.write_int buf 1L;
Simdjsont.Json.write_array_sep buf;
Simdjsont.Json.write_int buf 2L;
Simdjsont.Json.write_array_end buf;
Simdjsont.Json.contents buf
val write_array_end : Simdjsont__.Simdjsont_obuf.t -> unit

Write the closing array delimiter.

val write_array_sep : Simdjsont__.Simdjsont_obuf.t -> unit

Write , between array elements.

val write_object_start : Simdjsont__.Simdjsont_obuf.t -> unit

Write the opening object delimiter. Use with write_key, write_key_sep, write_object_sep, and write_object_end for manual object construction.

val write_object_end : Simdjsont__.Simdjsont_obuf.t -> unit

Write the closing object delimiter.

val write_object_sep : Simdjsont__.Simdjsont_obuf.t -> unit

Write , between object members.

val write_key_sep : Simdjsont__.Simdjsont_obuf.t -> unit

Write : between an object key and value.

val write_key : Simdjsont__.Simdjsont_obuf.t -> string -> unit

Write an escaped object key, including surrounding quotes. Pair it with write_key_sep before writing the value.