Skip to content

Commit aa1cb6b

Browse files
committed
Revert "extend JSON convertion to include vectors (BehaviorTree#965)"
This reverts commit 0ee319b.
1 parent 8d47d39 commit aa1cb6b

File tree

5 files changed

+60
-231
lines changed

5 files changed

+60
-231
lines changed

include/behaviortree_cpp/json_export.h

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ class JsonExporter
118118

119119
std::unordered_map<std::type_index, ToJonConverter> to_json_converters_;
120120
std::unordered_map<std::type_index, FromJonConverter> from_json_converters_;
121-
std::unordered_map<std::type_index, FromJonConverter> from_json_array_converters_;
122121
std::unordered_map<std::string, BT::TypeInfo> type_names_;
123122
};
124123

@@ -143,15 +142,6 @@ inline Expected<T> JsonExporter::fromJson(const nlohmann::json& source) const
143142
template <typename T>
144143
inline void JsonExporter::addConverter()
145144
{
146-
// we need to get the name of the type
147-
nlohmann::json const js = T{};
148-
// we insert both the name obtained from JSON and demangle
149-
if(js.contains("__type"))
150-
{
151-
type_names_.insert({ std::string(js["__type"]), BT::TypeInfo::Create<T>() });
152-
}
153-
type_names_.insert({ BT::demangle(typeid(T)), BT::TypeInfo::Create<T>() });
154-
155145
ToJonConverter to_converter = [](const BT::Any& entry, nlohmann::json& dst) {
156146
dst = *const_cast<BT::Any&>(entry).castPtr<T>();
157147
};
@@ -162,23 +152,16 @@ inline void JsonExporter::addConverter()
162152
return { BT::Any(value), BT::TypeInfo::Create<T>() };
163153
};
164154

165-
from_json_converters_.insert({ typeid(T), from_converter });
166-
167-
//---- include vectors of T
168-
ToJonConverter to_array_converter = [](const BT::Any& entry, nlohmann::json& dst) {
169-
dst = *const_cast<BT::Any&>(entry).castPtr<std::vector<T>>();
170-
};
171-
to_json_converters_.insert({ typeid(std::vector<T>), to_array_converter });
155+
// we need to get the name of the type
156+
nlohmann::json const js = T{};
157+
// we insert both the name obtained from JSON and demangle
158+
if(js.contains("__type"))
159+
{
160+
type_names_.insert({ std::string(js["__type"]), BT::TypeInfo::Create<T>() });
161+
}
162+
type_names_.insert({ BT::demangle(typeid(T)), BT::TypeInfo::Create<T>() });
172163

173-
FromJonConverter from_array_converter = [](const nlohmann::json& src) -> Entry {
174-
std::vector<T> value;
175-
for(const auto& item : src)
176-
{
177-
value.push_back(item.get<T>());
178-
}
179-
return { BT::Any(value), BT::TypeInfo::Create<std::vector<T>>() };
180-
};
181-
from_json_array_converters_.insert({ typeid(T), from_array_converter });
164+
from_json_converters_.insert({ typeid(T), from_converter });
182165
}
183166

184167
template <typename T>
@@ -193,18 +176,6 @@ inline void JsonExporter::addConverter(
193176
}
194177
};
195178
to_json_converters_.insert({ typeid(T), std::move(converter) });
196-
//---------------------------------------------
197-
// add the vector<T> converter
198-
auto vector_converter = [converter](const BT::Any& entry, nlohmann::json& json) {
199-
auto& vec = *const_cast<BT::Any&>(entry).castPtr<std::vector<T>>();
200-
for(const auto& item : vec)
201-
{
202-
nlohmann::json item_json;
203-
converter(BT::Any(item), item_json);
204-
json.push_back(item_json);
205-
}
206-
};
207-
to_json_converters_.insert({ typeid(std::vector<T>), std::move(vector_converter) });
208179
}
209180

210181
template <typename T>
@@ -218,19 +189,6 @@ JsonExporter::addConverter(std::function<void(const nlohmann::json&, T&)> func)
218189
};
219190
type_names_.insert({ BT::demangle(typeid(T)), BT::TypeInfo::Create<T>() });
220191
from_json_converters_.insert({ typeid(T), std::move(converter) });
221-
//---------------------------------------------
222-
// add the vector<T> converter
223-
auto vector_converter = [func](const nlohmann::json& json) -> Entry {
224-
std::vector<T> tmp;
225-
for(const auto& item : json)
226-
{
227-
T item_tmp;
228-
func(item, item_tmp);
229-
tmp.push_back(item_tmp);
230-
}
231-
return { BT::Any(tmp), BT::TypeInfo::Create<std::vector<T>>() };
232-
};
233-
from_json_array_converters_.insert({ typeid(T), std::move(vector_converter) });
234192
}
235193

236194
template <typename T>

