From fa123699d32948eb4f539934a4574e3b73ce6c3d Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Wed, 7 Oct 2015 21:48:39 +0200 Subject: [PATCH 1/2] Keep Document value unchanged on parse error, fixes #437 Keeping the DOM unchanged in case of an error is the intended behaviour according to the [documentation] [1]. Instead of forcing the value to `kNullType` before starting the parsing, store the parsed value upon success via regular move. [1]: https://miloyip.github.io/rapidjson/md_doc_dom.html#ParseError --- include/rapidjson/document.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 0a5a7b2..516cb5e 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -1886,14 +1886,13 @@ public: */ template GenericDocument& ParseStream(InputStream& is) { - ValueType::SetNull(); // Remove existing root if exist GenericReader reader( stack_.HasAllocator() ? &stack_.GetAllocator() : 0); ClearStackOnExit scope(*this); parseResult_ = reader.template Parse(is, *this); if (parseResult_) { RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object - this->RawAssign(*stack_.template Pop(1)); // Add this-> to prevent issue 13. + ValueType::operator=(*stack_.template Pop(1));// Move value from stack to document } return *this; } From 41dd68f092d9a90e8cf87ba78a4af7dbccf261d9 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Wed, 7 Oct 2015 21:50:14 +0200 Subject: [PATCH 2/2] add simple test for unchanged DOM after parse error --- test/unittest/documenttest.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/unittest/documenttest.cpp b/test/unittest/documenttest.cpp index 810a99c..83325a7 100644 --- a/test/unittest/documenttest.cpp +++ b/test/unittest/documenttest.cpp @@ -95,6 +95,21 @@ TEST(Document, Parse) { ParseTest(); } +TEST(Document, UnchangedOnParseError) { + Document doc; + doc.SetArray().PushBack(0, doc.GetAllocator()); + + doc.Parse("{]"); + EXPECT_TRUE(doc.HasParseError()); + EXPECT_TRUE(doc.IsArray()); + EXPECT_EQ(doc.Size(), 1u); + + doc.Parse("{}"); + EXPECT_FALSE(doc.HasParseError()); + EXPECT_TRUE(doc.IsObject()); + EXPECT_EQ(doc.MemberCount(), 0u); +} + static FILE* OpenEncodedFile(const char* filename) { const char *paths[] = { "encodings/%s",