Ppx_simdjsont
ppx_simdjsont · API reference
Ppx deriver for simdjsont codecs.
type t = ... [@@deriving simdjsont] generates:
t_codec : t Simdjsont.Codec.tt_of_string : string -> (t, string) resultt_to_string : t -> stringFor'a box,box_codectakes 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/@keyon 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 exprmakes a field optional on decode (missing -> expr) while always encoding it. -
@codec exproverrides the field codec. For'a optionfields 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.tval name_label_attr :
(Astlib.Ast_502.Parsetree.label_declaration, string) Ppxlib.Attribute.tval default_label_attr :
(Astlib.Ast_502.Parsetree.label_declaration,
Astlib.Ast_502.Parsetree.expression)
Ppxlib.Attribute.tval codec_label_attr :
(Astlib.Ast_502.Parsetree.label_declaration,
Astlib.Ast_502.Parsetree.expression)
Ppxlib.Attribute.tval codec_core_attr :
(Astlib.Ast_502.Parsetree.core_type, Astlib.Ast_502.Parsetree.expression)
Ppxlib.Attribute.tval default_core_attr :
(Astlib.Ast_502.Parsetree.core_type, Astlib.Ast_502.Parsetree.expression)
Ppxlib.Attribute.tval key_ctor_attr :
(Astlib.Ast_502.Parsetree.constructor_declaration, string) Ppxlib.Attribute.tval name_ctor_attr :
(Astlib.Ast_502.Parsetree.constructor_declaration, string) Ppxlib.Attribute.tval get_label_key :
Astlib.Ast_502.Parsetree.label_declaration ->
default:string ->
stringval get_ctor_tag :
Astlib.Ast_502.Parsetree.constructor_declaration ->
default:string ->
stringval get_label_default :
Astlib.Ast_502.Parsetree.label_declaration ->
Astlib.Ast_502.Parsetree.expression optionval get_label_codec :
Astlib.Ast_502.Parsetree.label_declaration ->
Astlib.Ast_502.Parsetree.expression optionval evar :
loc:Ppxlib.Location.t ->
string ->
Astlib.Ast_502.Parsetree.expressionval ecodec :
loc:Ppxlib.Location.t ->
string ->
Astlib.Ast_502.Parsetree.expressionval suppress_unused_warning :
loc:Ppxlib.Location.t ->
Ppxlib.expression ->
Ppxlib.expressionval longident_append_codec : Ppxlib.longident -> Ppxlib.longidentval is_option_type : Ppxlib.core_type -> Ppxlib.core_type optionval primitive_codec_expr :
loc:Ppxlib.Location.t ->
Ppxlib.longident ->
Ppxlib.expression optionval codec_of_core_type :
loc:Ppxlib.Location.t ->
(string * Ppxlib.expression) list ->
Ppxlib.core_type ->
Ppxlib.expressionval tuple_codec_expr :
loc:Ppxlib.Location.t ->
(string * Ppxlib.expression) list ->
Ppxlib.core_type list ->
Ppxlib.expressiontype 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_infoval record_codec_expr :
loc:Ppxlib.Location.t ->
(string * Ppxlib.expression) list ->
Ppxlib.label_declaration list ->
Ppxlib.expressiontype ctor_info = {
cname : string;
cloc : Ppxlib.Location.t;
tag : string;
payload : Ppxlib.core_type list;
}val ctor_info_of_decl : Ppxlib.constructor_declaration -> ctor_infoval variant_codec_expr :
loc:Ppxlib.Location.t ->
(string * Ppxlib.expression) list ->
Ppxlib.constructor_declaration list ->
Ppxlib.expressionval codec_value_name : Ppxlib.type_declaration -> stringval of_string_name : Ppxlib.type_declaration -> stringval to_string_name : Ppxlib.type_declaration -> stringval params_of_decl :
loc:Ppxlib.Location.t ->
Ppxlib.type_declaration ->
(string * string * Astlib.Ast_502.Parsetree.expression) listval env_of_params : (string * 'a * 'b) list -> (string * 'b) listval codec_expr_of_decl :
loc:Ppxlib.Location.t ->
(string * Ppxlib.expression) list ->
Ppxlib.type_declaration ->
Ppxlib.expressionval codec_binding :
loc:Ppxlib.Location.t ->
Ppxlib.type_declaration ->
(string * string * Ppxlib.expression) list ->
(string * Ppxlib.expression) list ->
Astlib.Ast_502.Parsetree.value_bindingval of_string_binding :
loc:Ppxlib.Location.t ->
Ppxlib.type_declaration ->
(string * string * Ppxlib.expression) list ->
Astlib.Ast_502.Parsetree.value_bindingval to_string_binding :
loc:Ppxlib.Location.t ->
Ppxlib.type_declaration ->
(string * string * Ppxlib.expression) list ->
Astlib.Ast_502.Parsetree.value_bindingval str_type_decl :
loc:Ppxlib.Location.t ->
path:'a ->
(Ppxlib.rec_flag * Ppxlib.type_declaration list) ->
Astlib.Ast_502.Parsetree.structure_item listval core_type_of_decl :
loc:Ppxlib.Location.t ->
Ppxlib.type_declaration ->
Ppxlib.core_typeval codec_core_type :
loc:Ppxlib.location ->
Ppxlib.type_declaration ->
Ppxlib.core_typeval sig_type_decl :
loc:Ppxlib.location ->
path:'a ->
('b * Ppxlib.type_declaration list) ->
Astlib.Ast_502.Parsetree.signature_item list