Discussion:
[Interest] Qt 5: Converting QJsonValue to QJsonArray
Stephen Chu
2012-08-08 19:14:03 UTC
Permalink
I am trying out the new QJsonValue in Qt 5 and find myself wondering:
Does converting a QJsonValue which is an array to a QJsonArray with
QJsonValue::toArray() makes a copy of the underlying data?

Say I load a JSON file which is an object that contains an array with
thousands of items using QJsonDocument:

auto myObj = QJsonDocument::fromJson(jsonData).object();
auto myArray = myObject["theArray"].toArray();

How efficient is the conversion? Does it make a deep copy of the data in
the original object to the new array?

Thanks.
l***@nokia.com
2012-08-13 13:24:04 UTC
Permalink
Post by Stephen Chu
Does converting a QJsonValue which is an array to a QJsonArray with
QJsonValue::toArray() makes a copy of the underlying data?
Say I load a JSON file which is an object that contains an array with
auto myObj = QJsonDocument::fromJson(jsonData).object();
auto myArray = myObject["theArray"].toArray();
How efficient is the conversion? Does it make a deep copy of the data in
the original object to the new array?
No it doesn't make any deep copies as long as the data is not being modified.

Cheers,
Lars
Stephen Chu
2012-08-13 21:21:08 UTC
Permalink
Post by l***@nokia.com
Post by Stephen Chu
Does converting a QJsonValue which is an array to a QJsonArray with
QJsonValue::toArray() makes a copy of the underlying data?
Say I load a JSON file which is an object that contains an array with
auto myObj = QJsonDocument::fromJson(jsonData).object();
auto myArray = myObject["theArray"].toArray();
How efficient is the conversion? Does it make a deep copy of the data in
the original object to the new array?
No it doesn't make any deep copies as long as the data is not being modified.
Thanks. That's good to know.

Now, what happens if I want to modify one of the 1000 items in an array
that is an item of a QJsonObject? Looks like I need to make a copy of
the array with toArray(), change the item, then assign the modified (and
deep copied) array back to the item in the object?

There doesn't seem to have a way to get a reference to a second-level
item in an object or array.
l***@nokia.com
2012-08-13 21:47:15 UTC
Permalink
Post by Stephen Chu
Post by l***@nokia.com
Post by Stephen Chu
Does converting a QJsonValue which is an array to a QJsonArray with
QJsonValue::toArray() makes a copy of the underlying data?
Say I load a JSON file which is an object that contains an array with
auto myObj = QJsonDocument::fromJson(jsonData).object();
auto myArray = myObject["theArray"].toArray();
How efficient is the conversion? Does it make a deep copy of the data in
the original object to the new array?
No it doesn't make any deep copies as long as the data is not being modified.
Thanks. That's good to know.
Now, what happens if I want to modify one of the 1000 items in an array that is an item of a QJsonObject? Looks like I need to make a copy of the array with toArray(), change the item, then assign the modified (and deep copied) array back to the item in the object?
There doesn't seem to have a way to get a reference to a second-level item in an object or array.
No, not currently. The reason is that the internal data structure is optimised for reading, not writing. But I was considering adding some API that returns references after 5.0.

Cheers,
Lars

Loading...