Git Product home page Git Product logo

Comments (2)

Skycoder42 avatar Skycoder42 commented on July 20, 2024

I see. So what you basically need is a "serialize into" method you could call, something like this (from the ctor):

QJsonSerializer s;
s.serializeInto<JsonSerializable>(this, json);

Currently, there is no such feature. But there is one problem with your Idea in general. How do you want to handle subclassing? E.g. if you have a class MyGadget that extends JsonSerializable - how do you want to "tell" the JsonSerializable that it needs to deserialize to that type - within a constructor of a base class, it is impossible to know what concrete class is calling it (See https://stackoverflow.com/questions/962132/calling-virtual-functions-inside-constructors for more details)!

So even if that method did exist, you would not be able to deserialize anything with it in the constructor. Also, gadgets do not support polymorphing, so even if the json you deserialize contains information about the concrete class, it cannot be processed by the serializer. You will also have a similar problem when serializing in the tojson method, as the base class somehow needs to now the concrete gadget type! (Please note that this limitation only applies to gadgets - with objects, this would not be a problem at all!)

Instead of having a constructor that takes JSON, I would recommend you to create a static construction method instead. This would solve all of your problems in a simple and elegant way. The following is my idea of implementing a self-serializing class:

class JsonSerializable 
{
    Q_GADGET

public:
    template <typename T>
    static T fromJson(const QByteArray &json) {
        static_assert(std::is_base_of<JsonSerializable, T>::value, "T must extend JsonSerializable");
        QJsonSerializer s;
        return s.deserializeFrom<T>(json);
    }

    QByteArray toJson() const {
        QJsonSerializer s;
        return s.serializeTo(toVariant());
    }
 
protected:
    virtual QVariant toVariant() const = 0;
};

// in every subclass, implement the toVariant method as:

QVariant MyGadget::toVariant() const {
    return QVariant::fromValue(this);
}

I know that it is kind of ugly to have the toVariant method here, but sadly, there is no other way, as once again, gadgets are very limited when it comes to dynamic typing (The reason is that gadgets, unlike objects, do not have a dynamic meta object, only a static one). If you want to avoid this, the only possible way is to use QObject based classes instead.

from qtjsonserializer.

arietto avatar arietto commented on July 20, 2024

Ok, thank you, I understood your idea.

from qtjsonserializer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.