summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/json/qjsonparser.cpp16
-rw-r--r--src/corelib/json/qjsonparser_p.h1
-rw-r--r--tests/auto/corelib/json/bom.json3
-rw-r--r--tests/auto/corelib/json/tst_qtjson.cpp16
4 files changed, 34 insertions, 2 deletions
diff --git a/src/corelib/json/qjsonparser.cpp b/src/corelib/json/qjsonparser.cpp
index 8c5693c9be..5fecb8d4e7 100644
--- a/src/corelib/json/qjsonparser.cpp
+++ b/src/corelib/json/qjsonparser.cpp
@@ -185,7 +185,16 @@ enum {
Quote = 0x22
};
-
+void Parser::eatBOM()
+{
+ // eat UTF-8 byte order mark
+ uchar utf8bom[3] = { 0xef, 0xbb, 0xbf };
+ if (end - json > 3 &&
+ (uchar)json[0] == utf8bom[0] &&
+ (uchar)json[1] == utf8bom[1] &&
+ (uchar)json[2] == utf8bom[2])
+ json += 3;
+}
bool Parser::eatSpace()
{
@@ -244,8 +253,10 @@ QJsonDocument Parser::parse(QJsonParseError *error)
current = sizeof(QJsonPrivate::Header);
+ eatBOM();
char token = nextToken();
- DEBUG << token;
+
+ DEBUG << hex << (uint)token;
if (token == BeginArray) {
if (!parseArray())
goto error;
@@ -253,6 +264,7 @@ QJsonDocument Parser::parse(QJsonParseError *error)
if (!parseObject())
goto error;
} else {
+ lastError = QJsonParseError::IllegalValue;
goto error;
}
diff --git a/src/corelib/json/qjsonparser_p.h b/src/corelib/json/qjsonparser_p.h
index 20e57dcf26..8085edb2e0 100644
--- a/src/corelib/json/qjsonparser_p.h
+++ b/src/corelib/json/qjsonparser_p.h
@@ -84,6 +84,7 @@ public:
private:
+ inline void eatBOM();
inline bool eatSpace();
inline char nextToken();
diff --git a/tests/auto/corelib/json/bom.json b/tests/auto/corelib/json/bom.json
new file mode 100644
index 0000000000..d1e8d90e28
--- /dev/null
+++ b/tests/auto/corelib/json/bom.json
@@ -0,0 +1,3 @@
+{
+ "info-version": "1.0"
+}
diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp
index 7a11b5aec2..84913f9289 100644
--- a/tests/auto/corelib/json/tst_qtjson.cpp
+++ b/tests/auto/corelib/json/tst_qtjson.cpp
@@ -125,6 +125,8 @@ private Q_SLOTS:
void testDetachBug();
void valueEquals();
+
+ void bom();
private:
QString testDataDir;
};
@@ -1908,5 +1910,19 @@ void TestQtJson::valueEquals()
QVERIFY(QJsonValue(QJsonObject()) != QJsonValue(QJsonArray()));
}
+void TestQtJson::bom()
+{
+ QFile file(testDataDir + "/bom.json");
+ file.open(QFile::ReadOnly);
+ QByteArray json = file.readAll();
+
+ // Import json document into a QJsonDocument
+ QJsonParseError error;
+ QJsonDocument doc = QJsonDocument::fromJson(json, &error);
+
+ QVERIFY(!doc.isNull());
+ QVERIFY(error.error == QJsonParseError::NoError);
+}
+
QTEST_MAIN(TestQtJson)
#include "tst_qtjson.moc"