Qt5 Quickly convert json to an object without converting field by field
-
#qt5 #http #json
In Qt5, when I want to convert a JSON to an object, I must get aQJsonObject
and know each the object's field name, then use the key-value pairs to retrieve the object's properties, like this:obj.setId(json["data"]["id"].toInt());
Is there a better way to quickly convert a json ?
thanks :)
-
I don't see how this should be done quicker - you have to pass the keys somehow.
-
@Christian-Ehrlicher said in Qt5 Quickly convert json to an object without converting field by field:
I don't see how this should be done quicker - you have to pass the keys somehow.
like this:
a http get request ....... In http request ..... QNetworkReply* reply; /*initialized*/ QByteArray data = reply->readAll().data(); QJsonParseError jsonError; QJsonDocument json = QJsonDocument::fromJson(data, &jsonError); MyObject* myObject = MyObject::fromJson(json.object()); ...... In MyObject fromJson function ..... myObject.setId(json["data"]["id"].toInt());
-
And what should setId() do? How should it magically know where to put the value to?
-
And what should setId() do? How should it magically know where to put the value to?
@Christian-Ehrlicher
just set my object field..... in MyObject class id is MyObject field ..... void setId(quint32 newId){ id = newId }
-
Sorry I don't understand the problem.
What's wrong withmyObject.setId(json["data"]["id"].toInt());
?
-
@Christian-Ehrlicher said in Qt5 Quickly convert json to an object without converting field by field:
I don't see how this should be done quicker - you have to pass the keys somehow.
like this:
a http get request ....... In http request ..... QNetworkReply* reply; /*initialized*/ QByteArray data = reply->readAll().data(); QJsonParseError jsonError; QJsonDocument json = QJsonDocument::fromJson(data, &jsonError); MyObject* myObject = MyObject::fromJson(json.object()); ...... In MyObject fromJson function ..... myObject.setId(json["data"]["id"].toInt());
@Xrtero
Like @Christian-Ehrlicher I'm not sure what your issue is.You are apparently receiving the JSON text over the network. I would assume that will take waayyy longer than any in-memory manipulation anyway, and the second-most expensive (though far behind) might be the
fromJson()
text parsing. -
@Xrtero
Like @Christian-Ehrlicher I'm not sure what your issue is.You are apparently receiving the JSON text over the network. I would assume that will take waayyy longer than any in-memory manipulation anyway, and the second-most expensive (though far behind) might be the
fromJson()
text parsing.@Christian-Ehrlicher said in Qt5 Quickly convert json to an object without converting field by field:
Sorry I don't understand the problem.
What's wrong withmyObject.setId(json["data"]["id"].toInt());
?
@JonB said in Qt5 Quickly convert json to an object without converting field by field:
@Xrtero
Like @Christian-Ehrlicher I'm not sure what your issue is.You are apparently receiving the JSON text over the network. I would assume that will take waayyy longer than any in-memory manipulation anyway, and the second-most expensive (though far behind) might be the
fromJson()
text parsing.Send an HTTP GET request using QNetworkAccessManager and receive a response in the format of Application/json. I need to parse this response (JSON) into an object.
-
@Christian-Ehrlicher said in Qt5 Quickly convert json to an object without converting field by field:
Sorry I don't understand the problem.
What's wrong withmyObject.setId(json["data"]["id"].toInt());
?
@JonB said in Qt5 Quickly convert json to an object without converting field by field:
@Xrtero
Like @Christian-Ehrlicher I'm not sure what your issue is.You are apparently receiving the JSON text over the network. I would assume that will take waayyy longer than any in-memory manipulation anyway, and the second-most expensive (though far behind) might be the
fromJson()
text parsing.Send an HTTP GET request using QNetworkAccessManager and receive a response in the format of Application/json. I need to parse this response (JSON) into an object.
-
@Christian-Ehrlicher said in Qt5 Quickly convert json to an object without converting field by field:
Sorry I don't understand the problem.
What's wrong withmyObject.setId(json["data"]["id"].toInt());
?
@JonB said in Qt5 Quickly convert json to an object without converting field by field:
@Xrtero
Like @Christian-Ehrlicher I'm not sure what your issue is.You are apparently receiving the JSON text over the network. I would assume that will take waayyy longer than any in-memory manipulation anyway, and the second-most expensive (though far behind) might be the
fromJson()
text parsing.Send an HTTP GET request using QNetworkAccessManager and receive a response in the format of Application/json. I need to parse this response (JSON) into an object.
@Xrtero said in Qt5 Quickly convert json to an object without converting field by field:
. I need to parse this response (JSON) into an object.
You already do this...
-
@Xrtero said in Qt5 Quickly convert json to an object without converting field by field:
. I need to parse this response (JSON) into an object.
You already do this...
Therefore, I'm wondering if there's a more convenient method. For instance, if the JSON is large and the object to be converted has many fields, manually writing the code would involve a significant amount of work.
-
What should be more convenient here? How should some magic code know which attribute you want? Noone else but you can know what you want to retrieve from the json.
-
Therefore, I'm wondering if there's a more convenient method. For instance, if the JSON is large and the object to be converted has many fields, manually writing the code would involve a significant amount of work.
-
With a bit of luck reflection will be included in C++26 and then you can write something like this:
MyObject myObject; for (auto d : std::meta::nonstatic_data_members_of(^MyObject)) myObject.[:d:] = json["data"][[:d:]].toInt();
I.e. steps through all your different data members in a loop (assuming they are all of the type int).
More info here -
What should be more convenient here? How should some magic code know which attribute you want? Noone else but you can know what you want to retrieve from the json.
@Christian-Ehrlicher said in Qt5 Quickly convert json to an object without converting field by field:
What should be more convenient here? How should some magic code know which attribute you want? Noone else but you can know what you want to retrieve from the json.
My current idea is to implement this feature using Qt's reflection, which would allow us to access each field and convert it into the corresponding type.
-
With a bit of luck reflection will be included in C++26 and then you can write something like this:
MyObject myObject; for (auto d : std::meta::nonstatic_data_members_of(^MyObject)) myObject.[:d:] = json["data"][[:d:]].toInt();
I.e. steps through all your different data members in a loop (assuming they are all of the type int).
More info here@hskoglund said in Qt5 Quickly convert json to an object without converting field by field:
With a bit of luck reflection will be included in C++26 and then you can write something like this:
MyObject myObject; for (auto d : std::meta::nonstatic_data_members_of(^MyObject)) myObject.[:d:] = json["data"][[:d:]].toInt();
I.e. steps through all your different data members in a loop (assuming they are all of the type int).
More info hereQt also has the same feature, and that's what I was planning to do.
Finally, someone understands my idea. Thank you! -
@hskoglund said in Qt5 Quickly convert json to an object without converting field by field:
With a bit of luck reflection will be included in C++26 and then you can write something like this:
MyObject myObject; for (auto d : std::meta::nonstatic_data_members_of(^MyObject)) myObject.[:d:] = json["data"][[:d:]].toInt();
I.e. steps through all your different data members in a loop (assuming they are all of the type int).
More info hereQt also has the same feature, and that's what I was planning to do.
Finally, someone understands my idea. Thank you!@Xrtero
So you will use a feature of C++ 26, and it will only work if all your properties are of typeint
. You could do something like this easily if you changed to a language like Python. Anyway if this is what you are looking for and you are happy that is good. -