Fix parsing empty json object. (#4868)

* Fix parsing empty json object.

* Better error message.
This commit is contained in:
Jiaming Yuan 2019-09-18 03:31:46 -04:00 committed by GitHub
parent 006eb80578
commit 57106a3459
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View File

@ -384,8 +384,18 @@ void JsonReader::Error(std::string msg) const {
auto end = cursor_.Pos() + kExtend >= raw_str_.size() ?
raw_str_.size() : cursor_.Pos() + kExtend;
std::string const& raw_portion = raw_str_.substr(beg, end - beg);
std::string portion;
for (auto c : raw_portion) {
if (c == '\n') {
portion += "\\n";
} else {
portion += c;
}
}
msg += " ";
msg += raw_str_.substr(beg, end - beg);
msg += portion;
msg += '\n';
msg += " ";
@ -488,9 +498,12 @@ Json JsonReader::ParseArray() {
}
Json JsonReader::ParseObject() {
char ch = GetChar('{');
GetChar('{');
std::map<std::string, Json> data;
SkipSpaces();
char ch = PeekNextChar();
if (ch == '}') return Json(std::move(data));
while (true) {

View File

@ -214,6 +214,20 @@ TEST(Json, Null) {
ASSERT_TRUE(IsA<Null>(json["key"]));
}
TEST(Json, EmptyObject) {
std::string str = R"json(
{
"rank": 1,
"statistic": {
}
}
)json";
std::stringstream iss(str);
auto json = Json::Load(StringView{str.c_str(), str.size()});
ASSERT_TRUE(IsA<Object>(json["statistic"]));
}
TEST(Json, EmptyArray) {
std::string str = R"json(
{