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

Can not serialize enum properties #44

Open

Description

@wartista

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)

Activity

  1. pjorourke05 commented on Feb 27, 2021

    @pjorourke05

    if I register my enum with the serializer it will work

    QtJsonSerializer::JsonSerializer::registerBasicConverters<RADIANT::SCP::Common::ClientDef::CLIENT_ACCESS_LEVEL>();
  2. wartista commented on Feb 28, 2021

    @wartista
    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.

  3. pjorourke05 commented on Mar 1, 2021

    @pjorourke05

    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 join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions