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- {
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 ())
64+ if (!source.contains (" __type" ))
7265 {
73- return Entry{ BT::Any (source. get < bool >()), BT::TypeInfo::Create< bool >() } ;
66+ return nonstd::make_unexpected ( " Missing field '__type' " ) ;
7467 }
7568
76- if (! source.contains (" __type " ))
69+ if (source.contains (" value " ))
7770 {
78- return nonstd::make_unexpected (" Missing field '__type'" );
71+ if (source[kValueField ].is_string ())
72+ {
73+ return Entry{ BT::Any (source[kValueField ].get <std::string>()),
74+ BT::TypeInfo::Create<std::string>() };
75+ }
76+ if (source[kValueField ].is_number_unsigned ())
77+ {
78+ return Entry{ BT::Any (source[kValueField ].get <uint64_t >()),
79+ BT::TypeInfo::Create<uint64_t >() };
80+ }
81+ if (source[kValueField ].is_number_integer ())
82+ {
83+ return Entry{ BT::Any (source[kValueField ].get <int64_t >()),
84+ BT::TypeInfo::Create<int64_t >() };
85+ }
86+ if (source[kValueField ].is_number_float ())
87+ {
88+ return Entry{ BT::Any (source[kValueField ].get <double >()),
89+ BT::TypeInfo::Create<double >() };
90+ }
91+ if (source[kValueField ].is_boolean ())
92+ {
93+ return Entry{ BT::Any (source[kValueField ].get <bool >()),
94+ 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