diff options
author | Darryl L. Miles <darryl.miles@darrylmiles.org> | 2013-02-10 07:18:42 +0000 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-04-12 22:33:13 +0200 |
commit | e2d614b6a9e88c90331e3de9bb339a2ed609ea95 (patch) | |
tree | 63040ef66ecfff3d779ddcd2d79ec201750ac949 | |
parent | c002a274262808a7328dd59c31f0666d20072d47 (diff) |
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 <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/json/qjson_p.h | 1 | ||||
-rw-r--r-- | src/corelib/json/qjsonwriter.cpp | 9 | ||||
-rw-r--r-- | tests/auto/corelib/json/tst_qtjson.cpp | 30 |
3 files changed, 38 insertions, 2 deletions
diff --git a/src/corelib/json/qjson_p.h b/src/corelib/json/qjson_p.h index 81439a00ce..8d6735273c 100644 --- a/src/corelib/json/qjson_p.h +++ b/src/corelib/json/qjson_p.h @@ -60,6 +60,7 @@ #include <qatomic.h> #include <qstring.h> #include <qendian.h> +#include <qnumeric.h> #include <limits.h> 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)); diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index 6811551769..7cdf6deb87 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -44,6 +44,7 @@ #include "qjsonobject.h" #include "qjsonvalue.h" #include "qjsondocument.h" +#include <limits> #define INVALID_UNICODE "\357\277\277" // "\uffff" #define UNICODE_DJE "\320\202" // Character from the Serbian Cyrillic alphabet @@ -94,6 +95,7 @@ private Q_SLOTS: void toVariantList(); void toJson(); + void toJsonSillyNumericValues(); void fromJson(); void fromJsonErrors(); void fromBinary(); @@ -1080,6 +1082,34 @@ void tst_QtJson::toJson() } } +void tst_QtJson::toJsonSillyNumericValues() +{ + QJsonObject object; + QJsonArray array; + array.append(QJsonValue(std::numeric_limits<double>::infinity())); // encode to: null + array.append(QJsonValue(-std::numeric_limits<double>::infinity())); // encode to: null + array.append(QJsonValue(std::numeric_limits<double>::quiet_NaN())); // encode to: null + object.insert("Array", array); + + QByteArray json = QJsonDocument(object).toJson(); + + QByteArray expected = + "{\n" + " \"Array\": [\n" + " null,\n" + " null,\n" + " null\n" + " ]\n" + "}\n"; + + QCOMPARE(json, expected); + + QJsonDocument doc; + doc.setObject(object); + json = doc.toJson(); + QCOMPARE(json, expected); +} + void tst_QtJson::fromJson() { { |