Commit 9d47c623 authored by Milo Yip's avatar Milo Yip

Merge pull request #480 from pah/feature/document-parseresult

GenericDocument: add implicit conversion to ParseResult
parents 8ec389f1 9378001e
...@@ -1986,6 +1986,17 @@ public: ...@@ -1986,6 +1986,17 @@ public:
//! Get the position of last parsing error in input, 0 otherwise. //! Get the position of last parsing error in input, 0 otherwise.
size_t GetErrorOffset() const { return parseResult_.Offset(); } size_t GetErrorOffset() const { return parseResult_.Offset(); }
//! Implicit conversion to get the last parse result
/*! \return \ref ParseResult of the last parse operation
\code
Document doc;
ParseResult ok = doc.Parse(json);
if (!ok)
printf( "JSON parse error: %s (%u)\n", GetParseError_En(ok.Code()), ok.Offset());
\endcode
*/
operator ParseResult() const { return parseResult_; }
//!@} //!@}
//! Get the allocator of this document. //! Get the allocator of this document.
......
...@@ -28,6 +28,7 @@ void ParseCheck(DocumentType& doc) { ...@@ -28,6 +28,7 @@ void ParseCheck(DocumentType& doc) {
typedef typename DocumentType::ValueType ValueType; typedef typename DocumentType::ValueType ValueType;
EXPECT_FALSE(doc.HasParseError()); EXPECT_FALSE(doc.HasParseError());
EXPECT_TRUE((ParseResult)doc);
EXPECT_TRUE(doc.IsObject()); EXPECT_TRUE(doc.IsObject());
...@@ -99,13 +100,18 @@ TEST(Document, UnchangedOnParseError) { ...@@ -99,13 +100,18 @@ TEST(Document, UnchangedOnParseError) {
Document doc; Document doc;
doc.SetArray().PushBack(0, doc.GetAllocator()); doc.SetArray().PushBack(0, doc.GetAllocator());
doc.Parse("{]"); ParseResult err = doc.Parse("{]");
EXPECT_TRUE(doc.HasParseError()); EXPECT_TRUE(doc.HasParseError());
EXPECT_EQ(err.Code(), doc.GetParseError());
EXPECT_EQ(err.Offset(), doc.GetErrorOffset());
EXPECT_TRUE(doc.IsArray()); EXPECT_TRUE(doc.IsArray());
EXPECT_EQ(doc.Size(), 1u); EXPECT_EQ(doc.Size(), 1u);
doc.Parse("{}"); err = doc.Parse("{}");
EXPECT_FALSE(doc.HasParseError()); EXPECT_FALSE(doc.HasParseError());
EXPECT_FALSE(err.IsError());
EXPECT_EQ(err.Code(), doc.GetParseError());
EXPECT_EQ(err.Offset(), doc.GetErrorOffset());
EXPECT_TRUE(doc.IsObject()); EXPECT_TRUE(doc.IsObject());
EXPECT_EQ(doc.MemberCount(), 0u); EXPECT_EQ(doc.MemberCount(), 0u);
} }
......
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