From 573f87d81363a303b7005f999a283db624201db8 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Tue, 10 Nov 2015 15:41:01 +0100 Subject: Use "shortest" double conversion for JSON Task-number: QTBUG-42419 Change-Id: I649c4de96a2e41078e139c0f382e5a6cf96d3bb9 Reviewed-by: Lars Knoll --- src/corelib/json/qjsonwriter.cpp | 3 ++- tests/auto/corelib/json/json.pro | 4 ++++ tests/auto/corelib/json/tst_qtjson.cpp | 16 +++++++++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/corelib/json/qjsonwriter.cpp b/src/corelib/json/qjsonwriter.cpp index 45a05e93a3..0ab8b2bb80 100644 --- a/src/corelib/json/qjsonwriter.cpp +++ b/src/corelib/json/qjsonwriter.cpp @@ -32,6 +32,7 @@ ** ****************************************************************************/ +#include #include "qjsonwriter_p.h" #include "qjson_p.h" #include "private/qutfcodec_p.h" @@ -123,7 +124,7 @@ static void valueToJson(const QJsonPrivate::Base *b, const QJsonPrivate::Value & case QJsonValue::Double: { const double d = v.toDouble(b); if (qIsFinite(d)) // +2 to format to ensure the expected precision - json += QByteArray::number(d, 'g', std::numeric_limits::digits10 + 2); // ::digits10 is 15 + json += QByteArray::number(d, 'g', QLocale::FloatingPointShortest); else json += "null"; // +INF || -INF || NaN (see RFC4627#section2.4) break; diff --git a/tests/auto/corelib/json/json.pro b/tests/auto/corelib/json/json.pro index a8224fd201..b3038d7ca1 100644 --- a/tests/auto/corelib/json/json.pro +++ b/tests/auto/corelib/json/json.pro @@ -6,4 +6,8 @@ CONFIG += testcase !android:TESTDATA += bom.json test.json test.bjson test3.json test2.json else:RESOURCES += json.qrc +!contains(QT_CONFIG, doubleconversion):!contains(QT_CONFIG, system-doubleconversion) { + DEFINES += QT_NO_DOUBLECONVERSION +} + SOURCES += tst_qtjson.cpp diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index e8d836683e..aebf332fe3 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -1408,9 +1408,7 @@ void tst_QtJson::toJsonLargeNumericValues() " \"Array\": [\n" " 1.234567,\n" " 1.7976931348623157e+308,\n" - // ((4.9406564584124654e-324 == 5e-324) == true) - // I can only think JavaScript has a special formatter to - // emit this value for this IEEE754 bit pattern. +#ifdef QT_NO_DOUBLECONVERSION // "shortest" double conversion is not very short then " 4.9406564584124654e-324,\n" " 2.2250738585072014e-308,\n" " 1.7976931348623157e+308,\n" @@ -1421,6 +1419,18 @@ void tst_QtJson::toJsonLargeNumericValues() " -1.7976931348623157e+308,\n" " -2.2204460492503131e-16,\n" " -4.9406564584124654e-324,\n" +#else + " 5e-324,\n" + " 2.2250738585072014e-308,\n" + " 1.7976931348623157e+308,\n" + " 2.220446049250313e-16,\n" + " 5e-324,\n" + " 0,\n" + " -2.2250738585072014e-308,\n" + " -1.7976931348623157e+308,\n" + " -2.220446049250313e-16,\n" + " -5e-324,\n" +#endif " 0,\n" " 9007199254740992,\n" " -9007199254740992\n" -- cgit v1.2.3