Simdjsont.Decode

simdjsont · API reference

Codecs and decoding functions.

This module re-exports the Codec interface.

Example: a record codec

type user = { id : int; name : string; email : string option }

let user : user Simdjsont.Codec.t =
  let open Simdjsont.Codec in
  Obj.field (fun id name email -> { id; name; email })
  |> Obj.mem "id" int ~enc:(fun u -> u.id)
  |> Obj.mem "name" string ~enc:(fun u -> u.name)
  |> Obj.opt_mem "email" string ~enc:(fun u -> u.email)
  |> Obj.finish

How object decoding works

  • a decoder from a low-level raw element to 'a
  • an encoder from 'a to JSON written into a buffer
  • a conversion from 'a to the dynamic JSON tree Required fields fail with a path-aware error when they are absent or have the wrong type. Optional object members accept both a missing field and a present null value as None. Unknown object members are ignored.
type error = {
  path : string list;
  message : string;
}

Error value used by Decode_error.

  • path is a list of strings accumulated during decoding.
  • message is a human-readable error message.
exception Decode_error of error

Exception raised by decoders.

val error_to_string : error -> string

Convert an error to a string.

type 'a t

A codec for values of type 'a.

val decode_string : 'a t -> string -> ('a, string) result

decode_string codec json parses json and decodes the root value using codec. The Error case contains either the simdjson parse message or a codec error such as "user.id: expected int".

val decode_string_exn : 'a t -> string -> 'a

Like decode_string, but raises Failure with the error message. Prefer decode_string at trust boundaries.

val decode_bigstring : 
  'a t ->
  (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t ->
  len:int ->
  ('a, string) result

decode_bigstring codec buf ~len decodes the first len bytes of the bigstring buf using codec. Parsing is zero-copy when buf already has the padding required by the parser; otherwise the data is copied into a padded buffer.

val decode_bigstring_exn : 
  'a t ->
  (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t ->
  len:int ->
  'a

Like decode_bigstring, but raises on error.

val decode_element : 
  'a t ->
  Simdjsont__.Simdjsont_raw.element ->
  ('a, string) result

Decode an already parsed low-level element. The caller must keep the parser that owns element alive for the duration of the call.

val encode_to_obuf : 'a t -> 'a -> Simdjsont__.Simdjsont_obuf.t

Encode a value into a fresh output buffer using the given codec.

val encode_string : 'a t -> 'a -> string

Encode a value to a JSON string using the given codec.

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

Encode a value directly into a parse-ready bigstring, returning it with the number of valid bytes. No intermediate string is allocated, and the result can be fed straight back into decode_bigstring.

val to_json : 'a t -> 'a -> Simdjsont__.Simdjsont_json.t

Convert a value to JSON AST using the given codec.

val null : unit t

Codec for the JSON null value, represented as ().

val bool : bool t

Codec for JSON booleans.

Simdjsont.Codec.decode_string Simdjsont.Codec.bool "true" = Ok true
val int : int t

Codec for JSON integers mapped to OCaml int. Use int64 when the input may exceed the platform int range.

val int64 : int64 t

Codec for JSON integers mapped to OCaml int64.

val float : float t

Codec for JSON numbers mapped to OCaml float. Integer JSON numbers are accepted and converted to floats. Non-finite OCaml floats encode as null because JSON has no NaN or infinity literals.

val int32 : int32 t

Codec for JSON integers mapped to OCaml int32. Decoding fails when the JSON number is not an integer or does not fit in 32 bits.

val nativeint : nativeint t

Codec for JSON integers mapped to OCaml nativeint. Decoding fails when the JSON number is not an integer or does not fit in a nativeint (checked by round-trip).

val char : char t

Codec for JSON strings of length 1 mapped to OCaml char. Decoding fails for strings whose length is not exactly 1.

val bytes : bytes t

Codec for JSON strings mapped to OCaml bytes. Encoding and decoding are the same as string, converted with Bytes.of_string / Bytes.to_string.

val string : string t

Codec for JSON strings. Encoding escapes control characters, quotes, and backslashes.

val list : 'a t -> 'a list t

list item maps JSON arrays to OCaml lists.

Simdjsont.Codec.decode_string Simdjsont.Codec.(list int) "[1,2,3]"
= Ok [ 1; 2; 3 ]
val array : 'a t -> 'a array t

array item maps JSON arrays to OCaml arrays. Prefer list when you do not need random access or mutable array operations.

val optional : 'a t -> 'a option t

optional codec maps null to None and any other value to Some decoded_value. When used with Obj.opt_mem, a missing object member also becomes None.

val map : ('a -> 'b) -> ('b -> 'a) -> 'a t -> 'b t

map decode encode codec adapts codec through conversion functions.

This is useful for small domain types:

type user_id = User_id of int

let user_id =
  Simdjsont.Codec.map
    (fun n -> User_id n)
    (fun (User_id n) -> n)
    Simdjsont.Codec.int
val make : 
  decode:(string list -> Simdjsont__.Simdjsont_raw.element -> 'a) ->
  encode:('a -> Simdjsont__.Simdjsont_obuf.t -> unit) ->
  to_json:('a -> Simdjsont__.Simdjsont_json.t) ->
  'a t

make ~decode ~encode ~to_json builds a codec from its three operations. This is the escape hatch used by generated code (and hand-written custom codecs) to define JSON mappings that cannot be expressed with the combinators above, such as tuples or sum types.

  • decode path elt decodes elt, raising Decode_error { path; message } on failure. Use a field :: path extension when delegating to nested codecs via decode_with_path so errors keep their JSON path.
  • encode v buf writes the JSON representation of v into the existing output buffer buf. Delegate to nested codecs with encode_into.
  • to_json converts v to the dynamic JSON tree (used by CBOR encoding).
val decode_with_path : 
  'a t ->
  string list ->
  Simdjsont__.Simdjsont_raw.element ->
  'a

decode_with_path codec path elt decodes elt with an explicit error path prefix. Raises Decode_error on failure. Use this (rather than decode_element) when implementing make codecs so nested errors keep their full path.

val encode_into : 'a t -> 'a -> Simdjsont__.Simdjsont_obuf.t -> unit

encode_into codec v buf writes the JSON encoding of v into the existing output buffer buf. Use this (rather than encode_to_obuf) when implementing make codecs so nested values share one buffer.

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

fix f builds a recursive codec. f self must construct a codec that may mention self (only under decode/encode/to_json closures, which is how generated recursive codecs use it). The result delegates every operation through a single lazily-computed f self, so f runs once.

This exists because let rec c = ... c ... is rejected when the right-hand side is a codec combinator application rather than a function; fix moves the recursion inside the library where a record/lazy right-hand side is allowed. Generated code uses it for recursive monomorphic types.

val fix2 : (('a t * 'b t) -> 'a t * 'b t) -> 'a t * 'b t

fix2 f is the two-way mutual-recursion analogue of fix. f (a, b) must construct the pair of codecs, possibly mentioning a and b under closures. The result pair delegates through one lazily-computed f (a,b). Generated code uses it for mutually recursive pairs of monomorphic types.

module Obj : sig ... end

Builder for JSON objects.

val value : Simdjsont__.Simdjsont_json.t t

Codec for dynamic JSON values.

Use this when the document shape is not known at compile time, or when you want to transform a JSON/CBOR value tree manually before re-encoding it.