gtest_json.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
5 
6 //----------- Custom types ----------
7 
8 namespace TestTypes
9 {
10 
11 struct Vector3D
12 {
13  double x = 0;
14  double y = 0;
15  double z = 0;
16 };
17 
19 {
20  double w = 1;
21  double x = 0;
22  double y = 0;
23  double z = 0;
24 };
25 
26 struct Pose3D
27 {
30 };
31 
32 struct Time
33 {
34  uint32_t sec;
35  uint32_t nsec;
36 };
37 
39 {
40  add_field("x", &v.x);
41  add_field("y", &v.y);
42  add_field("z", &v.z);
43 }
44 
46 {
47  add_field("w", &v.w);
48  add_field("x", &v.x);
49  add_field("y", &v.y);
50  add_field("z", &v.z);
51 }
52 
54 {
55  add_field("pos", &v.pos);
56  add_field("rot", &v.rot);
57 }
58 
59 // specialized functions
60 void jsonFromTime(const Time& t, nlohmann::json& j)
61 {
62  j["stamp"] = double(t.sec) + 1e-9 * double(t.nsec);
63 }
64 
65 void jsonToTime(const nlohmann::json& j, Time& t)
66 {
67  double sec = j["stamp"];
68  t.sec = int(sec);
69  t.nsec = (sec - t.sec) * 1e9;
70 }
71 
72 } // namespace TestTypes
73 
74 //----------- JSON specialization ----------
75 
76 class JsonTest : public testing::Test
77 {
78 protected:
80  {
82  exporter.addConverter<TestTypes::Pose3D>();
85 
88  }
89 };
90 
91 TEST_F(JsonTest, TwoWaysConversion)
92 {
94 
95  TestTypes::Pose3D pose = { { 1, 2, 3 }, { 4, 5, 6, 7 } };
96 
98  exporter.toJson(BT::Any(69), json["int"]);
99  exporter.toJson(BT::Any(3.14), json["real"]);
100  exporter.toJson(BT::Any(pose), json["pose"]);
101 
102  std::cout << json.dump(2) << std::endl;
103 
104  ASSERT_EQ(json["int"], 69);
105  ASSERT_EQ(json["real"], 3.14);
106 
107  ASSERT_EQ(json["pose"]["__type"], "Pose3D");
108  ASSERT_EQ(json["pose"]["pos"]["x"], 1);
109  ASSERT_EQ(json["pose"]["pos"]["y"], 2);
110  ASSERT_EQ(json["pose"]["pos"]["z"], 3);
111 
112  ASSERT_EQ(json["pose"]["rot"]["w"], 4);
113  ASSERT_EQ(json["pose"]["rot"]["x"], 5);
114  ASSERT_EQ(json["pose"]["rot"]["y"], 6);
115  ASSERT_EQ(json["pose"]["rot"]["z"], 7);
116 
117  // check the two-ways transform, i.e. "from_json"
118  auto pose2 = exporter.fromJson(json["pose"])->first.cast<TestTypes::Pose3D>();
119 
120  ASSERT_EQ(pose.pos.x, pose2.pos.x);
121  ASSERT_EQ(pose.pos.y, pose2.pos.y);
122  ASSERT_EQ(pose.pos.z, pose2.pos.z);
123 
124  ASSERT_EQ(pose.rot.w, pose2.rot.w);
125  ASSERT_EQ(pose.rot.x, pose2.rot.x);
126  ASSERT_EQ(pose.rot.y, pose2.rot.y);
127  ASSERT_EQ(pose.rot.z, pose2.rot.z);
128 
129  auto num = exporter.fromJson(json["int"])->first.cast<int>();
130  ASSERT_EQ(num, 69);
131  auto real = exporter.fromJson(json["real"])->first.cast<double>();
132  ASSERT_EQ(real, 3.14);
133 }
134 
135 TEST_F(JsonTest, CustomTime)
136 {
138 
139  TestTypes::Time stamp = { 3, 8000000 };
141  exporter.toJson(BT::Any(stamp), json);
142  std::cout << json.dump() << std::endl;
143 
144  {
145  auto res = exporter.fromJson(json, typeid(TestTypes::Time));
146  ASSERT_TRUE(res);
147  auto stamp_out = res->first.cast<TestTypes::Time>();
148  ASSERT_EQ(stamp.sec, stamp_out.sec);
149  ASSERT_EQ(stamp.nsec, stamp_out.nsec);
150  }
151  {
152  auto res = exporter.fromJson(json);
153  ASSERT_TRUE(res);
154  auto stamp_out = res->first.cast<TestTypes::Time>();
155  ASSERT_EQ(stamp.sec, stamp_out.sec);
156  ASSERT_EQ(stamp.nsec, stamp_out.nsec);
157  }
158  {
159  auto stamp_out = exporter.fromJson<TestTypes::Time>(json);
160  ASSERT_TRUE(stamp_out);
161  ASSERT_EQ(stamp.sec, stamp_out->sec);
162  ASSERT_EQ(stamp.nsec, stamp_out->nsec);
163  }
164 }
165 
166 TEST_F(JsonTest, ConvertFromString)
167 {
168  TestTypes::Vector3D vect;
169  auto const test_json = R"(json:{"x":2.1, "y":4.2, "z":6.3})";
170  ASSERT_NO_THROW(vect = BT::convertFromString<TestTypes::Vector3D>(test_json));
171 
172  ASSERT_EQ(vect.x, 2.1);
173  ASSERT_EQ(vect.y, 4.2);
174  ASSERT_EQ(vect.z, 6.3);
175 }
176 
177 TEST_F(JsonTest, BlackboardInOut)
178 {
179  auto bb_in = BT::Blackboard::create();
180  bb_in->set("int", 42);
181  bb_in->set("real", 3.14);
182  bb_in->set("vect", TestTypes::Vector3D{ 1.1, 2.2, 3.3 });
183 
184  auto json = ExportBlackboardToJSON(*bb_in);
185  std::cout << json.dump(2) << std::endl;
186 
187  auto bb_out = BT::Blackboard::create();
188  ImportBlackboardFromJSON(json, *bb_out);
189 
190  ASSERT_EQ(bb_out->get<int>("int"), 42);
191  ASSERT_EQ(bb_out->get<double>("real"), 3.14);
192 
193  auto vect_out = bb_out->get<TestTypes::Vector3D>("vect");
194  ASSERT_EQ(vect_out.x, 1.1);
195  ASSERT_EQ(vect_out.y, 2.2);
196  ASSERT_EQ(vect_out.z, 3.3);
197 }
TestTypes::Quaternion3D::y
double y
Definition: gtest_json.cpp:22
BT::JsonExporter::toJson
bool toJson(const BT::Any &any, nlohmann::json &destination) const
toJson adds the content of "any" to the JSON "destination".
Definition: json_export.cpp:12
BT::Any
Definition: safe_any.hpp:36
TestTypes::Time
Definition: gtest_json.cpp:32
TestTypes::Vector3D
Definition: gtest_json.cpp:11
basic_types.h
TestTypes::BT_JSON_CONVERTER
BT_JSON_CONVERTER(Vector3D, v)
Definition: gtest_json.cpp:38
BT::ImportBlackboardFromJSON
void ImportBlackboardFromJSON(const nlohmann::json &json, Blackboard &blackboard)
ImportBlackboardFromJSON will append elements to the blackboard, using the values parsed from the JSO...
Definition: blackboard.cpp:280
TestTypes::Pose3D
Definition: gtest_json.cpp:26
basic_json
namespace for Niels Lohmann
Definition: json.hpp:3411
TestTypes::Vector3D::z
double z
Definition: gtest_json.cpp:15
BT::ExportBlackboardToJSON
nlohmann::json ExportBlackboardToJSON(const Blackboard &blackboard)
ExportBlackboardToJSON will create a JSON that contains the current values of the blackboard....
Definition: blackboard.cpp:263
TEST_F
TEST_F(JsonTest, TwoWaysConversion)
Definition: gtest_json.cpp:91
BT::JsonExporter
Definition: json_export.h:49
json_export.h
TestTypes::Pose3D::rot
Quaternion3D rot
Definition: gtest_json.cpp:29
BT::JsonExporter::get
static JsonExporter & get()
Definition: json_export.cpp:6
TestTypes::Time::sec
uint32_t sec
Definition: gtest_json.cpp:34
TestTypes::jsonFromTime
void jsonFromTime(const Time &t, nlohmann::json &j)
Definition: gtest_json.cpp:60
TestTypes::Vector3D::x
double x
Definition: gtest_json.cpp:13
BT::Blackboard::create
static Blackboard::Ptr create(Blackboard::Ptr parent={})
Definition: blackboard.h:63
JsonTest
Definition: gtest_json.cpp:76
TestTypes::Time::nsec
uint32_t nsec
Definition: gtest_json.cpp:35
json
basic_json<> json
default specialization
Definition: json.hpp:3422
TestTypes
Definition: gtest_json.cpp:8
TestTypes::jsonToTime
void jsonToTime(const nlohmann::json &j, Time &t)
Definition: gtest_json.cpp:65
TestTypes::Pose3D::pos
Vector3D pos
Definition: gtest_json.cpp:28
JsonTest::JsonTest
JsonTest()
Definition: gtest_json.cpp:79
BT::JsonExporter::addConverter
void addConverter()
Definition: json_export.h:130
basic_json::dump
string_t dump(const int indent=-1, const char indent_char=' ', const bool ensure_ascii=false, const error_handler_t error_handler=error_handler_t::strict) const
serialization
Definition: json.hpp:20574
BT::JsonExporter::fromJson
ExpectedEntry fromJson(const nlohmann::json &source) const
fromJson will return an Entry (value wrappedn in Any + TypeInfo) from a json source....
Definition: json_export.cpp:48
TestTypes::Quaternion3D
Definition: gtest_json.cpp:18
blackboard.h
TestTypes::Quaternion3D::w
double w
Definition: gtest_json.cpp:20
TestTypes::Quaternion3D::z
double z
Definition: gtest_json.cpp:23
TestTypes::Quaternion3D::x
double x
Definition: gtest_json.cpp:21
TestTypes::Vector3D::y
double y
Definition: gtest_json.cpp:14


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:16