Skip to content

Commit

Permalink
Make json serialisation methods inline (cross-language-cpp#136)
Browse files Browse the repository at this point in the history
Inline serialization functions to avoid non-used function warnings if they are enabled.
  • Loading branch information
mutagene authored Nov 5, 2022
1 parent b31fd9d commit 18db492
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <unordered_set>
#include <vector>

static void from_json(const nlohmann::json & j, all_datatypes_json& result) {
inline void from_json(const nlohmann::json & j, all_datatypes_json& result) {
if (j.contains("booleanData")) {
j.at("booleanData").get_to(result.booleanData);
}
Expand Down Expand Up @@ -65,7 +65,7 @@ static void from_json(const nlohmann::json & j, all_datatypes_json& result) {
j.at("myFlags").get_to(result.myFlags);
}
}
static void to_json(nlohmann::json & j, const all_datatypes_json & item) {
inline void to_json(nlohmann::json & j, const all_datatypes_json & item) {
j = nlohmann::json {
{"booleanData", item.booleanData},
{"integer8Data", item.integer8Data},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <algorithm>
#include <nlohmann/json.hpp>

static void to_json(nlohmann::json& j, enum_data e)
inline void to_json(nlohmann::json& j, enum_data e)
{
static const std::pair<enum_data, nlohmann::json> m[] = {{enum_data::FIRSTENUMVALUE,"FirstEnumValue"},{enum_data::SECONDENUMVALUE,"SecondEnumValue"}};
auto it = std::find_if(std::begin(m), std::end(m),
Expand All @@ -18,7 +18,7 @@ static void to_json(nlohmann::json& j, enum_data e)
});
j = ((it != std::end(m)) ? it : std::begin(m))->second;
}
static void from_json(const nlohmann::json& j, enum_data& e)
inline void from_json(const nlohmann::json& j, enum_data& e)
{
static const std::pair<enum_data, nlohmann::json> m[] = {{enum_data::FIRSTENUMVALUE,"FirstEnumValue"},{enum_data::SECONDENUMVALUE,"SecondEnumValue"}};
auto it = std::find_if(std::begin(m), std::end(m),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <algorithm>
#include <nlohmann/json.hpp>

static void to_json(nlohmann::json& j, my_flags e)
inline void to_json(nlohmann::json& j, my_flags e)
{
static const std::pair<my_flags, nlohmann::json> m[] = {{my_flags::FLAG1,"flag1"},{my_flags::FLAG2,"flag2"},{my_flags::FLAG3,"flag3"}};
j = nlohmann::json::array();
Expand All @@ -20,7 +20,7 @@ static void to_json(nlohmann::json& j, my_flags e)
}
}
}
static void from_json(const nlohmann::json& j, my_flags& e)
inline void from_json(const nlohmann::json& j, my_flags& e)
{
static const std::pair<my_flags, nlohmann::json> m[] = {{my_flags::FLAG1,"flag1"},{my_flags::FLAG2,"flag2"},{my_flags::FLAG3,"flag3"}};
e = static_cast<my_flags>(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace custom_namespace {

static void to_json(nlohmann::json& j, my_enum e)
inline void to_json(nlohmann::json& j, my_enum e)
{
static const std::pair<my_enum, nlohmann::json> m[] = {{my_enum::FIRSTENUMVALUE,"FirstEnumValue"},{my_enum::SECONDENUMVALUE,"SecondEnumValue"}};
auto it = std::find_if(std::begin(m), std::end(m),
Expand All @@ -20,7 +20,7 @@ static void to_json(nlohmann::json& j, my_enum e)
});
j = ((it != std::end(m)) ? it : std::begin(m))->second;
}
static void from_json(const nlohmann::json& j, my_enum& e)
inline void from_json(const nlohmann::json& j, my_enum& e)
{
static const std::pair<my_enum, nlohmann::json> m[] = {{my_enum::FIRSTENUMVALUE,"FirstEnumValue"},{my_enum::SECONDENUMVALUE,"SecondEnumValue"}};
auto it = std::find_if(std::begin(m), std::end(m),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace custom_namespace {

static void to_json(nlohmann::json& j, my_flags e)
inline void to_json(nlohmann::json& j, my_flags e)
{
static const std::pair<my_flags, nlohmann::json> m[] = {{my_flags::FLAG1,"flag1"},{my_flags::FLAG2,"flag2"}};
j = nlohmann::json::array();
Expand All @@ -22,7 +22,7 @@ static void to_json(nlohmann::json& j, my_flags e)
}
}
}
static void from_json(const nlohmann::json& j, my_flags& e)
inline void from_json(const nlohmann::json& j, my_flags& e)
{
static const std::pair<my_flags, nlohmann::json> m[] = {{my_flags::FLAG1,"flag1"},{my_flags::FLAG2,"flag2"}};
e = static_cast<my_flags>(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

namespace custom_namespace {

static void from_json(const nlohmann::json & j, my_record& result) {
inline void from_json(const nlohmann::json & j, my_record& result) {
if (j.contains("myEnum")) {
j.at("myEnum").get_to(result.myEnum);
}
if (j.contains("myFlags")) {
j.at("myFlags").get_to(result.myFlags);
}
}
static void to_json(nlohmann::json & j, const my_record & item) {
inline void to_json(nlohmann::json & j, const my_record & item) {
j = nlohmann::json {
{"myEnum", item.myEnum},
{"myFlags", item.myFlags}
Expand Down
57 changes: 29 additions & 28 deletions src/main/scala/djinni/CppGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -217,37 +217,38 @@ class CppGenerator(spec: Spec) extends Generator(spec) {
)
.mkString(",")

w.wl(s"static void to_json(nlohmann::json& j, ${ident.name} e)")
.braced {
if (e.flags) {
w.wl(
s"static const std::pair<${ident.name}, nlohmann::json> m[] = {$enumInitializer};"
)
w.wl("j = nlohmann::json::array();")
w.wl("for(const auto& flagOption : m)").braced {
w.wl(
"if(static_cast<unsigned>(e & flagOption.first) != 0)"
).braced {
w.wl("j.push_back(flagOption.second);")
}
}
} else {
w.wl(
s"static const std::pair<${ident.name}, nlohmann::json> m[] = {$enumInitializer};"
)
w.wl("auto it = std::find_if(std::begin(m), std::end(m),")
w.wl(
s"inline void to_json(nlohmann::json& j, ${ident.name} e)"
).braced {
if (e.flags) {
w.wl(
s"static const std::pair<${ident.name}, nlohmann::json> m[] = {$enumInitializer};"
)
w.wl("j = nlohmann::json::array();")
w.wl("for(const auto& flagOption : m)").braced {
w.wl(
s" [e](const std::pair<${ident.name}, nlohmann::json>& ej_pair) -> bool"
).bracedEnd(");") {
w.wl("return ej_pair.first == e;")
"if(static_cast<unsigned>(e & flagOption.first) != 0)"
).braced {
w.wl("j.push_back(flagOption.second);")
}
w.wl(
"j = ((it != std::end(m)) ? it : std::begin(m))->second;"
)
}
} else {
w.wl(
s"static const std::pair<${ident.name}, nlohmann::json> m[] = {$enumInitializer};"
)
w.wl("auto it = std::find_if(std::begin(m), std::end(m),")
w.wl(
s" [e](const std::pair<${ident.name}, nlohmann::json>& ej_pair) -> bool"
).bracedEnd(");") {
w.wl("return ej_pair.first == e;")
}
w.wl(
"j = ((it != std::end(m)) ? it : std::begin(m))->second;"
)
}
}
w.wl(
s"static void from_json(const nlohmann::json& j, ${ident.name}& e)"
s"inline void from_json(const nlohmann::json& j, ${ident.name}& e)"
).braced {
if (e.flags) {
w.wl(
Expand Down Expand Up @@ -530,7 +531,7 @@ class CppGenerator(spec: Spec) extends Generator(spec) {
val recordSelf = ident.name
// From JSON
w.w(
s"static void from_json(const nlohmann::json & j, ${recordSelf}& result) "
s"inline void from_json(const nlohmann::json & j, ${recordSelf}& result) "
).braced {
for (i <- fields.indices) {
val name = idCpp.field(fields(i).ident)
Expand All @@ -557,7 +558,7 @@ class CppGenerator(spec: Spec) extends Generator(spec) {
}
// To JSON
w.w(
s"static void to_json(nlohmann::json & j, const $recordSelf & item) "
s"inline void to_json(nlohmann::json & j, const $recordSelf & item) "
).braced {
w.w(s"j = nlohmann::json").bracedEnd(";") {
for (i <- fields.indices) {
Expand Down

0 comments on commit 18db492

Please sign in to comment.