Commit 3693d61f authored by Milo Yip's avatar Milo Yip

Add parse error codes and API for converting error code to text.

Parse errors is represented as enum type `ParseErrorCode`.
Error texts are optional for user.
Added  `GetParseError_En()` in `error/en.h`, user can localize this file
into other files. User may dynamically change the locale in runtime.
parent 813eaf4e
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "rapidjson/writer.h" #include "rapidjson/writer.h"
#include "rapidjson/filereadstream.h" #include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h" #include "rapidjson/filewritestream.h"
#include "rapidjson/error/en.h"
using namespace rapidjson; using namespace rapidjson;
...@@ -23,7 +24,7 @@ int main(int, char*[]) { ...@@ -23,7 +24,7 @@ int main(int, char*[]) {
// JSON reader parse from the input stream and let writer generate the output. // JSON reader parse from the input stream and let writer generate the output.
if (!reader.Parse<0>(is, writer)) { if (!reader.Parse<0>(is, writer)) {
fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), reader.GetParseError()); fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), GetParseError_En(reader.GetParseErrorCode()));
return 1; return 1;
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "rapidjson/prettywriter.h" #include "rapidjson/prettywriter.h"
#include "rapidjson/filereadstream.h" #include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h" #include "rapidjson/filewritestream.h"
#include "rapidjson/error/en.h"
using namespace rapidjson; using namespace rapidjson;
...@@ -21,7 +22,7 @@ int main(int, char*[]) { ...@@ -21,7 +22,7 @@ int main(int, char*[]) {
// JSON reader parse from the input stream and let writer generate the output. // JSON reader parse from the input stream and let writer generate the output.
if (!reader.Parse<kParseValidateEncodingFlag>(is, writer)) { if (!reader.Parse<kParseValidateEncodingFlag>(is, writer)) {
fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), reader.GetParseError()); fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), GetParseError_En(reader.GetParseErrorCode()));
return 1; return 1;
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "rapidjson/filereadstream.h" #include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h" #include "rapidjson/filewritestream.h"
#include "rapidjson/encodedstream.h" // NEW #include "rapidjson/encodedstream.h" // NEW
#include "rapidjson/error/en.h"
#ifdef _WIN32 #ifdef _WIN32
#include <fcntl.h> #include <fcntl.h>
#include <io.h> #include <io.h>
...@@ -47,7 +48,7 @@ int main(int, char*[]) { ...@@ -47,7 +48,7 @@ int main(int, char*[]) {
// JSON reader parse from the input stream and let writer generate the output. // JSON reader parse from the input stream and let writer generate the output.
//if (!reader.Parse<kParseValidateEncodingFlag>(is, writer)) { //if (!reader.Parse<kParseValidateEncodingFlag>(is, writer)) {
if (!reader.Parse<kParseValidateEncodingFlag>(eis, writer)) { // CHANGED if (!reader.Parse<kParseValidateEncodingFlag>(eis, writer)) { // CHANGED
fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), reader.GetParseError()); fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), GetParseError_En(reader.GetParseErrorCode()));
return 1; return 1;
} }
......
...@@ -788,7 +788,7 @@ public: ...@@ -788,7 +788,7 @@ public:
/*! \param allocator Optional allocator for allocating stack memory. /*! \param allocator Optional allocator for allocating stack memory.
\param stackCapacity Initial capacity of stack in bytes. \param stackCapacity Initial capacity of stack in bytes.
*/ */
GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity) : stack_(allocator, stackCapacity), parseError_(0), errorOffset_(0) {} GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity) : stack_(allocator, stackCapacity), parseErrorCode_(kParseErrorNone), errorOffset_(0) {}
//! Parse JSON text from an input stream. //! Parse JSON text from an input stream.
/*! \tparam parseFlags Combination of ParseFlag. /*! \tparam parseFlags Combination of ParseFlag.
...@@ -802,11 +802,11 @@ public: ...@@ -802,11 +802,11 @@ public:
if (reader.template Parse<parseFlags>(is, *this)) { if (reader.template Parse<parseFlags>(is, *this)) {
RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object
this->RawAssign(*stack_.template Pop<ValueType>(1)); // Add this-> to prevent issue 13. this->RawAssign(*stack_.template Pop<ValueType>(1)); // Add this-> to prevent issue 13.
parseError_ = 0; parseErrorCode_ = kParseErrorNone;
errorOffset_ = 0; errorOffset_ = 0;
} }
else { else {
parseError_ = reader.GetParseError(); parseErrorCode_ = reader.GetParseErrorCode();
errorOffset_ = reader.GetErrorOffset(); errorOffset_ = reader.GetErrorOffset();
ClearStack(); ClearStack();
} }
...@@ -851,10 +851,10 @@ public: ...@@ -851,10 +851,10 @@ public:
} }
//! Whether a parse error was occured in the last parsing. //! Whether a parse error was occured in the last parsing.
bool HasParseError() const { return parseError_ != 0; } bool HasParseError() const { return parseErrorCode_ != kParseErrorNone; }
//! Get the message of parsing error. //! Get the message of parsing error.
const char* GetParseError() const { return parseError_; } ParseErrorCode GetParseError() const { return parseErrorCode_; }
//! Get the offset in character of the parsing error. //! Get the offset in character of the parsing error.
size_t GetErrorOffset() const { return errorOffset_; } size_t GetErrorOffset() const { return errorOffset_; }
...@@ -914,7 +914,7 @@ private: ...@@ -914,7 +914,7 @@ private:
static const size_t kDefaultStackCapacity = 1024; static const size_t kDefaultStackCapacity = 1024;
internal::Stack<Allocator> stack_; internal::Stack<Allocator> stack_;
const char* parseError_; ParseErrorCode parseErrorCode_;
size_t errorOffset_; size_t errorOffset_;
}; };
......
#ifndef RAPIDJSON_ERROR_EN_H__
#define RAPIDJSON_ERROR_EN_H__
#include "error.h"
namespace rapidjson {
inline const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En(ParseErrorCode parseErrorCode) {
switch (parseErrorCode) {
case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error.");
case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty.");
case kParseErrorDocumentRootNotObjectOrArray: return RAPIDJSON_ERROR_STRING("The document root must be either object or array.");
case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not follow by other values.");
case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value.");
case kParseErrorObjectMissName: return RAPIDJSON_ERROR_STRING("Missing a name for object member.");
case kParseErrorObjectMissColon: return RAPIDJSON_ERROR_STRING("Missing a colon after a name of object member.");
case kParseErrorObjectMissCommaOrCurlyBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or '}' after an object member.");
case kParseErrorArrayMissCommaOrSquareBracket: return RAPIDJSON_ERROR_STRING("Must be a comma or ']' after an array element.");
case kParseErrorStringUnicodeEscapeInvalidHex: return RAPIDJSON_ERROR_STRING("Incorrect hex digit after \\u escape in string.");
case kParseErrorStringUnicodeSurrogateInvalid: return RAPIDJSON_ERROR_STRING("The surrogate pair in string is invalid.");
case kParseErrorStringEscapeInvalid: return RAPIDJSON_ERROR_STRING("Invalid escape character in string.");
case kParseErrorStringMissQuotationMark: return RAPIDJSON_ERROR_STRING("Missing a closing quotation mark in string.");
case kParseErrorStringInvalidEncoding: return RAPIDJSON_ERROR_STRING("Invalid encoidng in string.");
case kParesErrorNumberTooBig: return RAPIDJSON_ERROR_STRING("Number too big to store in double.");
case kParseErrorNumberMissFraction: return RAPIDJSON_ERROR_STRING("Miss fraction part in number.");
case kParseErrorNumberMissExponent: return RAPIDJSON_ERROR_STRING("Miss exponent in number.");
default:
return RAPIDJSON_ERROR_STRING("Unknown error.");
}
}
} // namespace rapidjson
#endif // RAPIDJSON_ERROR_EN_H__
#ifndef RAPIDJSON_ERROR_ERROR_H__
#define RAPIDJSON_ERROR_ERROR_H__
// For example, on Windows, user can define this macro as TCHAR
#ifndef RAPIDJSON_ERROR_CHARTYPE
#define RAPIDJSON_ERROR_CHARTYPE char
#endif
// For example, on Windows, user can define this macro as _T(x)
#ifndef RAPIDJSON_ERROR_STRING
#define RAPIDJSON_ERROR_STRING(x) x
#endif
namespace rapidjson {
// User can dynamically change locale in runtime, e.g.:
// GetParseErrorFunc GetParseError = GetParseError_En; // or whatever
// const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode());
typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode);
} // namespace rapidjson
#endif // RAPIDJSON_ERROR_ERROR_H__
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment