Commit ba50fe7d authored by Milo Yip's avatar Milo Yip

Merge pull request #27 from miloyip/issue23errorcode

Issue23errorcode
parents d8ed6095 389fe87c
...@@ -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(is, writer)) { if (!reader.Parse(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();
} }
...@@ -864,10 +864,10 @@ public: ...@@ -864,10 +864,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_; }
...@@ -927,7 +927,7 @@ private: ...@@ -927,7 +927,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 {
//! Maps error code of parsing into error message.
/*!
\param parseErrorCode Error code obtained in parsing.
\return the error message.
\note User can make a copy of this function for localization.
Using switch-case is safer for future modification of error codes.
*/
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("Missing 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 be stored 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__
#include "../reader.h" // ParseErrorCode
///////////////////////////////////////////////////////////////////////////////
// RAPIDJSON_ERROR_CHARTYPE
//! Character type of error messages.
/*! The default charater type is char.
On Windows, user can define this macro as TCHAR for supporting both
unicode/non-unicode settings.
*/
#ifndef RAPIDJSON_ERROR_CHARTYPE
#define RAPIDJSON_ERROR_CHARTYPE char
#endif
///////////////////////////////////////////////////////////////////////////////
// RAPIDJSON_ERROR_STRING
//! Macro for converting string literial to RAPIDJSON_ERROR_CHARTYPE[].
/*! By default this conversion macro does nothing.
On Windows, user can define this macro as _T(x) for supporting both
unicode/non-unicode settings.
*/
#ifndef RAPIDJSON_ERROR_STRING
#define RAPIDJSON_ERROR_STRING(x) x
#endif
namespace rapidjson {
//! Function pointer type of GetParseError().
/*! This is the prototype for GetParseError_X(), where X is a locale.
User can dynamically change locale in runtime, e.g.:
\code
GetParseErrorFunc GetParseError = GetParseError_En; // or whatever
const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode());
\endcode
*/
typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode);
} // namespace rapidjson
#endif // RAPIDJSON_ERROR_ERROR_H__
This diff is collapsed.
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