@@ -11,7 +11,7 @@ namespace BT
11
11
{
12
12
13
13
/* *
14
- * To add new type to the JSON library, you should follow these isntructions :
14
+ * To add new type to the JSON library, you should follow these instructions :
15
15
* https://json.nlohmann.me/features/arbitrary_types/
16
16
*
17
17
* Considering for instance the type:
@@ -105,6 +105,7 @@ class JsonExporter
105
105
106
106
std::unordered_map<std::type_index, ToJonConverter> to_json_converters_;
107
107
std::unordered_map<std::type_index, FromJonConverter> from_json_converters_;
108
+ std::unordered_map<std::type_index, FromJonConverter> from_json_array_converters_;
108
109
std::unordered_map<std::string, BT::TypeInfo> type_names_;
109
110
};
110
111
@@ -129,6 +130,15 @@ inline Expected<T> JsonExporter::fromJson(const nlohmann::json& source) const
129
130
template <typename T>
130
131
inline void JsonExporter::addConverter ()
131
132
{
133
+ // we need to get the name of the type
134
+ nlohmann::json const js = T{};
135
+ // we insert both the name obtained from JSON and demangle
136
+ if (js.contains (" __type" ))
137
+ {
138
+ type_names_.insert ({ std::string (js[" __type" ]), BT::TypeInfo::Create<T>() });
139
+ }
140
+ type_names_.insert ({ BT::demangle (typeid (T)), BT::TypeInfo::Create<T>() });
141
+
132
142
ToJonConverter to_converter = [](const BT::Any& entry, nlohmann::json& dst) {
133
143
dst = *const_cast <BT::Any&>(entry).castPtr <T>();
134
144
};
@@ -139,16 +149,23 @@ inline void JsonExporter::addConverter()
139
149
return { BT::Any (value), BT::TypeInfo::Create<T>() };
140
150
};
141
151
142
- // we need to get the name of the type
143
- nlohmann::json const js = T{};
144
- // we insert both the name obtained from JSON and demangle
145
- if (js.contains (" __type" ))
146
- {
147
- type_names_.insert ({ std::string (js[" __type" ]), BT::TypeInfo::Create<T>() });
148
- }
149
- type_names_.insert ({ BT::demangle (typeid (T)), BT::TypeInfo::Create<T>() });
150
-
151
152
from_json_converters_.insert ({ typeid (T), from_converter });
153
+
154
+ // ---- include vectors of T
155
+ ToJonConverter to_array_converter = [](const BT::Any& entry, nlohmann::json& dst) {
156
+ dst = *const_cast <BT::Any&>(entry).castPtr <std::vector<T>>();
157
+ };
158
+ to_json_converters_.insert ({ typeid (std::vector<T>), to_array_converter });
159
+
160
+ FromJonConverter from_array_converter = [](const nlohmann::json& src) -> Entry {
161
+ std::vector<T> value;
162
+ for (const auto & item : src)
163
+ {
164
+ value.push_back (item.get <T>());
165
+ }
166
+ return { BT::Any (value), BT::TypeInfo::Create<std::vector<T>>() };
167
+ };
168
+ from_json_array_converters_.insert ({ typeid (T), from_array_converter });
152
169
}
153
170
154
171
template <typename T>
@@ -163,6 +180,18 @@ inline void JsonExporter::addConverter(
163
180
}
164
181
};
165
182
to_json_converters_.insert ({ typeid (T), std::move (converter) });
183
+ // ---------------------------------------------
184
+ // add the vector<T> converter
185
+ auto vector_converter = [converter](const BT::Any& entry, nlohmann::json& json) {
186
+ auto & vec = *const_cast <BT::Any&>(entry).castPtr <std::vector<T>>();
187
+ for (const auto & item : vec)
188
+ {
189
+ nlohmann::json item_json;
190
+ converter (BT::Any (item), item_json);
191
+ json.push_back (item_json);
192
+ }
193
+ };
194
+ to_json_converters_.insert ({ typeid (std::vector<T>), std::move (vector_converter) });
166
195
}
167
196
168
197
template <typename T>
@@ -176,6 +205,19 @@ JsonExporter::addConverter(std::function<void(const nlohmann::json&, T&)> func)
176
205
};
177
206
type_names_.insert ({ BT::demangle (typeid (T)), BT::TypeInfo::Create<T>() });
178
207
from_json_converters_.insert ({ typeid (T), std::move (converter) });
208
+ // ---------------------------------------------
209
+ // add the vector<T> converter
210
+ auto vector_converter = [func](const nlohmann::json& json) -> Entry {
211
+ std::vector<T> tmp;
212
+ for (const auto & item : json)
213
+ {
214
+ T item_tmp;
215
+ func (item, item_tmp);
216
+ tmp.push_back (item_tmp);
217
+ }
218
+ return { BT::Any (tmp), BT::TypeInfo::Create<std::vector<T>>() };
219
+ };
220
+ from_json_array_converters_.insert ({ typeid (T), std::move (vector_converter) });
179
221
}
180
222
181
223
template <typename T>
0 commit comments