From e2d614b6a9e88c90331e3de9bb339a2ed609ea95 Mon Sep 17 00:00:00 2001 From: "Darryl L. Miles" Date: Sun, 10 Feb 2013 07:18:42 +0000 Subject: Json writer, only emit floating point finite numbers, ignore INF/NaN My interpretation of RFC4627, Section 2.4 "Numbers" of: Numeric values that cannot be represented as sequences of digits (such as Infinity and NaN) are not permitted. I have also verified this matter with NodeJS JSON.stringify() that emitting a null is consistent behavior with a JSON implementation written in JavaScript. Previously Qt would emit: { plusInfinity: inf, minusInfinity: -inf, notANumber: nan } Which maybe turned into a string values of "inf", "-inf", "nan" by the receiving parser. Now it returns the JSON null value just like NodeJS JSON.stringify(). Change-Id: I9f9c17f12b2606280806c47a9d90465c4ba5f786 Reviewed-by: Thiago Macieira --- src/corelib/json/qjsonwriter.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/corelib/json/qjsonwriter.cpp') diff --git a/src/corelib/json/qjsonwriter.cpp b/src/corelib/json/qjsonwriter.cpp index 76f62241e3..c284f76cbb 100644 --- a/src/corelib/json/qjsonwriter.cpp +++ b/src/corelib/json/qjsonwriter.cpp @@ -169,9 +169,14 @@ static void valueToJson(const QJsonPrivate::Base *b, const QJsonPrivate::Value & case QJsonValue::Bool: json += v.toBoolean() ? "true" : "false"; break; - case QJsonValue::Double: - json += QByteArray::number(v.toDouble(b)); + case QJsonValue::Double: { + const double d = v.toDouble(b); + if (qIsFinite(d)) + json += QByteArray::number(d); + else + json += "null"; // +INF || -INF || NaN (see RFC4627#section2.4) break; + } case QJsonValue::String: json += '"'; json += escapedString(v.toString(b)); -- cgit v1.2.3