11#include " behaviortree_cpp/json_export.h"
22
3+ namespace
4+ {
5+ constexpr std::string_view kTypeField = " __type" ;
6+ constexpr std::string_view kValueField = " value" ;
7+ } // namespace
8+
39namespace BT
410{
511
@@ -16,19 +22,23 @@ bool JsonExporter::toJson(const Any& any, nlohmann::json& dst) const
1622
1723 if (any.isString ())
1824 {
19- dst = any.cast <std::string>();
25+ dst[kTypeField ] = " string" ;
26+ dst[kValueField ] = any.cast <std::string>();
2027 }
2128 else if (type == typeid (int64_t ))
2229 {
23- dst = any.cast <int64_t >();
30+ dst[kTypeField ] = " int64_t" ;
31+ dst[kValueField ] = any.cast <int64_t >();
2432 }
2533 else if (type == typeid (uint64_t ))
2634 {
27- dst = any.cast <uint64_t >();
35+ dst[kTypeField ] = " uint64_t" ;
36+ dst[kValueField ] = any.cast <uint64_t >();
2837 }
2938 else if (type == typeid (double ))
3039 {
31- dst = any.cast <double >();
40+ dst[kTypeField ] = " double" ;
41+ dst[kValueField ] = any.cast <double >();
3242 }
3343 else if (type == typeid (std::vector<double >))
3444 {
@@ -67,61 +77,41 @@ JsonExporter::ExpectedEntry JsonExporter::fromJson(const nlohmann::json& source)
6777 {
6878 return Entry{ BT::Any (), BT::TypeInfo::Create<std::nullptr_t >() };
6979 }
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 ))
8181 {
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 ) + " '." );
8783 }
8884
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 ( ))
9187 {
92- auto first_element = source[0 ];
93- if (first_element.is_string ())
88+ if (source_value_it->is_string ())
9489 {
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>() };
9792 }
98- if (first_element. is_number_integer ())
93+ if (source_value_it-> is_number_unsigned ())
9994 {
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 >() };
10297 }
103- if (first_element. is_number_float ())
98+ if (source_value_it-> is_number_integer ())
10499 {
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 >() };
107102 }
108- if (first_element. is_boolean ())
103+ if (source_value_it-> is_number_float ())
109104 {
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 >() };
112111 }
113112 }
114113
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 ]);
125115 if (type_it == type_names_.end ())
126116 {
127117 return nonstd::make_unexpected (" Type not found in registered list" );
0 commit comments