Skip to content
This repository has been archived by the owner on Mar 4, 2023. It is now read-only.

Can not serialize enum properties #44

Open
wartista opened this issue Feb 14, 2021 · 3 comments
Open

Can not serialize enum properties #44

wartista opened this issue Feb 14, 2021 · 3 comments

Comments

@wartista
Copy link

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)

@pjorourke05
Copy link

if I register my enum with the serializer it will work

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

@wartista
Copy link
Author

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.

@pjorourke05
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants