Fixed parsing when opening brace/bracket is missing

This commit is contained in:
Benoit Blanchon
2014-10-31 16:50:21 +01:00
parent 2b5b8fb4c5
commit 74b4544560
3 changed files with 13 additions and 7 deletions

View File

@ -74,9 +74,9 @@ void JsonParser::parseAnythingTo(JsonValue &destination) {
}
JsonArray &JsonParser::parseArray() {
skip('[');
if (!skip('[')) return JsonArray::invalid(); // missing opening bracket
if (isEnd()) return JsonArray::invalid();
if (isEnd()) return JsonArray::invalid(); // end of stream
JsonArray &array = _buffer->createArray();
if (skip(']')) return array; // empty array
@ -127,7 +127,7 @@ void JsonParser::parseNullTo(JsonValue &destination) {
}
JsonObject &JsonParser::parseObject() {
skip('{');
if (!skip('{')) return JsonObject::invalid(); // missing opening brace
if (isEnd()) return JsonObject::invalid(); // premature ending

View File

@ -58,9 +58,13 @@ TEST_F(JsonParser_Array_Tests, EmptyArray) {
sizeMustBe(0);
}
TEST_F(JsonParser_Array_Tests, MissingOpeningBracket) {
whenInputIs("]");
parseMustFail();
}
TEST_F(JsonParser_Array_Tests, ArrayWithNoEnd) {
whenInputIs("[");
parseMustFail();
}

View File

@ -45,22 +45,24 @@ TEST_F(JsonParser_Object_Test, EmptyObject) {
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, MissingOpeningBrace) {
whenInputIs("}");
parseMustFail();
}
TEST_F(JsonParser_Object_Test, MissingClosingBrace) {
whenInputIs("{");
parseMustFail();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, MissingColonAndValue) {
whenInputIs("{\"key\"}");
parseMustFail();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, MissingQuotesAndColonAndValue) {
whenInputIs("{key}");
parseMustFail();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, OneString) {