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 if (type == typeid (std::vector<double >))
34
44
{
@@ -67,61 +77,41 @@ JsonExporter::ExpectedEntry JsonExporter::fromJson(const nlohmann::json& source)
67
77
{
68
78
return Entry{ BT::Any (), BT::TypeInfo::Create<std::nullptr_t >() };
69
79
}
70
-
71
- if (source.is_string ())
72
- {
73
- return Entry{ BT::Any (source.get <std::string>()),
74
- BT::TypeInfo::Create<std::string>() };
75
- }
76
- if (source.is_number_integer ())
77
- {
78
- return Entry{ BT::Any (source.get <int >()), BT::TypeInfo::Create<int >() };
79
- }
80
- if (source.is_number_float ())
80
+ if (!source.contains (kTypeField ))
81
81
{
82
- return Entry{ BT::Any (source.get <double >()), BT::TypeInfo::Create<double >() };
83
- }
84
- if (source.is_boolean ())
85
- {
86
- return Entry{ BT::Any (source.get <bool >()), BT::TypeInfo::Create<bool >() };
82
+ return nonstd::make_unexpected (" Missing field '" + std::string (kTypeField ) + " '." );
87
83
}
88
84
89
- // basic vectors
90
- if (source. is_array () && source.size () > 0 && !source. contains ( " __type " ))
85
+ const auto source_value_it = source. find ( kValueField );
86
+ if (source_value_it != source.end ( ))
91
87
{
92
- auto first_element = source[0 ];
93
- if (first_element.is_string ())
88
+ if (source_value_it->is_string ())
94
89
{
95
- return Entry{ BT::Any (source. get <std::vector<std:: string> >()),
96
- BT::TypeInfo::Create<std::vector<std:: string> >() };
90
+ return Entry{ BT::Any (source_value_it-> get <std::string>()),
91
+ BT::TypeInfo::Create<std::string>() };
97
92
}
98
- if (first_element. is_number_integer ())
93
+ if (source_value_it-> is_number_unsigned ())
99
94
{
100
- return Entry{ BT::Any (source. get <std::vector< int > >()),
101
- BT::TypeInfo::Create<std::vector< int > >() };
95
+ return Entry{ BT::Any (source_value_it-> get <uint64_t >()),
96
+ BT::TypeInfo::Create<uint64_t >() };
102
97
}
103
- if (first_element. is_number_float ())
98
+ if (source_value_it-> is_number_integer ())
104
99
{
105
- return Entry{ BT::Any (source. get <std::vector< double > >()),
106
- BT::TypeInfo::Create<std::vector< double > >() };
100
+ return Entry{ BT::Any (source_value_it-> get <int64_t >()),
101
+ BT::TypeInfo::Create<int64_t >() };
107
102
}
108
- if (first_element. is_boolean ())
103
+ if (source_value_it-> is_number_float ())
109
104
{
110
- return Entry{ BT::Any (source.get <std::vector<bool >>()),
111
- BT::TypeInfo::Create<std::vector<bool >>() };
105
+ return Entry{ BT::Any (source_value_it->get <double >()),
106
+ BT::TypeInfo::Create<double >() };
107
+ }
108
+ if (source_value_it->is_boolean ())
109
+ {
110
+ return Entry{ BT::Any (source_value_it->get <bool >()), BT::TypeInfo::Create<bool >() };
112
111
}
113
112
}
114
113
115
- if (!source.contains (" __type" ) && !source.is_array ())
116
- {
117
- return nonstd::make_unexpected (" Missing field '__type'" );
118
- }
119
-
120
- auto & from_converters =
121
- source.is_array () ? from_json_array_converters_ : from_json_converters_;
122
- auto type_field = source.is_array () ? source[0 ][" __type" ] : source[" __type" ];
123
-
124
- auto type_it = type_names_.find (type_field);
114
+ auto type_it = type_names_.find (source[kTypeField ]);
125
115
if (type_it == type_names_.end ())
126
116
{
127
117
return nonstd::make_unexpected (" Type not found in registered list" );
0 commit comments