Skip to content

Commit

Permalink
Merge pull request #439 from pah/fix/437-keep-value-on-error
Browse files Browse the repository at this point in the history
Keep Document value unchanged on parse error, fixes #437
  • Loading branch information
miloyip committed Oct 8, 2015
2 parents 221887d + 41dd68f commit 1c76070
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 1 addition & 2 deletions include/rapidjson/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -1886,14 +1886,13 @@ class GenericDocument : public GenericValue<Encoding, Allocator> {
*/
template <unsigned parseFlags, typename SourceEncoding, typename InputStream>
GenericDocument& ParseStream(InputStream& is) {
ValueType::SetNull(); // Remove existing root if exist
GenericReader<SourceEncoding, Encoding, StackAllocator> reader(
stack_.HasAllocator() ? &stack_.GetAllocator() : 0);
ClearStackOnExit scope(*this);
parseResult_ = reader.template Parse<parseFlags>(is, *this);
if (parseResult_) {
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.
ValueType::operator=(*stack_.template Pop<ValueType>(1));// Move value from stack to document
}
return *this;
}
Expand Down
15 changes: 15 additions & 0 deletions test/unittest/documenttest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ TEST(Document, Parse) {
ParseTest<CrtAllocator, CrtAllocator>();
}

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",
Expand Down

0 comments on commit 1c76070

Please sign in to comment.