-
Notifications
You must be signed in to change notification settings - Fork 819
Expand file tree
/
Copy pathjson_export.cpp
More file actions
122 lines (112 loc) · 3.07 KB
/
json_export.cpp
File metadata and controls
122 lines (112 loc) · 3.07 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
#include "behaviortree_cpp/json_export.h"
namespace
{
constexpr std::string_view kTypeField = "__type";
constexpr std::string_view kValueField = "value";
} // namespace
namespace BT
{
JsonExporter& JsonExporter::get()
{
static JsonExporter global_instance;
return global_instance;
}
bool JsonExporter::toJson(const Any& any, nlohmann::json& dst) const
{
nlohmann::json json;
auto const& type = any.castedType();
if(any.isString())
{
dst[kTypeField] = "string";
dst[kValueField] = any.cast<std::string>();
}
else if(type == typeid(int64_t))
{
dst[kTypeField] = "int64_t";
dst[kValueField] = any.cast<int64_t>();
}
else if(type == typeid(uint64_t))
{
dst[kTypeField] = "uint64_t";
dst[kValueField] = any.cast<uint64_t>();
}
else if(type == typeid(double))
{
dst[kTypeField] = "double";
dst[kValueField] = any.cast<double>();
}
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 nonstd::make_unexpected("json object is null");
}
if(!source.contains(kTypeField))
{
return nonstd::make_unexpected("Missing field '" + std::string(kTypeField) + "'.");
}
const auto source_value_it = source.find(kValueField);
if(source_value_it != source.end())
{
if(source_value_it->is_string())
{
return Entry{ BT::Any(source_value_it->get<std::string>()),
BT::TypeInfo::Create<std::string>() };
}
if(source_value_it->is_number_unsigned())
{
return Entry{ BT::Any(source_value_it->get<uint64_t>()),
BT::TypeInfo::Create<uint64_t>() };
}
if(source_value_it->is_number_integer())
{
return Entry{ BT::Any(source_value_it->get<int64_t>()),
BT::TypeInfo::Create<int64_t>() };
}
if(source_value_it->is_number_float())
{
return Entry{ BT::Any(source_value_it->get<double>()),
BT::TypeInfo::Create<double>() };
}
if(source_value_it->is_boolean())
{
return Entry{ BT::Any(source_value_it->get<bool>()), BT::TypeInfo::Create<bool>() };
}
}
auto type_it = type_names_.find(source[kTypeField]);
if(type_it == type_names_.end())
{
return nonstd::make_unexpected("Type not found in registered list");
}
auto func_it = from_json_converters_.find(type_it->second.type());
if(func_it == from_json_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