thrift_generator_codec_json (thrift_generator v0.7.0)
View SourceSpecialized JSON wire codecs emitted into the types module: per struct
an encode_<X>_json/1 (wire map → {ok, iodata()} | {error, _}), a
decode_<X>_json/1 (binary → {ok, WireMap, Rest} | more | {error, _}, consuming the message envelope's closing ] like the top-level
generic decode) and a json_codec/1 dispatcher ({ok, {Encode, Decode}} | error by wire struct name). The clauses mirror the generic
engine (thrift_runtime_json) byte for byte and error term for error term
— the same discipline as the binary and compact emitters, proven by
the PropEr equivalence properties and the negative battery in
thrift_codec_test.
Shape of the generated encode: a chain of per-field functions building
the list of "id":{"tag":value} chunks in declaration order (absent
fields fall through), the entry wrapping it in {...} with commas
joined exactly like the generic lists:join. The decode is a
fields/field/after-function triple walking the JSON object with the
shared scanner primitives (json_skip_ws, json_expect,
json_scan_string, json_scan_number — emitted once per module, when
something calls them) and a per-id/tag dispatch whose unknown-id clause
delegates to the exported thrift_runtime_json:skip_by_tag/2.
Every type tag ("i32", "str", "rec", ...) and the
"id":{"tag": field prefixes are compile-time literals. Doubles use
the shared json_format_double (16 fractional digits), map keys are
restricted to scalars and enums — a map over any other key type degrades
to constant type_not_supported errors, and so does uuid anywhere.
Nested same-module struct refs call the sibling's decode body (the
]-fold belongs to the top level only); cross-module refs go through
the generic engine, which probes the target module's json_codec/1.
Forms are built as raw erl_parse trees — patterns, guards and the
try ... catch key conversions have no convenient erl_syntax
constructors — so erl_pp renders them; doc and spec attributes stay
syntax trees like everywhere else in the emitter.
Summary
Functions
The -export/1 entries of the JSON wire codecs: json_codec/1 plus one pair per struct.
Every JSON wire-codec form of the module: per-struct encode/decode
functions, container and scanner helpers, json_codec/1.
Types
-type ast_node() :: #thrift_generator_node{line :: non_neg_integer(), col :: non_neg_integer(), doc :: doc(), annotations :: erl_annotations(), payload :: term(), type_module :: module()}.
-type base_type() :: bool | i8 | i16 | i32 | i64 | double | string | binary | uuid.
-type const_value() :: {ref, Name :: binary()} | boolean() | integer() | float() | binary() | {list, [const_value()]} | {map, [{const_value(), const_value()}]}.
-type constant() :: #thrift_generator_constant{name :: constant_name(), type :: thrift_type(), value :: const_value()}.
-nominal constant_name() :: binary().
-type cpp_include() :: #thrift_generator_cpp_include{path :: include_path()}.
-type doc() :: binary() | undefined.
-type enum_() :: #thrift_generator_enum{name :: enum_name(), values :: [enum_value()]}.
-nominal enum_name() :: binary().
-type enum_value() :: {Name :: binary(), Value :: integer() | undefined, annotations(), doc()}.
-type exception_() :: #thrift_generator_exception{name :: exception_name(), fields :: [ast_node()]}.
-nominal exception_name() :: binary().
-type field_() :: #thrift_generator_field{id :: integer() | undefined, required :: requiredness(), type :: thrift_type(), name :: field_name(), default :: const_value() | undefined}.
-nominal field_name() :: binary().
-type function_() :: #thrift_generator_function{oneway :: boolean(), return_type :: thrift_type() | void, name :: function_name(), fields :: [ast_node()], throws :: [ast_node()]}.
-nominal function_name() :: binary().
-type include_() :: #thrift_generator_include{path :: include_path()}.
-nominal include_path() :: binary().
-type module_key() :: binary().
-type namespace_() :: #thrift_generator_namespace{scope :: namespace_scope(), name :: namespace_name()}.
-nominal namespace_name() :: binary().
-nominal namespace_scope() :: binary().
-type node_payload() :: document() | include_() | cpp_include() | namespace_() | constant() | typedef() | enum_() | struct_() | union_def() | exception_() | service() | function_() | field_().
-type requiredness() :: required | optional | default.
-type service() :: #thrift_generator_service{name :: service_name(), extends :: service_parent() | undefined, functions :: [ast_node()]}.
-nominal service_name() :: binary().
-nominal service_parent() :: binary().
-type struct_() :: #thrift_generator_struct{name :: struct_name(), fields :: [ast_node()]}.
-nominal struct_name() :: binary().
-type thrift_type() :: {base, base_type(), annotations()} | type_ref() | {list, Element :: thrift_type(), annotations()} | {set, Element :: thrift_type(), annotations()} | {map, Key :: thrift_type(), Value :: thrift_type(), annotations()}.
-type type_ref() :: {ref, Name :: binary(), annotations()}.
-type typedef() :: #thrift_generator_typedef{name :: typedef_name(), type :: thrift_type()}.
-nominal typedef_name() :: binary().
-type union_def() :: #thrift_generator_union{name :: union_name(), fields :: [ast_node()]}.
-nominal union_name() :: binary().
Functions
-spec export_entries(#tg_module{name :: binary(), prefix :: binary(), path :: binary(), includes :: [binary()], structs :: [#tg_struct{kind :: struct | union | exception, name :: binary(), fields :: [#tg_field{id :: integer(), required :: requiredness(), type :: thrift_type(), name :: binary(), default :: const_value() | undefined, doc :: doc()}], erl :: erl_annotations(), doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], enums :: [#tg_enum{name :: binary(), values :: [{integer(), binary()}], doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], typedefs :: [#tg_typedef{name :: binary(), type :: thrift_type(), erl :: erl_annotations(), doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], consts :: [#tg_const{name :: binary(), type :: thrift_type(), value :: const_value(), doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], services :: [#tg_service{name :: binary(), extends :: binary() | undefined, functions :: [#tg_function{name :: binary(), oneway :: boolean(), return_type :: thrift_type() | void, params :: [#tg_field{id :: integer(), required :: requiredness(), type :: thrift_type(), name :: binary(), default :: const_value() | undefined, doc :: doc()}], throws :: [#tg_field{id :: integer(), required :: requiredness(), type :: thrift_type(), name :: binary(), default :: const_value() | undefined, doc :: doc()}], doc :: doc()}], doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}]}, #ectx{modules :: #{module_key() => #tg_module{name :: binary(), prefix :: binary(), path :: binary(), includes :: [binary()], structs :: [#tg_struct{kind :: struct | union | exception, name :: binary(), fields :: [#tg_field{id :: integer(), required :: requiredness(), type :: thrift_type(), name :: binary(), default :: const_value() | undefined, doc :: doc()}], erl :: erl_annotations(), doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], enums :: [#tg_enum{name :: binary(), values :: [{integer(), binary()}], doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], typedefs :: [#tg_typedef{name :: binary(), type :: thrift_type(), erl :: erl_annotations(), doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], consts :: [#tg_const{name :: binary(), type :: thrift_type(), value :: const_value(), doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], services :: [#tg_service{name :: binary(), extends :: binary() | undefined, functions :: [#tg_function{name :: binary(), oneway :: boolean(), return_type :: thrift_type() | void, params :: [#tg_field{id :: integer(), required :: requiredness(), type :: thrift_type(), name :: binary(), default :: const_value() | undefined, doc :: doc()}], throws :: [#tg_field{id :: integer(), required :: requiredness(), type :: thrift_type(), name :: binary(), default :: const_value() | undefined, doc :: doc()}], doc :: doc()}], doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}]}}, type_modules :: #{module_key() => module()}, include_modules :: #{module_key() => module_key()}, current :: undefined | module_key(), qualify :: boolean()}) -> [{atom(), arity()}].
The -export/1 entries of the JSON wire codecs: json_codec/1 plus one pair per struct.
-spec module_forms(#tg_module{name :: binary(), prefix :: binary(), path :: binary(), includes :: [binary()], structs :: [#tg_struct{kind :: struct | union | exception, name :: binary(), fields :: [#tg_field{id :: integer(), required :: requiredness(), type :: thrift_type(), name :: binary(), default :: const_value() | undefined, doc :: doc()}], erl :: erl_annotations(), doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], enums :: [#tg_enum{name :: binary(), values :: [{integer(), binary()}], doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], typedefs :: [#tg_typedef{name :: binary(), type :: thrift_type(), erl :: erl_annotations(), doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], consts :: [#tg_const{name :: binary(), type :: thrift_type(), value :: const_value(), doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], services :: [#tg_service{name :: binary(), extends :: binary() | undefined, functions :: [#tg_function{name :: binary(), oneway :: boolean(), return_type :: thrift_type() | void, params :: [#tg_field{id :: integer(), required :: requiredness(), type :: thrift_type(), name :: binary(), default :: const_value() | undefined, doc :: doc()}], throws :: [#tg_field{id :: integer(), required :: requiredness(), type :: thrift_type(), name :: binary(), default :: const_value() | undefined, doc :: doc()}], doc :: doc()}], doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}]}, #ectx{modules :: #{module_key() => #tg_module{name :: binary(), prefix :: binary(), path :: binary(), includes :: [binary()], structs :: [#tg_struct{kind :: struct | union | exception, name :: binary(), fields :: [#tg_field{id :: integer(), required :: requiredness(), type :: thrift_type(), name :: binary(), default :: const_value() | undefined, doc :: doc()}], erl :: erl_annotations(), doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], enums :: [#tg_enum{name :: binary(), values :: [{integer(), binary()}], doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], typedefs :: [#tg_typedef{name :: binary(), type :: thrift_type(), erl :: erl_annotations(), doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], consts :: [#tg_const{name :: binary(), type :: thrift_type(), value :: const_value(), doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}], services :: [#tg_service{name :: binary(), extends :: binary() | undefined, functions :: [#tg_function{name :: binary(), oneway :: boolean(), return_type :: thrift_type() | void, params :: [#tg_field{id :: integer(), required :: requiredness(), type :: thrift_type(), name :: binary(), default :: const_value() | undefined, doc :: doc()}], throws :: [#tg_field{id :: integer(), required :: requiredness(), type :: thrift_type(), name :: binary(), default :: const_value() | undefined, doc :: doc()}], doc :: doc()}], doc :: doc(), line :: non_neg_integer(), col :: non_neg_integer()}]}}, type_modules :: #{module_key() => module()}, include_modules :: #{module_key() => module_key()}, current :: undefined | module_key(), qualify :: boolean()}) -> erl_syntax:forms().
Every JSON wire-codec form of the module: per-struct encode/decode
functions, container and scanner helpers, json_codec/1.