summaryrefslogtreecommitdiffstats
path: root/src/corelib/serialization/qjsonwriter.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2019-06-19 13:43:00 +0200
committerUlf Hermann <ulf.hermann@qt.io>2019-10-21 19:12:51 +0200
commit35adb74ddd915831789f0175423660f8e898942e (patch)
tree3ac0fd8be75f0b7e0391149d13aab51a7f02dd3f /src/corelib/serialization/qjsonwriter.cpp
parent559b563d711db0760a51b0dce26536dbc8766a9d (diff)
Reimplement JSON support on top of Cbor
In turn, deprecate the QJsonDocument methods that deal with JSON binary data. You should use CBOR for data serialization these days. [ChangeLog][Deprecation Notice] The binary JSON representation is deprecated. The CBOR format should be used instead. Fixes: QTBUG-47629 Change-Id: Ic8b92ea36de87815b12307a9d8b1095f07166db8 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/serialization/qjsonwriter.cpp')
-rw-r--r--src/corelib/serialization/qjsonwriter.cpp75
1 files changed, 41 insertions, 34 deletions
diff --git a/src/corelib/serialization/qjsonwriter.cpp b/src/corelib/serialization/qjsonwriter.cpp
index 012d3bea86..627d1bbd62 100644
--- a/src/corelib/serialization/qjsonwriter.cpp
+++ b/src/corelib/serialization/qjsonwriter.cpp
@@ -44,13 +44,14 @@
#include "qjson_p.h"
#include "private/qutfcodec_p.h"
#include <private/qnumeric_p.h>
+#include <private/qcborvalue_p.h>
QT_BEGIN_NAMESPACE
using namespace QJsonPrivate;
-static void objectContentToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact);
-static void arrayContentToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact);
+static void objectContentToJson(const QCborContainerPrivate *o, QByteArray &json, int indent, bool compact);
+static void arrayContentToJson(const QCborContainerPrivate *a, QByteArray &json, int indent, bool compact);
static inline uchar hexdig(uint u)
{
@@ -126,16 +127,20 @@ static QByteArray escapedString(const QString &s)
return ba;
}
-static void valueToJson(const QJsonPrivate::Base *b, const QJsonPrivate::Value &v, QByteArray &json, int indent, bool compact)
+static void valueToJson(const QCborValue &v, QByteArray &json, int indent, bool compact)
{
- QJsonValue::Type type = (QJsonValue::Type)(uint)v.type;
+ QCborValue::Type type = v.type();
switch (type) {
- case QJsonValue::Bool:
- json += v.toBoolean() ? "true" : "false";
+ case QCborValue::True:
+ json += "true";
break;
- case QJsonValue::Double: {
- const double d = v.toDouble(b);
- if (qIsFinite(d)) { // +2 to format to ensure the expected precision
+ case QCborValue::False:
+ json += "false";
+ break;
+ case QCborValue::Integer:
+ case QCborValue::Double: {
+ const double d = v.toDouble();
+ if (qIsFinite(d)) {
quint64 absInt;
json += QByteArray::number(d, convertDoubleTo(std::abs(d), &absInt) ? 'f' : 'g',
QLocale::FloatingPointShortest);
@@ -144,42 +149,44 @@ static void valueToJson(const QJsonPrivate::Base *b, const QJsonPrivate::Value &
}
break;
}
- case QJsonValue::String:
+ case QCborValue::String:
json += '"';
- json += escapedString(v.toString(b));
+ json += escapedString(v.toString());
json += '"';
break;
- case QJsonValue::Array:
+ case QCborValue::Array:
json += compact ? "[" : "[\n";
- arrayContentToJson(static_cast<QJsonPrivate::Array *>(v.base(b)), json, indent + (compact ? 0 : 1), compact);
+ arrayContentToJson(
+ QJsonPrivate::Value::container(v), json, indent + (compact ? 0 : 1), compact);
json += QByteArray(4*indent, ' ');
json += ']';
break;
- case QJsonValue::Object:
+ case QCborValue::Map:
json += compact ? "{" : "{\n";
- objectContentToJson(static_cast<QJsonPrivate::Object *>(v.base(b)), json, indent + (compact ? 0 : 1), compact);
+ objectContentToJson(
+ QJsonPrivate::Value::container(v), json, indent + (compact ? 0 : 1), compact);
json += QByteArray(4*indent, ' ');
json += '}';
break;
- case QJsonValue::Null:
+ case QCborValue::Null:
default:
json += "null";
}
}
-static void arrayContentToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact)
+static void arrayContentToJson(const QCborContainerPrivate *a, QByteArray &json, int indent, bool compact)
{
- if (!a || !a->length)
+ if (!a || a->elements.empty())
return;
QByteArray indentString(4*indent, ' ');
- uint i = 0;
- while (1) {
+ qsizetype i = 0;
+ while (true) {
json += indentString;
- valueToJson(a, a->at(i), json, indent, compact);
+ valueToJson(a->valueAt(i), json, indent, compact);
- if (++i == a->length) {
+ if (++i == a->elements.size()) {
if (!compact)
json += '\n';
break;
@@ -190,23 +197,23 @@ static void arrayContentToJson(const QJsonPrivate::Array *a, QByteArray &json, i
}
-static void objectContentToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact)
+static void objectContentToJson(const QCborContainerPrivate *o, QByteArray &json, int indent, bool compact)
{
- if (!o || !o->length)
+ if (!o || o->elements.empty())
return;
QByteArray indentString(4*indent, ' ');
- uint i = 0;
- while (1) {
- QJsonPrivate::Entry *e = o->entryAt(i);
+ qsizetype i = 0;
+ while (true) {
+ QCborValue e = o->valueAt(i);
json += indentString;
json += '"';
- json += escapedString(e->key());
+ json += escapedString(o->valueAt(i).toString());
json += compact ? "\":" : "\": ";
- valueToJson(o, e->value, json, indent, compact);
+ valueToJson(o->valueAt(i + 1), json, indent, compact);
- if (++i == o->length) {
+ if ((i += 2) == o->elements.size()) {
if (!compact)
json += '\n';
break;
@@ -216,18 +223,18 @@ static void objectContentToJson(const QJsonPrivate::Object *o, QByteArray &json,
}
}
-void Writer::objectToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact)
+void Writer::objectToJson(const QCborContainerPrivate *o, QByteArray &json, int indent, bool compact)
{
- json.reserve(json.size() + (o ? (int)o->size : 16));
+ json.reserve(json.size() + (o ? (int)o->elements.size() : 16));
json += compact ? "{" : "{\n";
objectContentToJson(o, json, indent + (compact ? 0 : 1), compact);
json += QByteArray(4*indent, ' ');
json += compact ? "}" : "}\n";
}
-void Writer::arrayToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact)
+void Writer::arrayToJson(const QCborContainerPrivate *a, QByteArray &json, int indent, bool compact)
{
- json.reserve(json.size() + (a ? (int)a->size : 16));
+ json.reserve(json.size() + (a ? (int)a->elements.size() : 16));
json += compact ? "[" : "[\n";
arrayContentToJson(a, json, indent + (compact ? 0 : 1), compact);
json += QByteArray(4*indent, ' ');