Ppx_simdjsont

ppx_simdjsont · API reference

Ppx deriver for simdjsont codecs.

type t = ... [@@deriving simdjsont] generates:

  • t_codec : t Simdjsont.Codec.t
  • t_of_string : string -> (t, string) result
  • t_to_string : t -> string For 'a box, box_codec takes one codec argument per type parameter, and the helpers take them as well.

Supported shapes:

  • records (with @key/@name, @default, @codec)

  • variants with nullary and tuple payloads (nullary as string, payloads as "Tag", args...), with @name/@key on constructors

  • tuples (as JSON arrays), options, lists, arrays

  • primitives: bool, int, int32, int64, nativeint, float, string, bytes, char, unit, Json.t

  • aliases, type parameters, (mutually) recursive types Field attributes:

  • @key "name" (alias @name) renames the JSON member.

  • @default expr makes a field optional on decode (missing -> expr) while always encoding it.

  • @codec expr overrides the field codec. For 'a option fields the expression must be the inner codec (for 'a), otherwise the full codec. Constructor attributes:

  • @name "Tag" (alias @key) renames the JSON tag.

Complex transformations

Nested records compose: every level derives its own codec, so renames, defaults, options, and collections can be mixed freely.

type tls = { enabled : bool; cert : string option } [@@deriving simdjsont]

type server = {
  host : string; [@key "hostName"]
  port : int; [@default 80]
  tls : tls;
  tags : string list;
}
[@@deriving simdjsont]

(* Decoding fills the default and treats a missing cert as None. *)
let srv =
  server_of_string
    {|{"hostName":"db.internal","tls":{"enabled":true},
       "tags":["primary","eu"]}|}
(* Ok { host = "db.internal"; port = 80;
        tls = { enabled = true; cert = None };
        tags = ["primary"; "eu"] } *)

(* Encoding always writes port and omits cert when None. *)
let json =
  server_to_string
    {
      host = "db.internal";
      port = 80;
      tls = { enabled = true; cert = None };
      tags = [ "primary" ];
    }
(* {"hostName":"db.internal","port":80,
    "tls":{"enabled":true},"tags":["primary"]} *)

Variants map nullary constructors to strings and payloads to tag-led arrays, with optional tag renames:

type shape = Circle of float | Rect of float * float | Point
[@@deriving simdjsont]
(* Point <-> "Point", Circle 1.5 <-> ["Circle",1.5],
   Rect (1.5, 2.5) <-> ["Rect",1.5,2.5] *)

type msg = Ping [@name "ping"] | Text of string [@name "text"]
[@@deriving simdjsont]
(* Ping <-> "ping", Text "hello" <-> ["text","hello"] *)

Type parameters become codec arguments, and (mutually) recursive types need no extra plumbing:

type 'a page = { items : 'a list; total : int } [@@deriving simdjsont]
let json =
  page_to_string Simdjsont.Codec.int { items = [ 1; 2 ]; total = 2 }
(* {"items":[1,2],"total":2} *)

type expr = Lit of int | Add of expr * expr | Var of string
[@@deriving simdjsont]
(* Add (Lit 1, Var "x") <-> ["Add",["Lit",1],["Var","x"]] *)

type author = { name : string; books : book list }
and book = { title : string; author : author option }
[@@deriving simdjsont]

Decode errors carry the dotted JSON path, using JSON member names: a mistyped tls.enabled yields Error "tls.enabled: expected bool", and an omitted hostName yields Error "missing field: hostName".

All of the above is exercised verbatim by ppx/test/test_ppx_doc.ml.

val key_label_attr : 
  (Astlib.Ast_502.Parsetree.label_declaration, string) Ppxlib.Attribute.t
val name_label_attr : 
  (Astlib.Ast_502.Parsetree.label_declaration, string) Ppxlib.Attribute.t
val default_label_attr : 
  (Astlib.Ast_502.Parsetree.label_declaration,
    Astlib.Ast_502.Parsetree.expression)
    Ppxlib.Attribute.t
val codec_label_attr : 
  (Astlib.Ast_502.Parsetree.label_declaration,
    Astlib.Ast_502.Parsetree.expression)
    Ppxlib.Attribute.t
val codec_core_attr : 
  (Astlib.Ast_502.Parsetree.core_type, Astlib.Ast_502.Parsetree.expression)
    Ppxlib.Attribute.t
val default_core_attr : 
  (Astlib.Ast_502.Parsetree.core_type, Astlib.Ast_502.Parsetree.expression)
    Ppxlib.Attribute.t
val key_ctor_attr : 
  (Astlib.Ast_502.Parsetree.constructor_declaration, string) Ppxlib.Attribute.t
