QJsonParseError improvements
- added human-readable error message - improved enum value names Change-Id: I86d4bb419f9581f85d61b6e090048f1943017f9e Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
parent
4535913c4f
commit
7ecbc49c55
@ -63,14 +63,16 @@ struct Q_CORE_EXPORT QJsonParseError
|
||||
UnterminatedArray,
|
||||
MissingValueSeparator,
|
||||
IllegalValue,
|
||||
EndOfNumber,
|
||||
TerminationByNumber,
|
||||
IllegalNumber,
|
||||
StringEscapeSequence,
|
||||
StringUTF8Scan,
|
||||
EndOfString,
|
||||
IllegalEscapeSequence,
|
||||
IllegalUTF8String,
|
||||
UnterminatedString,
|
||||
MissingObject
|
||||
};
|
||||
|
||||
QString errorString() const;
|
||||
|
||||
int offset;
|
||||
ParseError error;
|
||||
};
|
||||
|
@ -39,6 +39,9 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
#include <qcoreapplication.h>
|
||||
#endif
|
||||
#include <qdebug.h>
|
||||
#include "qjsonparser_p.h"
|
||||
#include "qjson_p.h"
|
||||
@ -57,6 +60,80 @@ static int indent = 0;
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
// error strings for the JSON parser
|
||||
#define JSONERR_OK QT_TRANSLATE_NOOP("QJsonParseError", "no error occurred")
|
||||
#define JSONERR_UNTERM_OBJ QT_TRANSLATE_NOOP("QJsonParseError", "unterminated object")
|
||||
#define JSONERR_MISS_NSEP QT_TRANSLATE_NOOP("QJsonParseError", "missing name separator")
|
||||
#define JSONERR_UNTERM_AR QT_TRANSLATE_NOOP("QJsonParseError", "unterminated array")
|
||||
#define JSONERR_MISS_VSEP QT_TRANSLATE_NOOP("QJsonParseError", "missing value separator")
|
||||
#define JSONERR_ILLEGAL_VAL QT_TRANSLATE_NOOP("QJsonParseError", "illegal value")
|
||||
#define JSONERR_END_OF_NUM QT_TRANSLATE_NOOP("QJsonParseError", "invalid termination by number")
|
||||
#define JSONERR_ILLEGAL_NUM QT_TRANSLATE_NOOP("QJsonParseError", "illegal number")
|
||||
#define JSONERR_STR_ESC_SEQ QT_TRANSLATE_NOOP("QJsonParseError", "invalid escape sequence")
|
||||
#define JSONERR_STR_UTF8 QT_TRANSLATE_NOOP("QJsonParseError", "invalid UTF8 string")
|
||||
#define JSONERR_UTERM_STR QT_TRANSLATE_NOOP("QJsonParseError", "unterminated string")
|
||||
#define JSONERR_MISS_OBJ QT_TRANSLATE_NOOP("QJsonParseError", "object is missing after a comma")
|
||||
|
||||
/*!
|
||||
\class QJsonParseError
|
||||
\ingroup json
|
||||
\reentrant
|
||||
\since 5.0
|
||||
|
||||
\brief The QJsonParseError class is used to report errors during JSON parsing.
|
||||
*/
|
||||
|
||||
/*!
|
||||
Returns the human-readable message appropriate to the reported JSON parsing error.
|
||||
*/
|
||||
QString QJsonParseError::errorString() const
|
||||
{
|
||||
const char *sz = "";
|
||||
switch (error) {
|
||||
case NoError:
|
||||
sz = JSONERR_OK;
|
||||
break;
|
||||
case UnterminatedObject:
|
||||
sz = JSONERR_UNTERM_OBJ;
|
||||
break;
|
||||
case MissingNameSeparator:
|
||||
sz = JSONERR_MISS_NSEP;
|
||||
break;
|
||||
case UnterminatedArray:
|
||||
sz = JSONERR_UNTERM_AR;
|
||||
break;
|
||||
case MissingValueSeparator:
|
||||
sz = JSONERR_MISS_VSEP;
|
||||
break;
|
||||
case IllegalValue:
|
||||
sz = JSONERR_ILLEGAL_VAL;
|
||||
break;
|
||||
case TerminationByNumber:
|
||||
sz = JSONERR_END_OF_NUM;
|
||||
break;
|
||||
case IllegalNumber:
|
||||
sz = JSONERR_ILLEGAL_NUM;
|
||||
break;
|
||||
case IllegalEscapeSequence:
|
||||
sz = JSONERR_STR_ESC_SEQ;
|
||||
break;
|
||||
case IllegalUTF8String:
|
||||
sz = JSONERR_STR_UTF8;
|
||||
break;
|
||||
case UnterminatedString:
|
||||
sz = JSONERR_UTERM_STR;
|
||||
break;
|
||||
case MissingObject:
|
||||
sz = JSONERR_MISS_OBJ;
|
||||
break;
|
||||
}
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
return QCoreApplication::translate("QJsonParseError", sz);
|
||||
#else
|
||||
return QLatin1String(sz);
|
||||
#endif
|
||||
}
|
||||
|
||||
using namespace QJsonPrivate;
|
||||
|
||||
Parser::Parser(const char *json, int length)
|
||||
@ -524,7 +601,7 @@ bool Parser::parseNumber(QJsonPrivate::Value *val, int baseOffset)
|
||||
}
|
||||
|
||||
if (json >= end) {
|
||||
lastError = QJsonParseError::EndOfNumber;
|
||||
lastError = QJsonParseError::TerminationByNumber;
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -717,12 +794,12 @@ bool Parser::parseString(bool *latin1)
|
||||
break;
|
||||
else if (*json == '\\') {
|
||||
if (!scanEscapeSequence(json, end, &ch)) {
|
||||
lastError = QJsonParseError::StringEscapeSequence;
|
||||
lastError = QJsonParseError::IllegalEscapeSequence;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!scanUtf8Char(json, end, &ch)) {
|
||||
lastError = QJsonParseError::StringUTF8Scan;
|
||||
lastError = QJsonParseError::IllegalUTF8String;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -737,7 +814,7 @@ bool Parser::parseString(bool *latin1)
|
||||
++json;
|
||||
DEBUG << "end of string";
|
||||
if (json >= end) {
|
||||
lastError = QJsonParseError::EndOfString;
|
||||
lastError = QJsonParseError::UnterminatedString;
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -764,12 +841,12 @@ bool Parser::parseString(bool *latin1)
|
||||
break;
|
||||
else if (*json == '\\') {
|
||||
if (!scanEscapeSequence(json, end, &ch)) {
|
||||
lastError = QJsonParseError::StringEscapeSequence;
|
||||
lastError = QJsonParseError::IllegalEscapeSequence;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!scanUtf8Char(json, end, &ch)) {
|
||||
lastError = QJsonParseError::StringUTF8Scan;
|
||||
lastError = QJsonParseError::IllegalUTF8String;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -785,7 +862,7 @@ bool Parser::parseString(bool *latin1)
|
||||
++json;
|
||||
|
||||
if (json >= end) {
|
||||
lastError = QJsonParseError::EndOfString;
|
||||
lastError = QJsonParseError::UnterminatedString;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -1186,7 +1186,7 @@ void TestQtJson::fromJsonErrors()
|
||||
QByteArray json = "[\n 11111";
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json, &error);
|
||||
QVERIFY(doc.isEmpty());
|
||||
QCOMPARE(error.error, QJsonParseError::EndOfNumber);
|
||||
QCOMPARE(error.error, QJsonParseError::TerminationByNumber);
|
||||
QCOMPARE(error.offset, 11);
|
||||
}
|
||||
{
|
||||
@ -1210,7 +1210,7 @@ void TestQtJson::fromJsonErrors()
|
||||
QByteArray json = "[\n \"\\u12\"]";
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json, &error);
|
||||
QVERIFY(doc.isEmpty());
|
||||
QCOMPARE(error.error, QJsonParseError::StringEscapeSequence);
|
||||
QCOMPARE(error.error, QJsonParseError::IllegalEscapeSequence);
|
||||
QCOMPARE(error.offset, 11);
|
||||
}
|
||||
{
|
||||
@ -1218,7 +1218,7 @@ void TestQtJson::fromJsonErrors()
|
||||
QByteArray json = "[\n \"foo" INVALID_UNICODE "bar\"]";
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json, &error);
|
||||
QVERIFY(doc.isEmpty());
|
||||
QCOMPARE(error.error, QJsonParseError::StringUTF8Scan);
|
||||
QCOMPARE(error.error, QJsonParseError::IllegalUTF8String);
|
||||
QCOMPARE(error.offset, 13);
|
||||
}
|
||||
{
|
||||
@ -1226,7 +1226,7 @@ void TestQtJson::fromJsonErrors()
|
||||
QByteArray json = "[\n \"";
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json, &error);
|
||||
QVERIFY(doc.isEmpty());
|
||||
QCOMPARE(error.error, QJsonParseError::EndOfString);
|
||||
QCOMPARE(error.error, QJsonParseError::UnterminatedString);
|
||||
QCOMPARE(error.offset, 8);
|
||||
}
|
||||
{
|
||||
@ -1234,7 +1234,7 @@ void TestQtJson::fromJsonErrors()
|
||||
QByteArray json = "[\n \"c" UNICODE_DJE "a\\u12\"]";
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json, &error);
|
||||
QVERIFY(doc.isEmpty());
|
||||
QCOMPARE(error.error, QJsonParseError::StringEscapeSequence);
|
||||
QCOMPARE(error.error, QJsonParseError::IllegalEscapeSequence);
|
||||
QCOMPARE(error.offset, 15);
|
||||
}
|
||||
{
|
||||
@ -1242,7 +1242,7 @@ void TestQtJson::fromJsonErrors()
|
||||
QByteArray json = "[\n \"c" UNICODE_DJE "a" INVALID_UNICODE "bar\"]";
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json, &error);
|
||||
QVERIFY(doc.isEmpty());
|
||||
QCOMPARE(error.error, QJsonParseError::StringUTF8Scan);
|
||||
QCOMPARE(error.error, QJsonParseError::IllegalUTF8String);
|
||||
QCOMPARE(error.offset, 14);
|
||||
}
|
||||
{
|
||||
@ -1250,7 +1250,7 @@ void TestQtJson::fromJsonErrors()
|
||||
QByteArray json = "[\n \"c" UNICODE_DJE "a ]";
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json, &error);
|
||||
QVERIFY(doc.isEmpty());
|
||||
QCOMPARE(error.error, QJsonParseError::EndOfString);
|
||||
QCOMPARE(error.error, QJsonParseError::UnterminatedString);
|
||||
QCOMPARE(error.offset, 14);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user