Might relate to How to change QJsonObject value in a QJson hierarchy without using copies?, but the answer is basically, it doesn't work, maybe things have changed since then. Given is the following signature:
void addToJson(const QStringList& path, QJsonObject& target, const QJsonObject& object)
It should build a JSON object based on the path
and add object
, e.g:
path: obj,obj2,obj3
->
"obj": {
"obj2": {
"obj3": {
}
}
}
object
would be added to obj3.
Current implementation is:
void addToJson(const QStringList& path, QJsonObject& target, const QJsonObject& object)
{
if(path.isEmpty())
{
target = object;
return;
}
QString objName = path.first();
QJsonObject tmpObj = target[objName].toObject();
addToJson(path.mid(1), tmpObj, object);
target[objName] = tmpObj;
}
Can it be written, to avoid the copies (basically you would have a lot of deep copies in each iteration)?
The []
operator gives you a QJsonValueRef
, what is good, but if you want to add something to it, you would need to convert it to a QJsonObject
again and not a QJsonValueRef
, which breaks everything.