Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/lib_json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,18 @@ bool Reader::readArray(Token& token) {
currentValue().swapPayload(init);
currentValue().setOffsetStart(token.start_ - begin_);
skipSpaces();
if (features_.allowComments_) {
while (current_ != end_ && *current_ == '/' && (current_ + 1) != end_ &&
(current_[1] == '/' || current_[1] == '*')) {
Token comment;
if (!readToken(comment)) {
return addErrorAndRecover(
"Syntax error: value, object or array expected.", comment,
tokenArrayEnd);
}
skipSpaces();
Comment thread
greptile-apps[bot] marked this conversation as resolved.
}
}
if (current_ != end_ && *current_ == ']') // empty array
{
Token endArray;
Expand Down
91 changes: 91 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3007,6 +3007,11 @@ struct ReaderTest : JsonTest::TestCase {
JSONTEST_ASSERT(reader->parse(input, root));
}

template <typename Input>
void checkParse(Input&& input, bool collectComments) {
JSONTEST_ASSERT(reader->parse(input, root, collectComments));
}

template <typename Input>
void
checkParse(Input&& input,
Expand All @@ -3023,6 +3028,11 @@ struct ReaderTest : JsonTest::TestCase {
JSONTEST_ASSERT_EQUAL(formatted, reader->getFormattedErrorMessages());
}

template <typename Input>
void checkParseFailure(Input&& input, bool collectComments) {
JSONTEST_ASSERT(!reader->parse(input, root, collectComments));
}

std::unique_ptr<Json::Reader> reader{new Json::Reader()};
Json::Value root;
};
Expand Down Expand Up @@ -3095,6 +3105,87 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseComment) {
checkParse(" true //comment1\n//comment2\r//comment3\r\n");
}

JSONTEST_FIXTURE_LOCAL(ReaderTest, parseEmptyArrayWithComments) {
for (bool collectComments : {false, true}) {
for (const auto& doc :
{std::string("[ // line\n]"), std::string("[ /* block */ ]"),
std::string("[ // one\n/* two */ ]"),
std::string("{\"list\":[ // line\n]}"),
std::string("{\"list\":[ /* block */ ]}"),
std::string("{\"list\":[ // one\n/* two */ ]}")}) {
root = Json::Value();
checkParse(doc, collectComments);

const Json::Value& arrayValue =
doc[0] == '[' ? root : root[Json::StaticString("list")];
const std::size_t arrayStart = doc.find('[');
const std::size_t arrayLimit = doc.rfind(']') + 1;
JSONTEST_ASSERT(arrayValue.isArray());
JSONTEST_ASSERT_EQUAL(0U, arrayValue.size());
JSONTEST_ASSERT(!arrayValue.isValidIndex(0));
JSONTEST_ASSERT_EQUAL(static_cast<int>(arrayStart),
arrayValue.getOffsetStart());
JSONTEST_ASSERT_EQUAL(static_cast<int>(arrayLimit),
arrayValue.getOffsetLimit());
}
}
}

JSONTEST_FIXTURE_LOCAL(ReaderTest, parseArrayCommentBehaviorUnchanged) {
{
std::string doc = "[ /* before one */ 1 ]";
const std::size_t valueStart = doc.find('1');
const std::size_t valueLimit = valueStart + 1;
for (bool collectComments : {false, true}) {
root = Json::Value();
checkParse(doc, collectComments);
JSONTEST_ASSERT_EQUAL(1U, root.size());
JSONTEST_ASSERT_EQUAL(1, root[0].asInt());
JSONTEST_ASSERT_EQUAL(static_cast<int>(valueStart),
root[0].getOffsetStart());
JSONTEST_ASSERT_EQUAL(static_cast<int>(valueLimit),
root[0].getOffsetLimit());
JSONTEST_ASSERT_EQUAL(collectComments,
root[0].hasComment(Json::commentBefore));
if (collectComments) {
JSONTEST_ASSERT_STRING_EQUAL("/* before one */",
root[0].getComment(Json::commentBefore));
}
}
}
{
std::string doc = "// before\n[]// after\n";
root = Json::Value();
checkParse(doc, true);
JSONTEST_ASSERT(root.hasComment(Json::commentBefore));
JSONTEST_ASSERT_STRING_EQUAL("// before",
root.getComment(Json::commentBefore));
JSONTEST_ASSERT(root.hasComment(Json::commentAfterOnSameLine));
JSONTEST_ASSERT_STRING_EQUAL("// after",
root.getComment(Json::commentAfterOnSameLine));
}
}

JSONTEST_FIXTURE_LOCAL(ReaderTest, parseEmptyArrayCommentsDisallowed) {
Json::Features features = Json::Features::all();
features.allowComments_ = false;
setFeatures(features);

root = Json::Value();
checkParseFailure("[ // comment\n]", true);

root = Json::Value();
checkParseFailure("[ /* comment */ ]", false);
}

JSONTEST_FIXTURE_LOCAL(ReaderTest, parseEmptyArrayWithUnterminatedComment) {
root = Json::Value();
checkParseFailure("[ /*", true);
JSONTEST_ASSERT(root.isArray());
JSONTEST_ASSERT_EQUAL(0U, root.size());
JSONTEST_ASSERT(!root.isValidIndex(0));
}

JSONTEST_FIXTURE_LOCAL(ReaderTest, streamParseWithNoErrors) {
std::string styled = R"({ "property" : "value" })";
std::istringstream iss(styled);
Expand Down
Loading