QtJsonSerializer icon indicating copy to clipboard operation
QtJsonSerializer copied to clipboard

Can not serialize enum properties

Open wartista opened this issue 5 years ago • 3 comments

Hi I'm using Qt version 5.10.0. I get this error when serializing this class : `class Spectra : public QObject { Q_OBJECT Q_ENUM(SpectrumType) Q_ENUM(SpectrumPolarization) Q_PROPERTY(double incidenceAngle MEMBER incidenceAngle) Q_PROPERTY(SpectrumType type MEMBER type) Q_PROPERTY(SpectrumPolarization polarization MEMBER polarization)

public: Q_INVOKABLE Spectra(QObject *parent = nullptr) : QObject(parent) { type = ST_Reflectance; incidenceAngle = 0.0; polarization = SP_Unpolarized; }

~Spectra(){}

double incidenceAngle;
SpectrumType type;
SpectrumPolarization polarization;

};

Q_DECLARE_METATYPE(Spectra *) `

At runtime it says ; QMetaProperty::read: Unable to handle unregistered datatype 'SpectrumType' for property 'Spectra::type' QMetaProperty::read: Unable to handle unregistered datatype 'SpectrumPolarization' for property 'Spectra::polarization'

Then serializes the object as depicted below { "spectraList": [ { "incidenceAngle": 0, "polarization": null, "type": null } ] } To register datatypes I added required meta type declarations;

Q_DECLARE_METATYPE(SpectrumType) Q_DECLARE_METATYPE(SpectrumPolarization) This time QtJsonserializer throws exeption and crashes; terminate called after throwing an instance of 'QJsonSerializationException' what(): what: Failed to serialize with error: Failed to convert type SpectrumType to a JSON representation Property Trace: spectraList (Type: QList<Spectra*>) [0] (Type: Spectra*) type (Type: SpectrumType)

wartista avatar Feb 14 '21 07:02 wartista

if I register my enum with the serializer it will work

QtJsonSerializer::JsonSerializer::registerBasicConverters<RADIANT::SCP::Common::ClientDef::CLIENT_ACCESS_LEVEL>();

pjorourke05 avatar Feb 27 '21 05:02 pjorourke05

if I register my enum with the serializer it will work

QtJsonSerializer::JsonSerializer::registerBasicConverters<RADIANT::SCP::Common::ClientDef::CLIENT_ACCESS_LEVEL>();

I think my version is old because i could not find QtJsonSerializer::JsonSerializer::registerBasicConverters. How can i make sure which version I use? Actually I tried to use Qt maintenance tool to install the lasest version but i seems did not work.

wartista avatar Feb 28 '21 07:02 wartista

I build the repo manually because I am using Qt 5.15.0 and it's not included as a add on. Just follow his steps for building manually. Just make sure your build output is the same dir as repo because his build process uses generated files that are stored in the repos sub dirs

pjorourke05 avatar Mar 01 '21 00:03 pjorourke05