forked from BehaviorTree/BehaviorTree.CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_export.cpp
More file actions
158 lines (146 loc) · 4.13 KB
/
json_export.cpp
File metadata and controls
158 lines (146 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "behaviortree_cpp/json_export.h"
namespace BT
{
JsonExporter& JsonExporter::get()
{
static JsonExporter global_instance;
return global_instance;
}
bool JsonExporter::toJson(const Any& any, nlohmann::json& dst) const
{
auto const& type = any.castedType();
if(any.isString())
{
dst = any.cast<std::string>();
}
else if(type == typeid(int64_t))
{
dst = any.cast<int64_t>();
}
else if(type == typeid(uint64_t))
{
dst = any.cast<uint64_t>();
}
else if(type == typeid(double))
{
dst = any.cast<double>();
}
else if(type == typeid(std::vector<double>))
{
dst = any.cast<std::vector<double>>();
}
else if(type == typeid(std::vector<int>))
{
dst = any.cast<std::vector<int>>();
}
else if(type == typeid(std::vector<std::string>))
{
dst = any.cast<std::vector<std::string>>();
}
else if(type == typeid(std::vector<bool>))
{
dst = any.cast<std::vector<bool>>();
}
else
{
auto it = to_json_converters_.find(type);
if(it != to_json_converters_.end())
{
it->second(any, dst);
}
else
{
return false;
}
}
return true;
}
JsonExporter::ExpectedEntry JsonExporter::fromJson(const nlohmann::json& source) const
{
if(source.is_null())
{
return Entry{ BT::Any(), BT::TypeInfo::Create<std::nullptr_t>() };
}
if(source.is_string())
{
return Entry{ BT::Any(source.get<std::string>()),
BT::TypeInfo::Create<std::string>() };
}
if(source.is_number_integer())
{
return Entry{ BT::Any(source.get<int>()), BT::TypeInfo::Create<int>() };
}
if(source.is_number_float())
{
return Entry{ BT::Any(source.get<double>()), BT::TypeInfo::Create<double>() };
}
if(source.is_boolean())
{
return Entry{ BT::Any(source.get<bool>()), BT::TypeInfo::Create<bool>() };
}
// basic vectors
if(source.is_array() && source.size() > 0 && !source.contains("__type"))
{
const auto first_element = source[0];
if(first_element.is_string())
{
return Entry{ BT::Any(source.get<std::vector<std::string>>()),
BT::TypeInfo::Create<std::vector<std::string>>() };
}
if(first_element.is_number_integer())
{
return Entry{ BT::Any(source.get<std::vector<int>>()),
BT::TypeInfo::Create<std::vector<int>>() };
}
if(first_element.is_number_float())
{
return Entry{ BT::Any(source.get<std::vector<double>>()),
BT::TypeInfo::Create<std::vector<double>>() };
}
if(first_element.is_boolean())
{
return Entry{ BT::Any(source.get<std::vector<bool>>()),
BT::TypeInfo::Create<std::vector<bool>>() };
}
}
if(source.is_array())
{
if(source.empty())
return nonstd::make_unexpected("Missing field '__type'");
const auto& first = source[0];
if(!first.is_object() || !first.contains("__type"))
return nonstd::make_unexpected("Missing field '__type'");
if(!first["__type"].is_string())
return nonstd::make_unexpected("Invalid '__type' (must be string)");
}
else
{
if(!source.is_object() || !source.contains("__type") || !source["__type"].is_string())
return nonstd::make_unexpected("Missing field '__type'");
}
auto& from_converters =
source.is_array() ? from_json_array_converters_ : from_json_converters_;
auto type_field = source.is_array() ? source[0]["__type"] : source["__type"];
auto type_it = type_names_.find(type_field);
if(type_it == type_names_.end())
{
return nonstd::make_unexpected("Type not found in registered list");
}
auto func_it = from_converters.find(type_it->second.type());
if(func_it == from_converters.end())
{
return nonstd::make_unexpected("Type not found in registered list");
}
return func_it->second(source);
}
JsonExporter::ExpectedEntry JsonExporter::fromJson(const nlohmann::json& source,
std::type_index type) const
{
auto func_it = from_json_converters_.find(type);
if(func_it == from_json_converters_.end())
{
return nonstd::make_unexpected("Type not found in registered list");
}
return func_it->second(source);
}
} // namespace BT