|
Handles serialization and deserialization of dynamic data::Element
objects to and from JSON.
More...
#include <RobotAPI/components/ArViz/Introspection/ElementJsonSerializers.h>
Static Public Member Functions | |
static void | from_json (const nlohmann::json &j, data::Element &element) |
Deserialize element from JSON according to its dynamic type. More... | |
static void | from_json (const nlohmann::json &j, data::ElementPtr &elementPtr) |
Deserialize elementPtr from JSON according the type name in j . More... | |
static std::vector< std::string > | getRegisteredTypes () |
Get the type names for which serializers are registered. More... | |
static std::string | getTypeName (const nlohmann::json &j) |
static bool | isTypeRegistered (const std::string &typeName) |
Indicates whether there is a serializer registered for the given type name. More... | |
template<class DerivedElement > | |
static void | registerSerializer (RawToJsonFn< DerivedElement > to_json, RawFromJsonFn< DerivedElement > from_json, bool overwrite=false) |
Register a JSON seralizer for DerivedElement . More... | |
template<class DerivedElement > | |
static void | registerSerializer (ToJsonFn< DerivedElement > to_json, FromJsonFn< DerivedElement > from_json, bool overwrite=false) |
Register a JSON seralizer for DerivedElement . More... | |
template<class DerivedElement > | |
static void | removeSerializer () |
Remove a registered serializer for DerivedElement . More... | |
static void | to_json (nlohmann::json &j, const data::Element &element) |
Serialize element to JSON according to its dynamic type. More... | |
static void | to_json (nlohmann::json &j, const data::Element *elementPtr) |
Serialize element to JSON according to its dynamic type. More... | |
Static Public Attributes | |
static const std::string | JSON_TYPE_NAME_KEY = "__type__" |
JSON key under which demangled type name is stored. More... | |
Handles serialization and deserialization of dynamic data::Element
objects to and from JSON.
This class allows serialization of newly defined types through the standard nlohmann::json
interface, which builds upon user-defined to_json()
and from_json()
functions for a user-defined type.
Suppose you have a custom class deriving from semrel::data::Element
in a namespace myelement
, along with serializing functions following the standard pattern employed by nlohmann::json
:
To enable serialization through the general serialization functions for data::Element
, register your functions to ElementSerializers
, specifying your custom type as template argument:
You can also specify the namespace, like in myelement::to_json
.
To make sure the serializer is always registered, you can create a static variable (extern free or a static class member) whose initialization will register the serializers, e.g.:
ElementSerializers
has a global map, which maps a (demangled) type name to the respective serializer. When serializing an object of type data::Element
, the map is searched for an entry for the instance's dynamic type. If one is found, the registered to_json()
is used to write the JSON document. In addition, a type name entry specifying the (demangled) derived type name is stored (under the key ElementSerializers::JSON_TYPE_NAME_KEY
).
When deserializing from a JSON document, the type name entry is used to determine the correct serializer, which casts the to-be-deserialized data::Element
instance to the derived type and passes it to the registered from_json
method. When deserializing into a data::ElementPtr
, the pointer is allocated a new instance of the deriving type first.
Definition at line 157 of file ElementJsonSerializers.h.
|
static |
Deserialize element
from JSON according to its dynamic type.
<tt>error::NoTypeNameEntryInJsonObject</tt> | If j does not contain the key JSON_TYPE_NAME_KEY (or is not a JSON object). |
<tt>error::TypeNameMismatch</tt> | If the type name in j does not match element 's dynamic type. |
<tt>error::NoSerializerForType</tt> | If there is no serializer for the given name. |
Definition at line 114 of file ElementJsonSerializers.cpp.
|
static |
Deserialize elementPtr
from JSON according the type name in j
.
If there is a registered serializer for the type name in j
, assigns a new instance of the dynamic type to elementPtr
and deserializes the created instance from j
. /// Get the accepted demangled type names.
<tt>error::NoTypeNameEntryInJsonObject</tt> | If j does not contain the key JSON_TYPE_NAME_KEY (or is not a JSON object). |
<tt>error::NoSerializerForType</tt> | If there is no serializer for the type in j . |
Definition at line 133 of file ElementJsonSerializers.cpp.
|
static |
Get the type names for which serializers are registered.
Definition at line 99 of file ElementJsonSerializers.cpp.
|
static |
Definition at line 138 of file ElementJsonSerializers.cpp.
|
static |
Indicates whether there is a serializer registered for the given type name.
Definition at line 104 of file ElementJsonSerializers.cpp.
|
inlinestatic |
Register a JSON seralizer for DerivedElement
.
Can be called with raw function pointers with automatic type deduction e.g.:
<tt>error::SerializerAlreadyRegisteredForType</tt> | If there already is a registered serializer for that type. |
Definition at line 191 of file ElementJsonSerializers.h.
|
inlinestatic |
Register a JSON seralizer for DerivedElement
.
Can be called with std::function
objects, e.g. lambdas:
<tt>error::SerializerAlreadyRegisteredForType</tt> | If there already is a registered serializer for that type. |
Definition at line 214 of file ElementJsonSerializers.h.
|
inlinestatic |
Remove a registered serializer for DerivedElement
.
Definition at line 225 of file ElementJsonSerializers.h.
|
static |
Serialize element
to JSON according to its dynamic type.
<tt>error::NoSerializerForType</tt> | If there is no serializer for the given name. |
Definition at line 109 of file ElementJsonSerializers.cpp.
|
static |
Serialize element
to JSON according to its dynamic type.
<tt>error::data::ElementPointerIsNull</tt> | If elementPtr is null. |
<tt>error::NoSerializerForType</tt> | If there is no serializer for the element type. |
Definition at line 124 of file ElementJsonSerializers.cpp.
|
static |
JSON key under which demangled type name is stored.
Definition at line 162 of file ElementJsonSerializers.h.