1
1
#include " behaviortree_cpp/json_export.h"
2
2
3
+ namespace
4
+ {
5
+ constexpr std::string_view kTypeField = " __type" ;
6
+ constexpr std::string_view kValueField = " value" ;
7
+ } // namespace
8
+
3
9
namespace BT
4
10
{
5
11
@@ -16,19 +22,23 @@ bool JsonExporter::toJson(const Any& any, nlohmann::json& dst) const
16
22
17
23
if (any.isString ())
18
24
{
19
- dst = any.cast <std::string>();
25
+ dst[kTypeField ] = " string" ;
26
+ dst[kValueField ] = any.cast <std::string>();
20
27
}
21
28
else if (type == typeid (int64_t ))
22
29
{
23
- dst = any.cast <int64_t >();
30
+ dst[kTypeField ] = " int64_t" ;
31
+ dst[kValueField ] = any.cast <int64_t >();
24
32
}
25
33
else if (type == typeid (uint64_t ))
26
34
{
27
- dst = any.cast <uint64_t >();
35
+ dst[kTypeField ] = " uint64_t" ;
36
+ dst[kValueField ] = any.cast <uint64_t >();
28
37
}
29
38
else if (type == typeid (double ))
30
39
{
31
- dst = any.cast <double >();
40
+ dst[kTypeField ] = " double" ;
41
+ dst[kValueField ] = any.cast <double >();
32
42
}
33
43
else
34
44
{
@@ -51,33 +61,41 @@ JsonExporter::ExpectedEntry JsonExporter::fromJson(const nlohmann::json& source)
51
61
{
52
62
return nonstd::make_unexpected (" json object is null" );
53
63
}
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
+ if (!source.contains (kTypeField ))
64
65
{
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 ())
72
- {
73
- return Entry{ BT::Any (source.get <bool >()), BT::TypeInfo::Create<bool >() };
66
+ return nonstd::make_unexpected (" Missing field '" + std::string (kTypeField ) + " '." );
74
67
}
75
68
76
- if (!source.contains (" __type" ))
69
+ const auto source_value_it = source.find (kValueField );
70
+ if (source_value_it != source.end ())
77
71
{
78
- return nonstd::make_unexpected (" Missing field '__type'" );
72
+ if (source_value_it->is_string ())
73
+ {
74
+ return Entry{ BT::Any (source_value_it->get <std::string>()),
75
+ BT::TypeInfo::Create<std::string>() };
76
+ }
77
+ if (source_value_it->is_number_unsigned ())
78
+ {
79
+ return Entry{ BT::Any (source_value_it->get <uint64_t >()),
80
+ BT::TypeInfo::Create<uint64_t >() };
81
+ }
82
+ if (source_value_it->is_number_integer ())
83
+ {
84
+ return Entry{ BT::Any (source_value_it->get <int64_t >()),
85
+ BT::TypeInfo::Create<int64_t >() };
86
+ }
87
+ if (source_value_it->is_number_float ())
88
+ {
89
+ return Entry{ BT::Any (source_value_it->get <double >()),
90
+ BT::TypeInfo::Create<double >() };
91
+ }
92
+ if (source_value_it->is_boolean ())
93
+ {
94
+ return Entry{ BT::Any (source_value_it->get <bool >()), BT::TypeInfo::Create<bool >() };
95
+ }
79
96
}
80
- auto type_it = type_names_.find (source[" __type" ]);
97
+
98
+ auto type_it = type_names_.find (source[kTypeField ]);
81
99
if (type_it == type_names_.end ())
82
100
{
83
101
return nonstd::make_unexpected (" Type not found in registered list" );
0 commit comments