Comments (2)
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.
Ok, thank you, I understood your idea.
from qtjsonserializer.
Related Issues (20)
- QHash<int, QString> workaround HOT 2
- It can't convert QSharedPointer successfully.
- How to deserialize derived classes that use none-default constructors HOT 2
- Mac build "make" step giving .reggen errors. Any advice? HOT 2
- Add module fail HOT 1
- Using multiple instances in Qt subdir HOT 3
- Q_GADGET and QVariantList HOT 1
- Deserialization QPair within QVariant
- Cannot find missing dependency "qt.qt5.5130.android_arm64_v8a" ...
- Runtime exception occurred in sample.
- Sample: SerializationException (Unable to determine typeid of meta enum SampleObject::SuperFlags) HOT 1
- Converting QtJsonSerializer from QMake to CMake HOT 1
- Add entries in the repository for Qt 5.15.* and 6
- CopperSpice support HOT 2
- head of line cannot run sample application HOT 1
- Can not serialize enum properties HOT 3
- Poor performance for array of PODs with simple properties
- registerSetConverters results in build time error no matching function to call qHash(...
- Deserialize into existing object
- Create references to existing object , not duplicates HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qtjsonserializer.