Correctly parse json documents with a leading BOM
A leading byte order mark is valid in utf-8 and we should parse documents starting with those correctly. Change-Id: Id85398ff6e05b93ceefbaf4a6de5571d5e61ca13 Reviewed-by: Denis Dzyubenko <denis.dzyubenko@nokia.com>
This commit is contained in:
parent
f6bd10b8ff
commit
1f9a958e36
@ -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;
|
||||
}
|
||||
|
||||
|
@ -84,6 +84,7 @@ public:
|
||||
|
||||
|
||||
private:
|
||||
inline void eatBOM();
|
||||
inline bool eatSpace();
|
||||
inline char nextToken();
|
||||
|
||||
|
3
tests/auto/corelib/json/bom.json
Normal file
3
tests/auto/corelib/json/bom.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"info-version": "1.0"
|
||||
}
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user