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
- {
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" ))
72
65
{
73
- return Entry{ BT::Any (source. get < bool >()), BT::TypeInfo::Create< bool >() } ;
66
+ return nonstd::make_unexpected ( " Missing field '__type' " ) ;
74
67
}
75
68
76
- if (! source.contains (" __type " ))
69
+ if (source.contains (" value " ))
77
70
{
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
+ }
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