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
3444 {
@@ -51,33 +61,41 @@ JsonExporter::ExpectedEntry JsonExporter::fromJson(const nlohmann::json& source)
5161 {
5262 return nonstd::make_unexpected (" json object is null" );
5363 }
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 ))
6465 {
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 ) + " '." );
7467 }
7568
76- if (!source.contains (" __type" ))
69+ const auto source_value_it = source.find (kValueField );
70+ if (source_value_it != source.end ())
7771 {
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+ }
7996 }
80- auto type_it = type_names_.find (source[" __type" ]);
97+
98+ auto type_it = type_names_.find (source[kTypeField ]);
8199 if (type_it == type_names_.end ())
82100 {
83101 return nonstd::make_unexpected (" Type not found in registered list" );
0 commit comments