Skip to content

Commit 69b8f77

Browse files
committed
update
1 parent 265d0bc commit 69b8f77

6 files changed

Lines changed: 125 additions & 12 deletions

File tree

app/generation/cpp/templates/composite.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#include <bit>
88
#include <cmath>
99
#include <cstddef>
10+
{% if generate.decl_json_io %}
11+
12+
#include <nlohmann/json.hpp>
13+
{% endif %}
1014
{% if type.name == schema.header_type.name %}
1115
#include <span>
1216
{% endif %}

app/generation/cpp/templates/generate.tmpl

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
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

app/generation/cpp/templates/message.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#include <cmath>
99
#include <cstddef>
1010
#include <span>
11+
{% if generate.decl_json_io %}
12+
13+
#include <nlohmann/json.hpp>
14+
{% endif %}
1115
{# let's find all non-trivial types recursively #}
1216
{% set types = [ schema.header_type.name ] %}
1317
{% for field in message.fields recursive %}

tests/CMakeLists.txt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
include(FetchContent)
22

33
if (NOT TARGET doctest_with_main)
4-
FetchContent_Declare(doctest
5-
URL https://github.com/doctest/doctest/archive/refs/tags/v2.4.12.tar.gz
6-
DOWNLOAD_EXTRACT_TIMESTAMP ON
7-
)
8-
FetchContent_MakeAvailable(doctest)
4+
FetchContent_Declare(doctest
5+
URL https://github.com/doctest/doctest/archive/refs/tags/v2.4.12.tar.gz
6+
DOWNLOAD_EXTRACT_TIMESTAMP ON
7+
)
8+
FetchContent_MakeAvailable(doctest)
99
endif()
1010

1111
if (NOT TARGET fmt::fmt)
12-
FetchContent_Declare(fmt
13-
URL https://github.com/fmtlib/fmt/archive/refs/tags/12.1.0.tar.gz
14-
DOWNLOAD_EXTRACT_TIMESTAMP ON
15-
)
16-
FetchContent_MakeAvailable(fmt)
12+
FetchContent_Declare(fmt
13+
URL https://github.com/fmtlib/fmt/archive/refs/tags/12.1.0.tar.gz
14+
DOWNLOAD_EXTRACT_TIMESTAMP ON
15+
)
16+
FetchContent_MakeAvailable(fmt)
17+
endif()
18+
19+
if (NOT TARGET nlohmann_json::nlohmann_json)
20+
FetchContent_Declare(json
21+
URL https://github.com/nlohmann/json/archive/refs/tags/v3.12.0.tar.gz
22+
DOWNLOAD_EXTRACT_TIMESTAMP ON
23+
)
24+
FetchContent_MakeAvailable(json)
1725
endif()
1826

1927
add_subdirectory(binance)

tests/binance/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ set(Target binance)
88
add_executable(${Target} main.cpp)
99
target_compile_features(${Target} PRIVATE cxx_std_20)
1010
target_compile_options(${Target} PRIVATE -Wall -Wextra)
11-
target_link_libraries(${Target} PRIVATE doctest_with_main spot_2_0)
11+
target_link_libraries(${Target} PRIVATE spot_2_0 nlohmann_json::nlohmann_json)
1212
add_test(${Target} ${Target})

tests/binance/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,14 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[]) {
8484
std::bit_cast<std::byte*>((unsigned char*)content::exchangeInfo), content::exchangeInfo_len);
8585
[[maybe_unused]] auto buffer2 =
8686
std::span<std::byte>(std::bit_cast<std::byte*>((unsigned char*)content::ticker), content::ticker_len);
87+
#if 0
8788
spot_sbe::decode(buffer2, [](auto msg) {
8889
Printer::print(std::cout, msg, msg.sbeMessageName());
8990
});
91+
#endif
92+
spot_sbe::decode(buffer2, [](auto msg) {
93+
std::cout << msg.asJson().dump(2) << '\n';
94+
});
9095
} catch (std::exception const& e) {
9196
std::cerr << "ERROR: " << e.what() << '\n';
9297
return EXIT_FAILURE;

0 commit comments

Comments
 (0)