QtJsonSerializer  4.0.0
A library to perform generic seralization and deserialization of QObjects
Public Member Functions | List of all members
QtJsonSerializer::TypeExtractor Class Referenceabstract

Interface to extract data from any generic container. More...

#include <typeconverter.h>

Inherited by QtJsonSerializer::TypeExtractors::OptionalExtractor< TValue >, QtJsonSerializer::TypeExtractors::PairExtractor< TPair, TFirst, TSecond >, QtJsonSerializer::TypeExtractors::SmartPointerExtractor< TSmartPointer, TType >, QtJsonSerializer::TypeExtractors::SmartPointerExtractor< QPointer, TType >, QtJsonSerializer::TypeExtractors::TupleExtractor< TValues >, and QtJsonSerializer::TypeExtractors::VariantExtractor< TValues >.

Public Member Functions

virtual QByteArray baseType () const =0
 Returns the identifier of the general type this extractor is for. More...
 
virtual QList< int > subtypes () const =0
 Returns an ordered list of subtypes found in types handled by this extractor. More...
 
virtual QVariant extract (const QVariant &value, int index=-1) const =0
 Extracts the data of a value of the extractors type at the given index. More...
 
virtual void emplace (QVariant &target, const QVariant &value, int index=-1) const =0
 Emplaces the value into the target of the extractors type at the given index. More...
 

Detailed Description

Interface to extract data from any generic container.

Note
This class is only relevant if you plan on creating your own TypeConverter

This interface is used by custom TypeConverter classes to get data from a generic container in a non-generic fashion. For example, an extractor for QPair<T1, T2> allows the converters to access the first and second elements of the pair as QVariant, without having to know the actual types of T1 and T2.

When creating extractors, they are typlically generic themselves, as a generic extractor is registered to provide the non generic API. The following code is a basic sample for a QPair converter. Further details about the methods can be found here.

template <typename T1, typename T2>
class QPairExtractor final : public QtJsonSerializer::TypeExtractor
{
public:
using Type = QPair<T1, T2>;
QByteArray baseType() const final {
return QByteArrayLiteral("QPair");
}
QList<int> subtypes() const final {
return {qMetaTypeId<T1>(), qMetaTypeId<T2>()};
}
QVariant extract(const QVariant &value, int index) const final {
const auto vPair = value.value<Type>();
switch (index) {
case 0:
return QVariant::fromValue(vPair.first);
case 1:
return QVariant::fromValue(vPair.second);
default:
return {};
}
}
void emplace(QVariant &target, const QVariant &value, int index) const final {
Q_ASSERT(target.userType() == qMetaTypeId<Type>());
const auto vPair = reinterpret_cast<Type*>(target.data());
switch (index) {
case 0:
vPair->first = value.value<T1>();
break;
case 1:
vPair->second = value.value<T2>();
break;
default:
break;
}
}
};
See also
TypeConverter, TypeConverter::SerializationHelper, TypeConverter::SerializationHelper::extractor, SerializerBase::registerExtractor

Definition at line 19 of file typeconverter.h.

Member Function Documentation

◆ baseType()

QtJsonSerializer::TypeExtractor::baseType ( ) const
pure virtual

Returns the identifier of the general type this extractor is for.

Returns
The base type identifier

The base type should be a simple string representing the generic type this extractor provides. It must be the same for any generic instance created, independent of the generic type, i.e. a QPair<T1, T2> extractor could for example return QPair or pair etc.

When creating a converter that uses this extractor, you can easily check if your converter supports a certain type by checking this base type:

bool MyConverter::canConvert(int metaTypeId) const {
const auto extractor = helper()->extractor(metaTypeId);
return extractor && extractor->baseType() == "QPair";
}
See also
TypeConverter::canConvert

◆ emplace()

QtJsonSerializer::TypeExtractor::emplace ( QVariant target,
const QVariant value,
int  index = -1 
) const
pure virtual

Emplaces the value into the target of the extractors type at the given index.

Parameters
targetThe data to emplace the subdata into
valueThe subvalue to emplace into the target
indexAn optional index, if the value holds more than one subvalue

Use this method in your custom converters TypeConverter::deserialize to get data into a generic container. If a value is supported by the extractor, you can set the subvalues via this method. For simple containers (such as std::optional), you can ignore the index, as there is only one value. For more complex types, such as QPair<T1, T2>, use the index to access the elements.

The type of each value expected by a certain index will match the type at the same index from subtypes(). The subtypes list will also tell you, how many elements there are in the generic type.

To, for example, emplace the value of first of a QPair<T1, T2> after deserialize it, the following code can be used

QVariant pairData{propertyType, nullptr};
const auto extractor = helper()->extractor(metaTypeId);
const auto tIds = extractor->subtypes();
const auto deserData = helper()->deserializeSubtype(tIds[0], data);
extractor->emplace(pairData, deserData, 0);
See also
TypeConverter::deserialize, TypeExtractor::subtypes

◆ extract()

QtJsonSerializer::TypeExtractor::extract ( const QVariant value,
int  index = -1 
) const
pure virtual

Extracts the data of a value of the extractors type at the given index.

Parameters
valueThe data to extract subdata from
indexAn optional index, if the value holds more than one subvalue
Returns
The subvalue extract from value

Use this method in your custom converters TypeConverter::serialize to get data out of a generic container. If a value is supported by the extractor, you can get the subvalues via this method. For simple containers (such as std::optional), you can ignore the index, as there is only one value. For more complex types, such as QPair<T1, T2>, use the index to access the elements.

The type of each value returned by a certain index will match the type at the same index from subtypes(). The subtypes list will also tell you, how many elements there are in the generic type.

To, for example, access the value of second of a QPair<T1, T2> and then serialize it, the following code can be used

const auto extractor = helper()->extractor(metaTypeId);
const auto tIds = extractor->subtypes();
const auto subData = extractor->extract(data, 1);
const auto serData = helper()->serializeSubtype(tIds[1], subData);
See also
TypeConverter::serialize, TypeExtractor::subtypes

◆ subtypes()

QtJsonSerializer::TypeExtractor::subtypes ( ) const
pure virtual

Returns an ordered list of subtypes found in types handled by this extractor.

Returns
The ordered list of subtypes

This list must contain the type ids of all elements contained in the container, in the same order as they are accessed by extract() and emplace(). For example, for a QPair<T1, T2>, the type ids would be qMetaTypeId<T1>() and qMetaTypeId<T2>().

You can use these in your custom converter to pass a type id to the TypeConverter::SerializationHelper::serializeSubtype and TypeConverter::SerializationHelper::serializeSubtype methods when handling elements of the contained data.


The documentation for this class was generated from the following files:
QtJsonSerializer::TypeExtractor
Interface to extract data from any generic container.
Definition: typeconverter.h:19
QtJsonSerializer::TypeExtractor::emplace
virtual void emplace(QVariant &target, const QVariant &value, int index=-1) const =0
Emplaces the value into the target of the extractors type at the given index.
QVariant::fromValue
QVariant fromValue(const T &value)
QList
QtJsonSerializer::TypeExtractor::baseType
virtual QByteArray baseType() const =0
Returns the identifier of the general type this extractor is for.
QtJsonSerializer::TypeExtractor::extract
virtual QVariant extract(const QVariant &value, int index=-1) const =0
Extracts the data of a value of the extractors type at the given index.
QVariant
QtJsonSerializer::TypeExtractor::subtypes
virtual QList< int > subtypes() const =0
Returns an ordered list of subtypes found in types handled by this extractor.
QPair
QByteArray