summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/json
diff options
context:
space:
mode:
authorLiang Qi <liang.qi@digia.com>2013-05-14 14:19:47 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-05-20 12:06:48 +0200
commit57acf1b46ca4bb670f4bf1fe3807dfa91edb6248 (patch)
treed2e2d02cb09fab9904ba9009c7b91fa5c1e375a1 /tests/auto/corelib/json
parentabaaaea0416da2e111e543d67a31476b402cffde (diff)
QtCore: fix the number precision in QJsonDocument.toJson()
In JSON, any number is stored in double. We need to make sure we keep the maximum possible number precision for integer number. In IEEE 754 double format, the significand precision is 53 bits(52 explicityly stored). Autotest is included. qint64 and double work fine. Task-number: QTBUG-28467 Change-Id: I7f857671c50e4334e9329c778f9b4f090f490540 Reviewed-by: Sune Vuorela <sune@vuorela.dk> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/json')
-rw-r--r--tests/auto/corelib/json/tst_qtjson.cpp77
1 files changed, 65 insertions, 12 deletions
diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp
index 6811551769..fc4b3831c4 100644
--- a/tests/auto/corelib/json/tst_qtjson.cpp
+++ b/tests/auto/corelib/json/tst_qtjson.cpp
@@ -231,9 +231,54 @@ void tst_QtJson::testNumbers()
QJsonArray array;
for (int i = 0; i < n; ++i)
array.append((double)numbers[i]);
+
+ QByteArray serialized = QJsonDocument(array).toJson();
+ QJsonDocument json = QJsonDocument::fromJson(serialized);
+ QJsonArray array2 = json.array();
+
+ QCOMPARE(array.size(), array2.size());
+ for (int i = 0; i < array.size(); ++i) {
+ QCOMPARE(array.at(i).type(), QJsonValue::Double);
+ QCOMPARE(array.at(i).toDouble(), (double)numbers[i]);
+ QCOMPARE(array2.at(i).type(), QJsonValue::Double);
+ QCOMPARE(array2.at(i).toDouble(), (double)numbers[i]);
+ }
+ }
+
+ {
+ qint64 numbers[] = {
+ 0,
+ -1,
+ 1,
+ (1UL<<54),
+ (1UL<<55),
+ (1UL<<56),
+ -(1UL<<54),
+ -(1UL<<55),
+ -(1UL<<56),
+ (1UL<<54) - 1,
+ (1UL<<55) - 1,
+ (1UL<<56) - 1,
+ -((1UL<<54) - 1),
+ -((1UL<<55) - 1),
+ -((1UL<<56) - 1)
+ };
+ int n = sizeof(numbers)/sizeof(qint64);
+
+ QJsonArray array;
+ for (int i = 0; i < n; ++i)
+ array.append((double)numbers[i]);
+
+ QByteArray serialized = QJsonDocument(array).toJson();
+ QJsonDocument json = QJsonDocument::fromJson(serialized);
+ QJsonArray array2 = json.array();
+
+ QCOMPARE(array.size(), array2.size());
for (int i = 0; i < array.size(); ++i) {
QCOMPARE(array.at(i).type(), QJsonValue::Double);
QCOMPARE(array.at(i).toDouble(), (double)numbers[i]);
+ QCOMPARE(array2.at(i).type(), QJsonValue::Double);
+ QCOMPARE(array2.at(i).toDouble(), (double)numbers[i]);
}
}
@@ -242,18 +287,18 @@ void tst_QtJson::testNumbers()
0,
-1,
1,
- (1<<26),
- (1<<27),
- (1<<28),
- -(1<<26),
- -(1<<27),
- -(1<<28),
- (1<<26) - 1,
- (1<<27) - 1,
- (1<<28) - 1,
- -((1<<26) - 1),
- -((1<<27) - 1),
- -((1<<28) - 1),
+ (1UL<<54),
+ (1UL<<55),
+ (1UL<<56),
+ -(1UL<<54),
+ -(1UL<<55),
+ -(1UL<<56),
+ (1UL<<54) - 1,
+ (1UL<<55) - 1,
+ (1UL<<56) - 1,
+ -((1UL<<54) - 1),
+ -((1UL<<55) - 1),
+ -((1UL<<56) - 1),
1.1,
0.1,
-0.1,
@@ -266,9 +311,17 @@ void tst_QtJson::testNumbers()
QJsonArray array;
for (int i = 0; i < n; ++i)
array.append(numbers[i]);
+
+ QByteArray serialized = QJsonDocument(array).toJson();
+ QJsonDocument json = QJsonDocument::fromJson(serialized);
+ QJsonArray array2 = json.array();
+
+ QCOMPARE(array.size(), array2.size());
for (int i = 0; i < array.size(); ++i) {
QCOMPARE(array.at(i).type(), QJsonValue::Double);
QCOMPARE(array.at(i).toDouble(), numbers[i]);
+ QCOMPARE(array2.at(i).type(), QJsonValue::Double);
+ QCOMPARE(array2.at(i).toDouble(), numbers[i]);
}
}