Nuria Framework - Core
Core module of the NuriaProject Framework
 All Classes Functions Typedefs Enumerations Enumerator Groups
Public Types | Public Member Functions | Static Public Member Functions | List of all members
Nuria::Serializer Class Reference

(De-)Serializer for arbitary types based on Nuria::MetaObject. More...

#include <serializer.hpp>

Public Types

enum  RecursionDepth { NoRecursion = 0, InfiniteRecursion = -3 }
 
typedef std::function
< MetaObject *(const
QByteArray &) > 
MetaObjectFinder
 
typedef std::function< void
*(MetaObject *, QVariantMap &) > 
InstanceCreator
 

Public Member Functions

 Serializer (MetaObjectFinder metaObjectFinder=defaultMetaObjectFinder, InstanceCreator instanceCreator=defaultInstanceCreator)
 
 ~Serializer ()
 
QVector< QByteArray > exclude () const
 
void setExclude (const QVector< QByteArray > &list)
 
QVector< QByteArray > allowedTypes () const
 
void setAllowedTypes (const QVector< QByteArray > &list) const
 
QStringList failedFields () const
 
int recursionDepth () const
 
void setRecursionDepth (int maxDepth)
 
void * deserialize (const QVariantMap &data, MetaObject *meta)
 
void * deserialize (const QVariantMap &data, const QByteArray &typeName)
 
template<typename T >
T * deserialize (const QVariantMap &data)
 
bool populate (void *object, MetaObject *meta, const QVariantMap &data)
 
bool populate (void *object, const QByteArray &typeName, const QVariantMap &data)
 
template<typename T >
bool populate (T *object, const QVariantMap &data)
 
QVariantMap serialize (void *object, MetaObject *meta)
 
QVariantMap serialize (void *object, const QByteArray &typeName)
 

Static Public Member Functions

static MetaObjectdefaultMetaObjectFinder (const QByteArray &typeName)
 
static void * defaultInstanceCreator (MetaObject *metaObject, QVariantMap &data)
 

Detailed Description

(De-)Serializer for arbitary types based on Nuria::MetaObject.

This class offers methods to easily (de-)serialize structures from and to QVariantMaps, which can be e.g. (de-)serialized to JSON or other data structures.

Usage
This class makes heavy use of the Nuria meta system. Therefore it's necessary to have Nuria::MetaObjects for all involved types. Though the only thing you really need to do is using Tria (Or Nuria::QtMetaObjectWrapper as fallback).
Deserialization flow
When a fields type does not match the one in the variant map, the following steps in the specified order are tried to resolve it:

If all above steps fail, it'll be noted in the failed list.

See also
failed
Note
Tria will automatically try to register conversions through the generated code.
Serialization flow
For serialization, the following steps are tried:

If all above steps fail, it'll be noted in the failed list.

See also
failed

Member Typedef Documentation

typedef std::function< void *(MetaObject * , QVariantMap & ) > Nuria::Serializer::InstanceCreator

Prototype for a method which uses the two arguments metaObject and data to return a new instance of metaObject. data may be manipulated by the method freely if so desired (E.g. to filter fields or to add or remove fields for other reasons).

If creation fails, nullptr should be returned to indicate an error.

typedef std::function< MetaObject *(const QByteArray & ) > Nuria::Serializer::MetaObjectFinder

Prototype for a method which returns the Nuria::MetaObject for a named type. If no type can be found for that name, nullptr should be returned instead.

Constructor & Destructor Documentation

Nuria::Serializer::Serializer ( MetaObjectFinder  metaObjectFinder = defaultMetaObjectFinder,
InstanceCreator  instanceCreator = defaultInstanceCreator 
)

Constructor.

Nuria::Serializer::~Serializer ( )

Destructor.

Member Function Documentation

QVector< QByteArray > Nuria::Serializer::allowedTypes ( ) const

Returns the list of allowed data types for serialization. The default list is empty.

static void* Nuria::Serializer::defaultInstanceCreator ( MetaObject metaObject,
QVariantMap &  data 
)
static

Default instance creator. It simply tries to find the default constructor for the type in metaObject and returns a new instance. data is ignored.

static MetaObject* Nuria::Serializer::defaultMetaObjectFinder ( const QByteArray &  typeName)
static

Default meta object finder. typeName is expected to be known to the Nuria meta system.

void* Nuria::Serializer::deserialize ( const QVariantMap &  data,
MetaObject meta 
)

Creates a instance of type meta from data. Elements in data are interpreted to be fields in the object. Elements of data which are not fields in object are silently ignored.

Fields that are of a custom type (E.g. not a POD-type or a Qt type) will be deserialized too if the used MetaObjectFinder and InstanceCreator can handle the type.

If all attempts to create a instance failed, nullptr is returned.

void* Nuria::Serializer::deserialize ( const QVariantMap &  data,
const QByteArray &  typeName 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<typename T >
T* Nuria::Serializer::deserialize ( const QVariantMap &  data)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

QVector< QByteArray > Nuria::Serializer::exclude ( ) const

Returns a list of excluded fields. The default list is empty.

QStringList Nuria::Serializer::failedFields ( ) const

Returns a list of fields which failed to (de-)serialize.

bool Nuria::Serializer::populate ( void *  object,
MetaObject meta,
const QVariantMap &  data 
)

Like deserialize, but instead takes a existing object and populates it with data. Returns true if no elements failed to deserialize.

bool Nuria::Serializer::populate ( void *  object,
const QByteArray &  typeName,
const QVariantMap &  data 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<typename T >
bool Nuria::Serializer::populate ( T *  object,
const QVariantMap &  data 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

int Nuria::Serializer::recursionDepth ( ) const

Returns the maximum recursion depth for (de-)serialization. Default value is NoRecursion (i.e. 0).

QVariantMap Nuria::Serializer::serialize ( void *  object,
MetaObject meta 
)

Reads all fields from object and puts them into a QVariantMap. Fields can be excluded by using exclude.

If a field is a pointer which is a object known to , the method will recurse into it until recursion is 0, in which case the property is silently skipped. The default value for recursion is 0, thus by default the method will not recurse.

Note
A value of recursion lower than 0 is considered to be unlimited.
When recursing, exclude is passed on.
QVariantMap Nuria::Serializer::serialize ( void *  object,
const QByteArray &  typeName 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

void Nuria::Serializer::setAllowedTypes ( const QVector< QByteArray > &  list) const

Sets the list of additional allowed types for serialization.

void Nuria::Serializer::setExclude ( const QVector< QByteArray > &  list)

Sets the list of excluded fields. When (de-)serializing, all fields whose names match (case-sensitive) a entry in list will be ignored.

void Nuria::Serializer::setRecursionDepth ( int  maxDepth)

Sets the maximum recursion depth.


The documentation for this class was generated from the following file: