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 opt_mem_with_default : 
  string ->
  'a t ->
  default:'a ->
  enc:('o -> 'a) ->
  ('o, 'a -> 'b) builder ->
  ('o, 'b) builder

opt_mem_with_default name codec ~default ~enc builder adds a member with a default. Decoding yields default when the member is absent; otherwise the present value is decoded with codec (a present null is decoded, not defaulted, so it fails unless codec accepts null). Encoding always writes the member, like mem.

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.