val name_ctor_attr : 
  (Astlib.Ast_502.Parsetree.constructor_declaration, string) Ppxlib.Attribute.t
val get_label_key : 
  Astlib.Ast_502.Parsetree.label_declaration ->
  default:string ->
  string
val get_ctor_tag : 
  Astlib.Ast_502.Parsetree.constructor_declaration ->
  default:string ->
  string
val get_label_default : 
  Astlib.Ast_502.Parsetree.label_declaration ->
  Astlib.Ast_502.Parsetree.expression option
val get_label_codec : 
  Astlib.Ast_502.Parsetree.label_declaration ->
  Astlib.Ast_502.Parsetree.expression option
val evar : 
  loc:Ppxlib.Location.t ->
  string ->
  Astlib.Ast_502.Parsetree.expression
val ecodec : 
  loc:Ppxlib.Location.t ->
  string ->
  Astlib.Ast_502.Parsetree.expression
val suppress_unused_warning : 
  loc:Ppxlib.Location.t ->
  Ppxlib.expression ->
  Ppxlib.expression
val longident_append_codec : Ppxlib.longident -> Ppxlib.longident
val is_option_type : Ppxlib.core_type -> Ppxlib.core_type option
val primitive_codec_expr : 
  loc:Ppxlib.Location.t ->
  Ppxlib.longident ->
  Ppxlib.expression option
val codec_of_core_type : 
  loc:Ppxlib.Location.t ->
  (string * Ppxlib.expression) list ->
  Ppxlib.core_type ->
  Ppxlib.expression
val tuple_codec_expr : 
  loc:Ppxlib.Location.t ->
  (string * Ppxlib.expression) list ->
  Ppxlib.core_type list ->
  Ppxlib.expression
type field_info = {
  fname : string;
  floc : Ppxlib.Location.t;
  json_key : string;
  is_option : bool;
  inner_ct : Ppxlib.core_type;
  full_ct : Ppxlib.core_type;
  codec_override : Ppxlib.expression option;
  default : Ppxlib.expression option;
}
val field_info_of_label : Ppxlib.label_declaration -> field_info
val record_codec_expr : 
  loc:Ppxlib.Location.t ->
  (string * Ppxlib.expression) list ->
  Ppxlib.label_declaration list ->
  Ppxlib.expression
type ctor_info = {
  cname : string;
  cloc : Ppxlib.Location.t;
  tag : string;
  payload : Ppxlib.core_type list;
}
val ctor_info_of_decl : Ppxlib.constructor_declaration -> ctor_info
val variant_codec_expr : 
  loc:Ppxlib.Location.t ->
  (string * Ppxlib.expression) list ->
  Ppxlib.constructor_declaration list ->
  Ppxlib.expression
val codec_value_name : Ppxlib.type_declaration -> string
val of_string_name : Ppxlib.type_declaration -> string
val to_string_name : Ppxlib.type_declaration -> string
val params_of_decl : 
  loc:Ppxlib.Location.t ->
  Ppxlib.type_declaration ->
  (string * string * Astlib.Ast_502.Parsetree.expression) list
val env_of_params : (string * 'a * 'b) list -> (string * 'b) list
val codec_expr_of_decl : 
  loc:Ppxlib.Location.t ->
  (string * Ppxlib.expression) list ->
  Ppxlib.type_declaration ->
  Ppxlib.expression
val codec_binding : 
  loc:Ppxlib.Location.t ->
  Ppxlib.type_declaration ->
  (string * string * Ppxlib.expression) list ->
  (string * Ppxlib.expression) list ->
  Astlib.Ast_502.Parsetree.value_binding
val of_string_binding : 
  loc:Ppxlib.Location.t ->
  Ppxlib.type_declaration ->
  (string * string * Ppxlib.expression) list ->
  Astlib.Ast_502.Parsetree.value_binding
val to_string_binding : 
  loc:Ppxlib.Location.t ->
  Ppxlib.type_declaration ->
  (string * string * Ppxlib.expression) list ->
  Astlib.Ast_502.Parsetree.value_binding
val str_type_decl : 
  loc:Ppxlib.Location.t ->
  path:'a ->
  (Ppxlib.rec_flag * Ppxlib.type_declaration list) ->
  Astlib.Ast_502.Parsetree.structure_item list
val core_type_of_decl : 
  loc:Ppxlib.Location.t ->
  Ppxlib.type_declaration ->
  Ppxlib.core_type
val codec_core_type : 
  loc:Ppxlib.location ->
  Ppxlib.type_declaration ->
  Ppxlib.core_type
val sig_type_decl : 
  loc:Ppxlib.location ->
  path:'a ->
  ('b * Ppxlib.type_declaration list) ->
  Astlib.Ast_502.Parsetree.signature_item list