Skip to content

Commit

Permalink
Merge pull request #83 from miloyip/issue75stopwhendone
Browse files Browse the repository at this point in the history
Issue75stopwhendone
  • Loading branch information
miloyip committed Jul 28, 2014
2 parents c545895 + 3d10608 commit e6f3446
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
18 changes: 13 additions & 5 deletions include/rapidjson/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ enum ParseFlag {
kParseDefaultFlags = 0, //!< Default parse flags. Non-destructive parsing. Text strings are decoded into allocated buffer.
kParseInsituFlag = 1, //!< In-situ(destructive) parsing.
kParseValidateEncodingFlag = 2, //!< Validate encoding of JSON strings.
kParseIterativeFlag = 4 //!< Iterative(constant complexity in terms of function call stack size) parsing.
kParseIterativeFlag = 4, //!< Iterative(constant complexity in terms of function call stack size) parsing.
kParseStopWhenDoneFlag = 8 //!< After parsing a complete JSON root from stream, stop further processing the rest of stream. When this flag is used, parser will not generate kParseErrorDocumentRootNotSingular error.
};

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -309,11 +310,13 @@ class GenericReader {
}
RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);

SkipWhitespace(is);
if (!(parseFlags & kParseStopWhenDoneFlag)) {
SkipWhitespace(is);

if (is.Peek() != '\0') {
RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentRootNotSingular, is.Tell());
RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
if (is.Peek() != '\0') {
RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentRootNotSingular, is.Tell());
RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
}
}
}

Expand Down Expand Up @@ -1192,6 +1195,11 @@ class GenericReader {
}

state = d;

// Do not further consume streams if a root JSON has been parsed.
if ((parseFlags & kParseStopWhenDoneFlag) && state == IterativeParsingFinishState)
break;

SkipWhitespace(is);
}

Expand Down
33 changes: 33 additions & 0 deletions test/unittest/readertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,39 @@ TEST(Reader, Parse_EmptyObject) {
EXPECT_EQ(2u, h.step_);
}

struct ParseMultipleRootHandler : BaseReaderHandler<> {
ParseMultipleRootHandler() : step_(0) {}

bool Default() { ADD_FAILURE(); return false; }
bool StartObject() { EXPECT_EQ(0u, step_); step_++; return true; }
bool EndObject(SizeType) { EXPECT_EQ(1u, step_); step_++; return true; }
bool StartArray() { EXPECT_EQ(2u, step_); step_++; return true; }
bool EndArray(SizeType) { EXPECT_EQ(3u, step_); step_++; return true; }

unsigned step_;
};

template <unsigned parseFlags>
void TestMultipleRoot() {
StringStream s("{}[] a");
ParseMultipleRootHandler h;
Reader reader;
EXPECT_TRUE(reader.Parse<parseFlags>(s, h));
EXPECT_EQ(2u, h.step_);
EXPECT_TRUE(reader.Parse<parseFlags>(s, h));
EXPECT_EQ(4u, h.step_);
EXPECT_EQ(' ', s.Take());
EXPECT_EQ('a', s.Take());
}

TEST(Reader, Parse_MultipleRoot) {
TestMultipleRoot<kParseStopWhenDoneFlag>();
}

TEST(Reader, ParseIterative_MultipleRoot) {
TestMultipleRoot<kParseIterativeFlag | kParseStopWhenDoneFlag>();
}

#define TEST_ERROR(errorCode, str) \
{ \
char buffer[1001]; \
Expand Down

0 comments on commit e6f3446

Please sign in to comment.