Fix parsing empty json object. (#4868)
* Fix parsing empty json object. * Better error message.
This commit is contained in:
parent
006eb80578
commit
57106a3459
@ -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) {
|
||||
|
||||
@ -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(
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user