Skip to content

Commit

Permalink
Add type & value fields to primitive json types
Browse files Browse the repository at this point in the history
This aligns with how custom types are represented by the JsonExporter.

* Update both toJson & fromJson functions in the JsonExporter
* Add & update tests
  • Loading branch information
dyackzan committed Jan 29, 2025
1 parent ddc958e commit 067a07b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
61 changes: 36 additions & 25 deletions src/json_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,28 @@ bool JsonExporter::toJson(const Any& any, nlohmann::json& dst) const
nlohmann::json json;
auto const& type = any.castedType();

const std::string type_field = "__type";
const std::string value_field = "value";

if(any.isString())
{
dst = any.cast<std::string>();
dst[type_field] = "string";
dst[value_field] = any.cast<std::string>();
}
else if(type == typeid(int64_t))
{
dst = any.cast<int64_t>();
dst[type_field] = "int64_t";
dst[value_field] = any.cast<int64_t>();
}
else if(type == typeid(uint64_t))
{
dst = any.cast<uint64_t>();
dst[type_field] = "uint64_t";
dst[value_field] = any.cast<uint64_t>();
}
else if(type == typeid(double))
{
dst = any.cast<double>();
dst[type_field] = "double";
dst[value_field] = any.cast<double>();
}
else
{
Expand All @@ -51,32 +58,36 @@ JsonExporter::ExpectedEntry JsonExporter::fromJson(const nlohmann::json& source)
{
return nonstd::make_unexpected("json object is null");
}
if(source.is_string())
{
return Entry{ BT::Any(source.get<std::string>()),
BT::TypeInfo::Create<std::string>() };
}
if(source.is_number_unsigned())
{
return Entry{ BT::Any(source.get<uint64_t>()), BT::TypeInfo::Create<uint64_t>() };
}
if(source.is_number_integer())
{
return Entry{ BT::Any(source.get<int64_t>()), BT::TypeInfo::Create<int64_t>() };
}
if(source.is_number_float())
{
return Entry{ BT::Any(source.get<double>()), BT::TypeInfo::Create<double>() };
}
if(source.is_boolean())
if(!source.contains("__type"))
{
return Entry{ BT::Any(source.get<bool>()), BT::TypeInfo::Create<bool>() };
return nonstd::make_unexpected("Missing field '__type'");
}

if(!source.contains("__type"))
if (source.contains("value"))
{
return nonstd::make_unexpected("Missing field '__type'");
if(source["value"].is_string())
{
return Entry{ BT::Any(source["value"].get<std::string>()),
BT::TypeInfo::Create<std::string>() };
}
if(source["value"].is_number_unsigned())
{
return Entry{ BT::Any(source["value"].get<uint64_t>()), BT::TypeInfo::Create<uint64_t>() };
}
if(source["value"].is_number_integer())
{
return Entry{ BT::Any(source["value"].get<int64_t>()), BT::TypeInfo::Create<int64_t>() };
}
if(source["value"].is_number_float())
{
return Entry{ BT::Any(source["value"].get<double>()), BT::TypeInfo::Create<double>() };
}
if(source["value"].is_boolean())
{
return Entry{ BT::Any(source["value"].get<bool>()), BT::TypeInfo::Create<bool>() };
}
}

auto type_it = type_names_.find(source["__type"]);
if(type_it == type_names_.end())
{
Expand Down
23 changes: 19 additions & 4 deletions tests/gtest_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,25 @@ TEST_F(JsonTest, TwoWaysConversion)
TestTypes::Pose3D pose = { { 1, 2, 3 }, { 4, 5, 6, 7 } };

nlohmann::json json;
exporter.toJson(BT::Any("string_val"), json["string"]);
exporter.toJson(BT::Any(69), json["int"]);
exporter.toJson(BT::Any(static_cast<uint64_t>(96)), json["uint"]);
exporter.toJson(BT::Any(3.14), json["real"]);
exporter.toJson(BT::Any(pose), json["pose"]);

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

ASSERT_EQ(json["int"], 69);
ASSERT_EQ(json["real"], 3.14);
ASSERT_EQ(json["string"]["__type"], "string");
ASSERT_EQ(json["string"]["value"], "string_val");

ASSERT_EQ(json["int"]["__type"], "int64_t");
ASSERT_EQ(json["int"]["value"], 69);

ASSERT_EQ(json["uint"]["__type"], "uint64_t");
ASSERT_EQ(json["uint"]["value"], 96);

ASSERT_EQ(json["real"]["__type"], "double");
ASSERT_EQ(json["real"]["value"], 3.14);

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

auto num = exporter.fromJson(json["int"])->first.cast<int>();
ASSERT_EQ(num, 69);
auto string_val = exporter.fromJson(json["string"])->first.cast<std::string>();
ASSERT_EQ(string_val, "string_val");
auto int_val = exporter.fromJson(json["int"])->first.cast<int>();
ASSERT_EQ(int_val, 69);
auto uint_val = exporter.fromJson(json["uint"])->first.cast<uint>();
ASSERT_EQ(uint_val, 96);
auto real = exporter.fromJson(json["real"])->first.cast<double>();
ASSERT_EQ(real, 3.14);
}
Expand Down

0 comments on commit 067a07b

Please sign in to comment.