Simdjsont.Codec.Obj

simdjsont · API reference

Builder for JSON objects.

The builder API is driven by a constructor function provided to field, then extended with fields using mem and opt_mem, and finalized with finish.

type ('o, 'dec) builder

Builder state for an object codec.

'o is the resulting OCaml type.

val field : 'a -> ('o, 'a) builder

field constructor starts an object codec from a constructor function. Add one mem or opt_mem per constructor argument, in the same order the constructor expects them.

val mem : 
  string ->
  'a t ->
  enc:('o -> 'a) ->
  ('o, 'a -> 'b) builder ->
  ('o, 'b) builder

mem name codec ~enc builder adds a required member. Decoding fails if name is absent. enc extracts the field value when encoding.

val opt_mem : 
  string ->
  'a t ->
  enc:('o -> 'a option) ->
  ('o, 'a option -> 'b) builder ->
  ('o, 'b) builder

opt_mem name codec ~enc builder adds an optional member. Decoding yields None when the member is absent or null. Encoding omits the member when enc value is None.

val finish : ('o, 'o) builder -> 'o t

finish builder turns a completed object builder into a codec. The final builder type must show that all constructor arguments have been supplied; this is why missing mem / opt_mem calls are usually type errors.