1

Im using qt 5.0 and its support such classes as QJsonObject QJsonDocument and QJsonArray. In my programm i need to serialize json array and convert it to qstring/qbytearray but i didn't found any serialize or encode methods in those classes. Is there any way i can serialize data using included qt 5.0. libs? I found this example:

QVariant id(1), name("John Doe");
QJsonObject json;

json["Name"] = name.toString();
json.insert("id", id.toInt());

But i can't find how i can make an array from it.

Maxim Makhun
  • 2,197
  • 1
  • 22
  • 26
SirLanceloaaat
  • 213
  • 9
  • 18
  • I've never used qt myself, so I'm not posting this as an answer. I did a quick search on documentation [http://qt-project.org/doc/qt-5.0/qtcore/qjsonobject.html](http://qt-project.org/doc/qt-5.0/qtcore/qjsonobject.html) and it looks like you can create an array like this: `json.insert("id", QJsonArray());` The QJsonValue class has a conversion constructor from a QJsonArray. Of course, you'd want to setup the QJsonArray instead of inserting a default one. – Chris Cooper Jul 16 '13 at 17:32
  • but how do i convert full json object with all keys/and arrays to string? – SirLanceloaaat Jul 16 '13 at 17:45
  • See [http://qt-project.org/doc/qt-5.0/qtcore/qjsondocument.html](http://qt-project.org/doc/qt-5.0/qtcore/qjsondocument.html). Looks like you need to instantiate a QJsonDocument and get its raw data. `QJsonDocument Doc; Doc.SetObject(json); int Size; const char* RawJson = Doc.rawData(&Size);` – Chris Cooper Jul 16 '13 at 18:06
  • I have tried it like so: `QVariant name("John Doe"); QJsonObject json; json.insert("name", QJsonValue::fromVariant(name)); QJsonDocument Doc; Doc.setObject(json); int Size; const char* RawJson = Doc.rawData(&Size); QString t = QString::fromLatin1(RawJson); qDebug() << t;` I get RawData = "qbjs" and t = "qbjs" – SirLanceloaaat Jul 16 '13 at 19:28
  • My bad. It looks like `rawData()` returns the binary JSON format. Try `QByteArray ByteArray = Doc.toJson()` instead. Documentation: Converts the QJsonDocument to a UTF-8 encoded JSON document. – Chris Cooper Jul 16 '13 at 19:42
  • @Chris Cooper, Doc.toJson return void not QbyteArray. You must use Doc.toBinaryArray – Maryam Shabani Jan 05 '14 at 18:23

2 Answers2

0

Question closed. Use QJsonDocument::toJson to get data from QJsonObject.

SirLanceloaaat
  • 213
  • 9
  • 18
0

I think the secret is that when serializing to JSON, Qt writes out a UTF-8 binary encoded version of the text into a QByteArray. You can recover the string using QString::fromUtf8. Also, even though toJson writes out a QByteArray it is not a true binary representation; as I just mentioned, it is the bytes of the UTF-8 encoded string representation of the JSON. For binary serialization you must use either Qt's own QJsonDocument::toBinaryData or find a BSON library.

Text serialization

Use QJsonDocument::toJson to serialize as text via a QString, and QJsonDocument::fromJson to deserialize from text.

Tip: Consider passing the QJsonDocument::Compact format option to get compact JSON output during serialization.

To QString

QString json = QString::fromUtf8(doc.toJson(QJsonDocument::Compact));

From QString

QJsonDocument doc = QJsonDocument::fromJson(json);

Binary serialization

Use QJsonDocument::toBinaryData to serialize as binary via a QByteArray, and QJsonDocument::fromBinaryData to deserialize from binary.

Caveat: The binary format used by the above functions is Qt's own binary format (qbjs) so may not be suitable for interoperation with other binary formats such as BSON. For that, you might want to look into choosing a BSON implementation.

To QByteArray

QByteArray bytes = doc.toBinaryData();

From QByteArray

QJsonDocument doc = QJsonDocument::fromBinaryData(bytes);
Wyck
  • 10,311
  • 6
  • 39
  • 60