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>
57 using RawToJsonFn = void (*)(nlohmann::json& j,
const ValueT& v);
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)>;
154 class ElementJsonSerializers
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);
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>
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;
static void registerSerializer(ToJsonFn< DerivedElement > to_json, FromJsonFn< DerivedElement > from_json, bool overwrite=false)
Register a JSON seralizer for DerivedElement.
static void from_json(const nlohmann::json &j, data::Element &element)
Deserialize element from JSON according to its dynamic type.
static const std::string JSON_TYPE_NAME_KEY
JSON key under which demangled type name is stored.
static void to_json(nlohmann::json &j, const data::Element &element)
Serialize element to JSON according to its dynamic type.
static std::vector< std::string > getRegisteredTypes()
Get the type names for which serializers are registered.
static std::string getTypeName(const nlohmann::json &j)
static void registerSerializer(RawToJsonFn< DerivedElement > to_json, RawFromJsonFn< DerivedElement > from_json, bool overwrite=false)
Register a JSON seralizer for DerivedElement.
static bool isTypeRegistered(const std::string &typeName)
Indicates whether there is a serializer registered for the given type name.
static void removeSerializer()
Remove a registered serializer for DerivedElement.
void to_json(nlohmann::json &j, RecordingBatchHeader const &batch)
void from_json(nlohmann::json const &j, RecordingBatchHeader &batch)
std::function< void(nlohmann::json &j, const ValueT &v)> ToJsonFn
A std::function pointer to a function with signature:
void(*)(nlohmann::json &j, const ValueT &v) RawToJsonFn
A raw function pointer to a function with signature:
std::string getTypeName(const nlohmann::json &j, const std::string &key)
Get the type name stored in j.
void(*)(const nlohmann::json &j, ValueT &v) RawFromJsonFn
A raw function pointer to a function with signature:
std::function< void(const nlohmann::json &j, ValueT &v)> FromJsonFn
A std::function pointer to a function with signature:
void setTypeName(nlohmann::json &j, const std::string &key, const std::string &typeName)
Store the type name in j.
void from_json(const nlohmann::json &j, Vector2f &value)
void to_json(nlohmann::json &j, const Vector2f &value)