Nested map in QCborStreamWriter ?
-
So far, I managed to write large nested maps of strings, floats and ints using nested QJsonMap's in a QJsonDocument, but only after all the data is generated, the writing process is slow and the outfile is huge (>400MB).
So I turned my attention to CBOR and its stream writer, but it seems that QCborStreamWriter does not handle nested maps (QCborStreamWriter::append() does not take a QCborMap as argument).
Is there a way to do it anyway or an alternative to QCborStreamWriter that handles nested maps ?
Thanks -
@atrSbt said in Nested map in QCborStreamWriter ?:
@JKSH I submitted the feature request here, I hope I did it correctly.
Yep, you did it correctly.
And it looks like the Qt Core maintainer has provided a simpler solution:
QCborStreamWriter stream = ... QCborMap map = ... map.toCborValue().toCbor(stream); // OR: QCborValue(map).toCbor(stream);
-
QCBorMap has a toCBorValue() function.
-
Thank you for your reply @Christian-Ehrlicher
I see, however this probably won't work since append() does not take a QCborValue as an argument either. I suppose I could then convert the QCborValue further to a QByteArray, but I'm not sure if it's the best way to do this...QCborStreamWriter writer; writer.startArray(); QCBorMap map; writer.append(map.toCBorValue().toByteArray()); writer.endArray();
-
Ok I see.
Then a nested call to https://doc.qt.io/qt-6/qcborstreamwriter.html#startMap-1 / end map should work. Strange that there are no convenience functions for this.
-
Indeed this works:
QCborStreamWriter writer; writer.startMap(); writer.append("A"); writer.append(1); writer.append("B"); writer.append(2); writer.append("C"); writer.startMap(); // start nested map writer.append("C1"); writer.append(3.1); writer.append("C2"); writer.append(3.2); writer.append("C3"); writer.append(3.3); writer.endMap(); // end nested map writer.endMap();
and produces the following tree:
{ "A": 1, "B": 2, "C": { "C1": 3.1, "C2": 3.2, "C3": 3.3 }, "D": 4 }
But I agree, a convenience function would really help, especially with many nested levels and elements.
Thanks for your help ! -
@atrSbt said in Nested map in QCborStreamWriter ?:
Indeed this works:
Great!
a convenience function would really help, especially with many nested levels and elements.
Would you be willing to submit a feature request to https://bugreports.qt.io/ ?
-
@atrSbt said in Nested map in QCborStreamWriter ?:
@JKSH I submitted the feature request here, I hope I did it correctly.
Yep, you did it correctly.
And it looks like the Qt Core maintainer has provided a simpler solution:
QCborStreamWriter stream = ... QCborMap map = ... map.toCborValue().toCbor(stream); // OR: QCborValue(map).toCbor(stream);