5 #include <SimoxUtility/json.h>
6 #include <SimoxUtility/meta/type_name.h>
8 #include <RobotAPI/interface/ArViz/Elements.h>
16 void to_json(nlohmann::json& j,
const Element& element);
18 void from_json(
const nlohmann::json& j, Element& element);
21 void to_json(nlohmann::json& j,
const ElementPtr& elementPtr);
23 void from_json(
const nlohmann::json& j, ElementPtr& elementPtr);
26 void to_json(nlohmann::json& j,
const Element* elementPtr);
38 std::string
getTypeName(
const nlohmann::json& j,
const std::string& key);
49 void setTypeName(nlohmann::json& j,
const std::string& key,
const std::string& typeName);
58 template <
class ValueT>
66 template <
class ValueT>
76 template <
class ValueT>
77 using ToJsonFn = std::function<void(nlohmann::json& j,
const ValueT&
v)>;
84 template <
class ValueT>
85 using FromJsonFn = std::function<void(
const nlohmann::json& j, ValueT&
v)>;
190 template <
class DerivedElement>
193 bool overwrite =
false)
213 template <
class DerivedElement>
216 bool overwrite =
false)
218 _instance._registerSerializer<DerivedElement>(
to_json,
from_json, overwrite);
224 template <
class DerivedElement>
227 _instance._removeSerializer<DerivedElement>();
242 static void to_json(nlohmann::json& j,
const data::Element& element);
252 static void from_json(
const nlohmann::json& j, data::Element& element);
261 static void to_json(nlohmann::json& j,
const data::Element* elementPtr);
274 static void from_json(
const nlohmann::json& j, data::ElementPtr& elementPtr);
276 static std::string
getTypeName(
const nlohmann::json& j);
284 struct ElementJsonSerializer
286 template <
class DerivedElement>
290 _to_json = [
to_json](nlohmann::json & j,
const data::Element & element)
292 to_json(j, *
dynamic_cast<const DerivedElement*
>(&element));
295 _from_json = [
this,
from_json](
const nlohmann::json & j, data::Element & element)
297 from_json(j, *
dynamic_cast<DerivedElement*
>(&element));
299 _from_json_ptr = [
this,
from_json](
const nlohmann::json & j, data::ElementPtr & elementPtr)
303 elementPtr = {
new DerivedElement() };
304 from_json(j, *
dynamic_cast<DerivedElement*
>(elementPtr.get()));
309 void to_json(nlohmann::json& j,
const data::Element& element);
310 void from_json(
const nlohmann::json& j, data::Element& element);
312 void to_json(nlohmann::json& j,
const data::ElementPtr& elementPtr);
313 void from_json(
const nlohmann::json& j, data::ElementPtr& elementPtr);
318 ToJsonFn<data::Element> _to_json;
319 FromJsonFn<data::Element> _from_json;
320 FromJsonFn<data::ElementPtr> _from_json_ptr;
325 static ElementJsonSerializer& getSerializer(
const nlohmann::json& j);
326 static ElementJsonSerializer& getSerializer(
const std::string& demangledTypeName);
330 static ElementJsonSerializers _instance;
332 static void registerElements();
345 ElementJsonSerializers();
353 template <
class DerivedElement>
354 void _registerSerializer(ToJsonFn<DerivedElement>
to_json,
358 const std::string typeName = simox::meta::get_type_name<DerivedElement>();
359 if (!overwrite && _serializers.count(typeName))
369 template <
class DerivedElement>
370 void _removeSerializer()
372 _serializers.erase(simox::meta::get_type_name<DerivedElement>());
380 ElementJsonSerializer& _getSerializer(
const std::string& demangledTypeName);
383 std::vector<std::string> _getRegisteredTypes()
const;
386 bool _isTypeRegistered(
const std::string& typeName)
const;
390 std::map<std::string, ElementJsonSerializer> _serializers;