5 #include <SimoxUtility/json.h>
6 #include <SimoxUtility/meta/type_name.h>
8 #include <RobotAPI/interface/ArViz/Elements.h>
15 void to_json(nlohmann::json& j,
const Element& element);
17 void from_json(
const nlohmann::json& j, Element& element);
20 void to_json(nlohmann::json& j,
const ElementPtr& elementPtr);
22 void from_json(
const nlohmann::json& j, ElementPtr& elementPtr);
25 void to_json(nlohmann::json& j,
const Element* elementPtr);
36 std::string
getTypeName(
const nlohmann::json& j,
const std::string& key);
47 void setTypeName(nlohmann::json& j,
const std::string& key,
const std::string& typeName);
56 template <
class ValueT>
64 template <
class ValueT>
74 template <
class ValueT>
75 using ToJsonFn = std::function<void(nlohmann::json& j,
const ValueT&
v)>;
82 template <
class ValueT>
83 using FromJsonFn = std::function<void(
const nlohmann::json& j, ValueT&
v)>;
185 template <
class DerivedElement>
189 bool overwrite =
false)
211 template <
class DerivedElement>
215 bool overwrite =
false)
217 _instance._registerSerializer<DerivedElement>(
to_json,
from_json, overwrite);
223 template <
class DerivedElement>
227 _instance._removeSerializer<DerivedElement>();
241 static void to_json(nlohmann::json& j,
const data::Element& element);
251 static void from_json(
const nlohmann::json& j, data::Element& element);
260 static void to_json(nlohmann::json& j,
const data::Element* elementPtr);
273 static void from_json(
const nlohmann::json& j, data::ElementPtr& elementPtr);
275 static std::string
getTypeName(
const nlohmann::json& j);
282 struct ElementJsonSerializer
284 template <
class DerivedElement>
288 _to_json = [
to_json](nlohmann::json& j,
const data::Element& element)
290 to_json(j, *
dynamic_cast<const DerivedElement*
>(&element));
294 _from_json = [
this,
from_json](
const nlohmann::json& j, data::Element& element)
295 {
from_json(j, *
dynamic_cast<DerivedElement*
>(&element)); };
297 [
this,
from_json](
const nlohmann::json& j, data::ElementPtr& elementPtr)
301 elementPtr = {
new DerivedElement()};
302 from_json(j, *
dynamic_cast<DerivedElement*
>(elementPtr.get()));
307 void to_json(nlohmann::json& j,
const data::Element& element);
308 void from_json(
const nlohmann::json& j, data::Element& element);
310 void to_json(nlohmann::json& j,
const data::ElementPtr& elementPtr);
311 void from_json(
const nlohmann::json& j, data::ElementPtr& elementPtr);
315 ToJsonFn<data::Element> _to_json;
316 FromJsonFn<data::Element> _from_json;
317 FromJsonFn<data::ElementPtr> _from_json_ptr;
320 static ElementJsonSerializer& getSerializer(
const nlohmann::json& j);
321 static ElementJsonSerializer& getSerializer(
const std::string& demangledTypeName);
325 static ElementJsonSerializers _instance;
327 static void registerElements();
339 ElementJsonSerializers();
346 template <
class DerivedElement>
348 _registerSerializer(ToJsonFn<DerivedElement>
to_json,
352 const std::string typeName = simox::meta::get_type_name<DerivedElement>();
353 if (!overwrite && _serializers.count(typeName))
363 template <
class DerivedElement>
367 _serializers.erase(simox::meta::get_type_name<DerivedElement>());
374 ElementJsonSerializer& _getSerializer(
const std::string& demangledTypeName);
377 std::vector<std::string> _getRegisteredTypes()
const;
380 bool _isTypeRegistered(
const std::string& typeName)
const;
384 std::map<std::string, ElementJsonSerializer> _serializers;