From 6348de873724fc613446c46b5accfabab7afaaea Mon Sep 17 00:00:00 2001 From: "Darryl L. Miles" Date: Sun, 10 Feb 2013 09:02:00 +0000 Subject: Json writer, emit full precision for floating point Number types Previously Qt JSON writer would only emit 6 digits of precision as this is the default with the formatter. However with testing against NodeJS JSON.stringify() this behavior is inconsistent with the defacto standard JSON implementation and conveys a loss of precision. Change-Id: Ie1845a6e0ee0b4c05f63ec0062f372e891855f0b Reviewed-by: Thiago Macieira --- src/corelib/json/qjson_p.h | 1 + src/corelib/json/qjsonwriter.cpp | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/json/qjson_p.h b/src/corelib/json/qjson_p.h index 8d6735273c..96a11cd40e 100644 --- a/src/corelib/json/qjson_p.h +++ b/src/corelib/json/qjson_p.h @@ -63,6 +63,7 @@ #include #include +#include QT_BEGIN_NAMESPACE diff --git a/src/corelib/json/qjsonwriter.cpp b/src/corelib/json/qjsonwriter.cpp index c284f76cbb..8426b351f6 100644 --- a/src/corelib/json/qjsonwriter.cpp +++ b/src/corelib/json/qjsonwriter.cpp @@ -171,8 +171,8 @@ static void valueToJson(const QJsonPrivate::Base *b, const QJsonPrivate::Value & break; case QJsonValue::Double: { const double d = v.toDouble(b); - if (qIsFinite(d)) - json += QByteArray::number(d); + if (qIsFinite(d)) // +2 to format to ensure the expected precision + json += QByteArray::number(d, 'g', std::numeric_limits::digits10 + 2); // ::digits10 is 15 else json += "null"; // +INF || -INF || NaN (see RFC4627#section2.4) break; -- cgit v1.2.3