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