|
| static void | from_json (const nlohmann::json &j, data::Element &element) |
| | Deserialize element from JSON according to its dynamic type.
|
| |
| static void | from_json (const nlohmann::json &j, data::ElementPtr &elementPtr) |
| | Deserialize elementPtr from JSON according the type name in j.
|
| |
| static std::vector< std::string > | getRegisteredTypes () |
| | Get the type names for which serializers are registered.
|
| |
| 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.
|
| |
| template<class DerivedElement> |
| static void | registerSerializer (RawToJsonFn< DerivedElement > to_json, RawFromJsonFn< DerivedElement > from_json, bool overwrite=false) |
| | Register a JSON seralizer for DerivedElement.
|
| |
| template<class DerivedElement> |
| static void | registerSerializer (ToJsonFn< DerivedElement > to_json, FromJsonFn< DerivedElement > from_json, bool overwrite=false) |
| | Register a JSON seralizer for DerivedElement.
|
| |
| template<class DerivedElement> |
| static void | removeSerializer () |
| | Remove a registered serializer for DerivedElement.
|
| |
| static void | to_json (nlohmann::json &j, const data::Element &element) |
| | Serialize element to JSON according to its dynamic type.
|
| |
| static void | to_json (nlohmann::json &j, const data::Element *elementPtr) |
| | Serialize element to JSON according to its dynamic type.
|
| |
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.
to register your custom data::Element 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:
namespace myelement
{
class Mydata::Element : public semrel::data::Element { ... };
void to_json(nlohmann::json& j,
const Mydata::Element& element);
void from_json(
const nlohmann::json& j, Mydata::Element& element);
}
void from_json(const nlohmann::json &j, Vector2f &value)
void to_json(nlohmann::json &j, const Vector2f &value)
To enable serialization through the general serialization functions for data::Element, register your functions to ElementSerializers, specifying your custom type as template argument:
json::ElementSerializers::registerSerializer<Mydata::Element>(
to_json,
from_json);
static void from_json(const nlohmann::json &j, data::Element &element)
Deserialize element from JSON according to its dynamic type.
Definition ElementJsonSerializers.cpp:129
static void to_json(nlohmann::json &j, const data::Element &element)
Serialize element to JSON according to its dynamic type.
Definition ElementJsonSerializers.cpp:123
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.:
namespace myelement
{
extern const int _MY_SHAPE_JSON_REGISTRATION;
}
const int myelement::_MY_SHAPE_JSON_REGISTRATION = []()
{
json::ElementSerializers::registerSerializer<Mydata::Element>(
to_json,
from_json);
return 0;
}();
to register your custom data::Element type
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.