99{% set schema_id_cpp_t = schemaId.primitive_type.name | replace_keyword %}
1010{% set version_cpp_t = version.primitive_type.name | replace_keyword %}
1111
12+ {% set decl_json_io = True %}
13+
1214{# ------------------------------------ #}
1315
1416{% macro meta_decl() %}
@@ -115,7 +117,7 @@ public:
115117 {% set choice_method = choice.name [0].lower () ~ choice.name [1:] %}
116118 {% set choice_bit = 'k' ~ choice.name [0].upper () ~ choice.name [1:] ~ 'Bit' %}
117119
118- [[nodiscard]] constexpr auto {{ choice_method }}() noexcept -> bool {
120+ [[nodiscard]] constexpr auto {{ choice_method }}() const noexcept -> bool {
119121 return {{ choice_bit }} == ({{ choice_bit }} & value_);
120122 }
121123
@@ -439,6 +441,17 @@ public:
439441 static_assert(static_cast<std::string_view>(N).size () + 1 < 0, " Field not found ({{ class_cpp_t }})" );
440442 }
441443{% endif %}
444+ {% if decl_json_io %}
445+
446+ {# asJson() #}
447+ constexpr auto asJson() -> nlohmann::json {
448+ auto json = nlohmann::json::object();
449+ {% for field in message.fields %}
450+ json[" {{ field.name }}" ] = this->get<" {{ field.name }}" >().asJson ();
451+ {% endfor %}
452+ return json;
453+ }
454+ {% endif %}
442455};
443456{%- endmacro %}
444457
@@ -893,6 +906,18 @@ struct {{ class_cpp_t }} {
893906 return *this;
894907 }
895908{% endif %}
909+ {% if decl_json_io %}
910+
911+ {# asJson() #}
912+ constexpr auto asJson() -> nlohmann::json {
913+ {% if is_optional %}
914+ if (!this->present()) {
915+ return nlohmann::json();
916+ }
917+ {% endif %}
918+ return this->value();
919+ }
920+ {% endif %}
896921};
897922{%- endmacro %}
898923
@@ -1010,6 +1035,19 @@ struct {{ class_cpp_t }} {
10101035 return this->value({{ value_cpp_t }}::SBE_NULL);
10111036 }
10121037{% endif %}
1038+ {% if decl_json_io %}
1039+
1040+ {# asJson() #}
1041+ constexpr auto asJson() -> nlohmann::json {
1042+ switch (this->value()) {
1043+ {% for valid_value in type.valid_values %}
1044+ case {{ value_cpp_t }}::{{ valid_value.name | fmt_enum_value }}: return " {{ valid_value.name }}" ;
1045+ {% endfor %}
1046+ default: break;
1047+ }
1048+ return nlohmann::json();
1049+ }
1050+ {% endif %}
10131051};
10141052{%- endmacro %}
10151053
@@ -1083,6 +1121,21 @@ struct {{ class_cpp_t }} {
10831121 {% endif %}
10841122 return *this;
10851123 }
1124+ {% if decl_json_io %}
1125+
1126+ {# asJson() #}
1127+ constexpr auto asJson() -> nlohmann::json {
1128+ auto const val = this->value();
1129+ auto json = nlohmann::json::array();
1130+ {% for choice in type.choices %}
1131+ {% set choice_method = choice.name [0].lower () ~ choice.name [1:] %}
1132+ if (val. {{ choice_method }}()) {
1133+ json.push_back (" {{ choice.name }}" );
1134+ }
1135+ {% endfor %}
1136+ return json;
1137+ }
1138+ {% endif %}
10861139};
10871140{%- endmacro %}
10881141
@@ -1124,6 +1177,20 @@ struct {{ class_cpp_t }} : public {{ value_cpp_t }} {
11241177
11251178 // TODO: convert to decimal/double/etc... ?
11261179{% endif %}
1180+ {% if decl_json_io %}
1181+
1182+ {# asJson() #}
1183+ constexpr auto asJson() -> nlohmann::json {
1184+ if (!this->present()) {
1185+ return nlohmann::json();
1186+ }
1187+ auto json = nlohmann::json::object();
1188+ {% for composite_type in type.composite_types %}
1189+ json[" {{ composite_type.reference_name }}" ] = this->get<" {{ composite_type.reference_name }}" >().asJson ();
1190+ {% endfor %}
1191+ return json;
1192+ }
1193+ {% endif %}
11271194};
11281195{%- endmacro %}
11291196
@@ -1148,9 +1215,26 @@ struct {{ class_cpp_t }} : public {{ group_class_cpp_t }} {
11481215 return {{ entry.since_version }};
11491216 }
11501217
1218+ {# TODO: remove ? #}
11511219 [[nodiscard]] constexpr auto present() const noexcept -> bool {
11521220 return true;
11531221 }
1222+ {% if decl_json_io %}
1223+
1224+ {# asJson() #}
1225+ constexpr auto asJson() -> nlohmann::json {
1226+ auto json = nlohmann::json::array();
1227+ while (this->hasNext()) {
1228+ this->next();
1229+ auto obj = nlohmann::json::object();
1230+ {% for field in entry.fields %}
1231+ obj[" {{ field.name }}" ] = this->get<" {{ field.name }}" >().asJson ();
1232+ {% endfor %}
1233+ json.push_back (obj);
1234+ }
1235+ return json;
1236+ }
1237+ {% endif %}
11541238};
11551239{%- endmacro %}
11561240
@@ -1175,9 +1259,17 @@ struct {{ class_cpp_t }} : public {{ group_class_cpp_t }} {
11751259 return {{ entry.since_version }};
11761260 }
11771261
1262+ {# TODO: remove? #}
11781263 [[nodiscard]] constexpr auto present() const noexcept -> bool {
11791264 return true;
11801265 }
1266+ {% if decl_json_io %}
1267+
1268+ {# asJson() #}
1269+ constexpr auto asJson() -> nlohmann::json {
1270+ return this->value();
1271+ }
1272+ {% endif %}
11811273};
11821274{%- endmacro %}
11831275
0 commit comments