Skip to content

Commit d320cbf

Browse files
committed
Add type & value fields to primitive json types
This aligns with how custom types are represented by the JsonExporter. * Update both toJson & fromJson functions in the JsonExporter * Add & update tests
1 parent ddc958e commit d320cbf

File tree

2 files changed

+58
-29
lines changed

2 files changed

+58
-29
lines changed

src/json_export.cpp

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,28 @@ bool JsonExporter::toJson(const Any& any, nlohmann::json& dst) const
1414
nlohmann::json json;
1515
auto const& type = any.castedType();
1616

17+
const std::string type_field = "__type";
18+
const std::string value_field = "value";
19+
1720
if(any.isString())
1821
{
19-
dst = any.cast<std::string>();
22+
dst[type_field] = "string";
23+
dst[value_field] = any.cast<std::string>();
2024
}
2125
else if(type == typeid(int64_t))
2226
{
23-
dst = any.cast<int64_t>();
27+
dst[type_field] = "int64_t";
28+
dst[value_field] = any.cast<int64_t>();
2429
}
2530
else if(type == typeid(uint64_t))
2631
{
27-
dst = any.cast<uint64_t>();
32+
dst[type_field] = "uint64_t";
33+
dst[value_field] = any.cast<uint64_t>();
2834
}
2935
else if(type == typeid(double))
3036
{
31-
dst = any.cast<double>();
37+
dst[type_field] = "double";
38+
dst[value_field] = any.cast<double>();
3239
}
3340
else
3441
{
@@ -51,32 +58,39 @@ JsonExporter::ExpectedEntry JsonExporter::fromJson(const nlohmann::json& source)
5158
{
5259
return nonstd::make_unexpected("json object is null");
5360
}
54-
if(source.is_string())
55-
{
56-
return Entry{ BT::Any(source.get<std::string>()),
57-
BT::TypeInfo::Create<std::string>() };
58-
}
59-
if(source.is_number_unsigned())
60-
{
61-
return Entry{ BT::Any(source.get<uint64_t>()), BT::TypeInfo::Create<uint64_t>() };
62-
}
63-
if(source.is_number_integer())
64-
{
65-
return Entry{ BT::Any(source.get<int64_t>()), BT::TypeInfo::Create<int64_t>() };
66-
}
67-
if(source.is_number_float())
68-
{
69-
return Entry{ BT::Any(source.get<double>()), BT::TypeInfo::Create<double>() };
70-
}
71-
if(source.is_boolean())
61+
if(!source.contains("__type"))
7262
{
73-
return Entry{ BT::Any(source.get<bool>()), BT::TypeInfo::Create<bool>() };
63+
return nonstd::make_unexpected("Missing field '__type'");
7464
}
7565

76-
if(!source.contains("__type"))
66+
if(source.contains("value"))
7767
{
78-
return nonstd::make_unexpected("Missing field '__type'");
68+
if(source["value"].is_string())
69+
{
70+
return Entry{ BT::Any(source["value"].get<std::string>()),
71+
BT::TypeInfo::Create<std::string>() };
72+
}
73+
if(source["value"].is_number_unsigned())
74+
{
75+
return Entry{ BT::Any(source["value"].get<uint64_t>()),
76+
BT::TypeInfo::Create<uint64_t>() };
77+
}
78+
if(source["value"].is_number_integer())
79+
{
80+
return Entry{ BT::Any(source["value"].get<int64_t>()),
81+
BT::TypeInfo::Create<int64_t>() };
82+
}
83+
if(source["value"].is_number_float())
84+
{
85+
return Entry{ BT::Any(source["value"].get<double>()),
86+
BT::TypeInfo::Create<double>() };
87+
}
88+
if(source["value"].is_boolean())
89+
{
90+
return Entry{ BT::Any(source["value"].get<bool>()), BT::TypeInfo::Create<bool>() };
91+
}
7992
}
93+
8094
auto type_it = type_names_.find(source["__type"]);
8195
if(type_it == type_names_.end())
8296
{

tests/gtest_json.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,25 @@ TEST_F(JsonTest, TwoWaysConversion)
9595
TestTypes::Pose3D pose = { { 1, 2, 3 }, { 4, 5, 6, 7 } };
9696

9797
nlohmann::json json;
98+
exporter.toJson(BT::Any("string_val"), json["string"]);
9899
exporter.toJson(BT::Any(69), json["int"]);
100+
exporter.toJson(BT::Any(static_cast<uint64_t>(96)), json["uint"]);
99101
exporter.toJson(BT::Any(3.14), json["real"]);
100102
exporter.toJson(BT::Any(pose), json["pose"]);
101103

102104
std::cout << json.dump(2) << std::endl;
103105

104-
ASSERT_EQ(json["int"], 69);
105-
ASSERT_EQ(json["real"], 3.14);
106+
ASSERT_EQ(json["string"]["__type"], "string");
107+
ASSERT_EQ(json["string"]["value"], "string_val");
108+
109+
ASSERT_EQ(json["int"]["__type"], "int64_t");
110+
ASSERT_EQ(json["int"]["value"], 69);
111+
112+
ASSERT_EQ(json["uint"]["__type"], "uint64_t");
113+
ASSERT_EQ(json["uint"]["value"], 96);
114+
115+
ASSERT_EQ(json["real"]["__type"], "double");
116+
ASSERT_EQ(json["real"]["value"], 3.14);
106117

107118
ASSERT_EQ(json["pose"]["__type"], "Pose3D");
108119
ASSERT_EQ(json["pose"]["pos"]["x"], 1);
@@ -126,8 +137,12 @@ TEST_F(JsonTest, TwoWaysConversion)
126137
ASSERT_EQ(pose.rot.y, pose2.rot.y);
127138
ASSERT_EQ(pose.rot.z, pose2.rot.z);
128139

129-
auto num = exporter.fromJson(json["int"])->first.cast<int>();
130-
ASSERT_EQ(num, 69);
140+
auto string_val = exporter.fromJson(json["string"])->first.cast<std::string>();
141+
ASSERT_EQ(string_val, "string_val");
142+
auto int_val = exporter.fromJson(json["int"])->first.cast<int>();
143+
ASSERT_EQ(int_val, 69);
144+
auto uint_val = exporter.fromJson(json["uint"])->first.cast<uint>();
145+
ASSERT_EQ(uint_val, 96);
131146
auto real = exporter.fromJson(json["real"])->first.cast<double>();
132147
ASSERT_EQ(real, 3.14);
133148
}

0 commit comments

Comments
 (0)