include/behaviortree_cpp/utils/safe_any.hpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,32 @@ class Any
162162
[[nodiscard]] T* castPtr()
163163
{
164164
static_assert(!std::is_same_v<T, float>, "The value has been casted internally to "
165-
"[double]. Use that instead");
165+
"[double]. "
166+
"Use that instead");
167+
static_assert(!SafeAny::details::is_integer<T>() || std::is_same_v<T, uint64_t>, "The"
168+
" va"
169+
"lue"
170+
" ha"
171+
"s "
172+
"bee"
173+
"n "
174+
"cas"
175+
"ted"
176+
" in"
177+
"ter"
178+
"nal"
179+
"ly "
180+
"to "
181+
"[in"
182+
"t64"
183+
"_t]"
184+
". "
185+
"Use"
186+
" th"
187+
"at "
188+
"ins"
189+
"tea"
190+
"d");
166191

167192
return _any.empty() ? nullptr : linb::any_cast<T>(&_any);
168193
}

src/bt_factory.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <filesystem>
1414
#include "behaviortree_cpp/bt_factory.h"
1515
#include "behaviortree_cpp/utils/shared_library.h"
16+
#include "behaviortree_cpp/contrib/json.hpp"
1617
#include "behaviortree_cpp/xml_parsing.h"
1718
#include "wildcards/wildcards.hpp"
1819

src/json_export.cpp

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,6 @@ bool JsonExporter::toJson(const Any& any, nlohmann::json& dst) const
3030
{
3131
dst = any.cast<double>();
3232
}
33-
else if(type == typeid(std::vector<double>))
34-
{
35-
dst = any.cast<std::vector<double>>();
36-
}
37-
else if(type == typeid(std::vector<int>))
38-
{
39-
dst = any.cast<std::vector<int>>();
40-
}
41-
else if(type == typeid(std::vector<std::string>))
42-
{
43-
dst = any.cast<std::vector<std::string>>();
44-
}
45-
else if(type == typeid(std::vector<bool>))
46-
{
47-
dst = any.cast<std::vector<bool>>();
48-
}
4933
else
5034
{
5135
auto it = to_json_converters_.find(type);
@@ -65,17 +49,20 @@ JsonExporter::ExpectedEntry JsonExporter::fromJson(const nlohmann::json& source)
6549
{
6650
if(source.is_null())
6751
{
68-
return Entry{ BT::Any(), BT::TypeInfo::Create<std::nullptr_t>() };
52+
return nonstd::make_unexpected("json object is null");
6953
}
70-
7154
if(source.is_string())
7255
{
7356
return Entry{ BT::Any(source.get<std::string>()),
7457
BT::TypeInfo::Create<std::string>() };
7558
}
59+
if(source.is_number_unsigned())
60+
{
61+
return Entry{ BT::Any(source.get<uint64_t>()), BT::TypeInfo::Create<uint64_t>() };
62+
}
7663
if(source.is_number_integer())
7764
{
78-
return Entry{ BT::Any(source.get<int>()), BT::TypeInfo::Create<int>() };
65+
return Entry{ BT::Any(source.get<int64_t>()), BT::TypeInfo::Create<int64_t>() };
7966
}
8067
if(source.is_number_float())
8168
{
@@ -86,48 +73,17 @@ JsonExporter::ExpectedEntry JsonExporter::fromJson(const nlohmann::json& source)
8673
return Entry{ BT::Any(source.get<bool>()), BT::TypeInfo::Create<bool>() };
8774
}
8875

89-
// basic vectors
90-
if(source.is_array() && source.size() > 0 && !source.contains("__type"))
91-
{
92-
auto first_element = source[0];
93-
if(first_element.is_string())
94-
{
95-
return Entry{ BT::Any(source.get<std::vector<std::string>>()),
96-
BT::TypeInfo::Create<std::vector<std::string>>() };
97-
}
98-
if(first_element.is_number_integer())
99-
{
100-
return Entry{ BT::Any(source.get<std::vector<int>>()),
101-
BT::TypeInfo::Create<std::vector<int>>() };
102-
}
103-
if(first_element.is_number_float())
104-
{
105-
return Entry{ BT::Any(source.get<std::vector<double>>()),
106-
BT::TypeInfo::Create<std::vector<double>>() };
107-
}
108-
if(first_element.is_boolean())
109-
{
110-
return Entry{ BT::Any(source.get<std::vector<bool>>()),
111-
BT::TypeInfo::Create<std::vector<bool>>() };
112-
}
113-
}
114-
115-
if(!source.contains("__type") && !source.is_array())
76+
if(!source.contains("__type"))
11677
{
11778
return nonstd::make_unexpected("Missing field '__type'");
11879
}
119-
120-
auto& from_converters =
121-
source.is_array() ? from_json_array_converters_ : from_json_converters_;
122-
auto type_field = source.is_array() ? source[0]["__type"] : source["__type"];
123-
124-
auto type_it = type_names_.find(type_field);
80+
auto type_it = type_names_.find(source["__type"]);
12581
if(type_it == type_names_.end())
12682
{
12783
return nonstd::make_unexpected("Type not found in registered list");
12884
}
129-
auto func_it = from_converters.find(type_it->second.type());
130-
if(func_it == from_converters.end())
85+
auto func_it = from_json_converters_.find(type_it->second.type());
86+
if(func_it == from_json_converters_.end())
13187
{
13288
return nonstd::make_unexpected("Type not found in registered list");
13389
}

0 commit comments

Comments
 (0)