json encoder: Harmonize number serialization with ES6
Ensures that numbers representable as 64-bit integer are not printed using exponent notation. Some JSON implementations such as the one of the Go standard library expect this in the default conversion to int. Change-Id: Ic3ac718b7fd36462b4fcabbfb100a528a87798c8 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
5ca7d56aca
commit
38550c562d
@ -38,6 +38,7 @@
|
|||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
#include <qlocale.h>
|
#include <qlocale.h>
|
||||||
#include "qjsonwriter_p.h"
|
#include "qjsonwriter_p.h"
|
||||||
#include "qjson_p.h"
|
#include "qjson_p.h"
|
||||||
@ -129,10 +130,12 @@ static void valueToJson(const QJsonPrivate::Base *b, const QJsonPrivate::Value &
|
|||||||
break;
|
break;
|
||||||
case QJsonValue::Double: {
|
case QJsonValue::Double: {
|
||||||
const double d = v.toDouble(b);
|
const double d = v.toDouble(b);
|
||||||
if (qIsFinite(d)) // +2 to format to ensure the expected precision
|
if (qIsFinite(d)) { // +2 to format to ensure the expected precision
|
||||||
json += QByteArray::number(d, 'g', QLocale::FloatingPointShortest);
|
const double abs = std::abs(d);
|
||||||
else
|
json += QByteArray::number(d, abs == static_cast<quint64>(abs) ? 'f' : 'g', QLocale::FloatingPointShortest);
|
||||||
|
} else {
|
||||||
json += "null"; // +INF || -INF || NaN (see RFC4627#section2.4)
|
json += "null"; // +INF || -INF || NaN (see RFC4627#section2.4)
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case QJsonValue::String:
|
case QJsonValue::String:
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
#include "qjsonobject.h"
|
#include "qjsonobject.h"
|
||||||
#include "qjsonvalue.h"
|
#include "qjsonvalue.h"
|
||||||
#include "qjsondocument.h"
|
#include "qjsondocument.h"
|
||||||
|
#include "qregularexpression.h"
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
#define INVALID_UNICODE "\xCE\xBA\xE1"
|
#define INVALID_UNICODE "\xCE\xBA\xE1"
|
||||||
@ -49,6 +50,7 @@ private Q_SLOTS:
|
|||||||
void testNumbers();
|
void testNumbers();
|
||||||
void testNumbers_2();
|
void testNumbers_2();
|
||||||
void testNumbers_3();
|
void testNumbers_3();
|
||||||
|
void testNumbers_4();
|
||||||
|
|
||||||
void testObjectSimple();
|
void testObjectSimple();
|
||||||
void testObjectSmallKeys();
|
void testObjectSmallKeys();
|
||||||
@ -375,6 +377,33 @@ void tst_QtJson::testNumbers_3()
|
|||||||
QVERIFY(d1_1 != d2_1);
|
QVERIFY(d1_1 != d2_1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tst_QtJson::testNumbers_4()
|
||||||
|
{
|
||||||
|
// no exponent notation used to print numbers between -2^64 and 2^64
|
||||||
|
QJsonArray array;
|
||||||
|
array << QJsonValue(+1000000000000000.0);
|
||||||
|
array << QJsonValue(-1000000000000000.0);
|
||||||
|
array << QJsonValue(+9007199254740992.0);
|
||||||
|
array << QJsonValue(-9007199254740992.0);
|
||||||
|
array << QJsonValue(+9223372036854775808.0);
|
||||||
|
array << QJsonValue(-9223372036854775808.0);
|
||||||
|
array << QJsonValue(+18446744073709551616.0);
|
||||||
|
array << QJsonValue(-18446744073709551616.0);
|
||||||
|
const QByteArray json(QJsonDocument(array).toJson());
|
||||||
|
const QByteArray expected =
|
||||||
|
"[\n"
|
||||||
|
" 1000000000000000,\n"
|
||||||
|
" -1000000000000000,\n"
|
||||||
|
" 9007199254740992,\n"
|
||||||
|
" -9007199254740992,\n"
|
||||||
|
" 9223372036854776000,\n"
|
||||||
|
" -9223372036854776000,\n"
|
||||||
|
" 18446744073709552000,\n"
|
||||||
|
" -18446744073709552000\n"
|
||||||
|
"]\n";
|
||||||
|
QCOMPARE(json, expected);
|
||||||
|
}
|
||||||
|
|
||||||
void tst_QtJson::testObjectSimple()
|
void tst_QtJson::testObjectSimple()
|
||||||
{
|
{
|
||||||
QJsonObject object;
|
QJsonObject object;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user