From 38550c562d918e783bb609622bc8fb46de1bfec4 Mon Sep 17 00:00:00 2001 From: Marius Kittler Date: Mon, 27 Feb 2017 22:16:41 +0100 Subject: 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 --- src/corelib/json/qjsonwriter.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/corelib/json/qjsonwriter.cpp b/src/corelib/json/qjsonwriter.cpp index b1544c749d..12ce20ef09 100644 --- a/src/corelib/json/qjsonwriter.cpp +++ b/src/corelib/json/qjsonwriter.cpp @@ -38,6 +38,7 @@ ** ****************************************************************************/ +#include #include #include "qjsonwriter_p.h" #include "qjson_p.h" @@ -129,10 +130,12 @@ static void valueToJson(const QJsonPrivate::Base *b, const QJsonPrivate::Value & break; 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', QLocale::FloatingPointShortest); - else + if (qIsFinite(d)) { // +2 to format to ensure the expected precision + const double abs = std::abs(d); + json += QByteArray::number(d, abs == static_cast(abs) ? 'f' : 'g', QLocale::FloatingPointShortest); + } else { json += "null"; // +INF || -INF || NaN (see RFC4627#section2.4) + } break; } case QJsonValue::String: -- cgit v1.2.3