Nested maps in Qt's QCborStreamWriter?

66 Views Asked by At

My program provides a large amount of data, organized in nested maps, that I would like to serialize and write in a file. Originally, I used a QJsonDocument with QJsonMap's and QJsonArray's, but the file writing process at the end is very long and the file too large (>400MB). So I looked into QCborStreamWriter but I can't find examples with nested maps.

Is there a way to do it ? QCborStreamWriter::append() does not take a QCborMap as argument.

QCborStreamWriter writer;
writer.startArray();
QCBorMap map;
writer.append(map); <= not accepted
writer.endArray();
1

There are 1 best solutions below

0
On

Calling repeatedly startMap() and endMap() seems to do the trick.

QCborStreamWriter writer(&file);
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
}