class
#include <core/data/ser/serializer.hpp>
Serializer Base class for serializers, which defines the interface for serializing arbitrary data using its reflection metadata.
Serializers are type visitors which allow overriding the default serialization behaviour for each type using hooks. Hooks are functions which are called when the serializer encounters a type, and can be used to customize the serialization process.
If a type which can't be further decomposed is encountered for which no hook is defined, the serializer will emit a warning and fail. Implementations should set default hooks for at least the primitive types.
Derived classes
- class BinarySerializer
- Implementation of the abstract Serializer class meant to serialize data in a non-readable but fast and compact way.
- class DebugSerializer
- Serializer implementation which prints the given data to a stream in a human-readable format not meant to be parsed.
- class JSONSerializer
- Implementation of the abstract Serializer class for serializing to JSON.
Public types
-
using Hook = memory::
Function<bool(const void*)> - Function type for serialization hooks.
-
template<typename T>using TypedHook = memory::
Function<bool(const T&)> - Function type for serialization hooks.
Constructors, destructors, conversion operators
- Serializer() defaulted
- Default constructs.
Public functions
-
auto write(const reflection::
Type& type, const void* value) -> bool - Serialize the given value.
-
template<typename T>auto write(const T& value) -> bool
- Serialize the given value.
-
void hook(const reflection::
Type& type, Hook hook) - Sets the hook to be called on serialization of the given type.
-
template<typename T>void hook(TypedHook<T> hook)
- Sets the hook to be called on serialization of the given type.
Protected functions
-
auto decompose(const reflection::
Type& type, const void* value) -> bool pure virtual - Called for each type with no hook defined.
Typedef documentation
template<typename T>
using cubos:: core:: data:: Serializer:: TypedHook = memory:: Function<bool(const T&)>
Function type for serialization hooks.
Template parameters | |
---|---|
T | Type. |
Function documentation
bool cubos:: core:: data:: Serializer:: write(const reflection:: Type& type,
const void* value)
Serialize the given value.
Parameters | |
---|---|
type | Type. |
value | Value. |
Returns | Whether the value was successfully serialized. |
template<typename T>
bool cubos:: core:: data:: Serializer:: write(const T& value)
Serialize the given value.
Template parameters | |
---|---|
T | Type. |
Parameters | |
value | Value. |
Returns | Whether the value was successfully serialized. |
void cubos:: core:: data:: Serializer:: hook(const reflection:: Type& type,
Hook hook)
Sets the hook to be called on serialization of the given type.
Parameters | |
---|---|
type | Type. |
hook | Hook. |
bool cubos:: core:: data:: Serializer:: decompose(const reflection:: Type& type,
const void* value) pure virtual protected
Called for each type with no hook defined.
Parameters | |
---|---|
type | Type. |
value | Value. |
Returns | Whether the value was successfully serialized. |
Should recurse by calling write() again as appropriate.