forked from bblanchon/ArduinoJson
Fixed bug in parser when "null", "true" or "false" is mispelled
This commit is contained in:
@ -22,6 +22,7 @@ class JsonParser {
|
|||||||
bool isEnd() { return *_ptr == '\0'; }
|
bool isEnd() { return *_ptr == '\0'; }
|
||||||
|
|
||||||
bool skip(char charToSkip);
|
bool skip(char charToSkip);
|
||||||
|
bool skip(const char *wordToSkip);
|
||||||
void skipSpaces();
|
void skipSpaces();
|
||||||
|
|
||||||
void parseAnythingTo(JsonValue &destination);
|
void parseAnythingTo(JsonValue &destination);
|
||||||
|
@ -30,6 +30,15 @@ bool JsonParser::skip(char charToSkip) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool JsonParser::skip(const char *wordToSkip) {
|
||||||
|
const char *charToSkip = wordToSkip;
|
||||||
|
while (*charToSkip && *_ptr == *charToSkip) {
|
||||||
|
charToSkip++;
|
||||||
|
_ptr++;
|
||||||
|
}
|
||||||
|
return *charToSkip == '\0';
|
||||||
|
}
|
||||||
|
|
||||||
void JsonParser::parseAnythingTo(JsonValue &destination) {
|
void JsonParser::parseAnythingTo(JsonValue &destination) {
|
||||||
skipSpaces();
|
skipSpaces();
|
||||||
|
|
||||||
@ -96,13 +105,10 @@ JsonArray &JsonParser::parseArray() {
|
|||||||
void JsonParser::parseBooleanTo(JsonValue &destination) {
|
void JsonParser::parseBooleanTo(JsonValue &destination) {
|
||||||
bool value = *_ptr == 't';
|
bool value = *_ptr == 't';
|
||||||
|
|
||||||
// TODO: bug if string ends here !!!
|
if (skip(value ? "true" : "false"))
|
||||||
|
destination = value;
|
||||||
_ptr += value ? 4 : 5;
|
else
|
||||||
// 4 = strlen("true")
|
destination = JsonValue::invalid();
|
||||||
// 5 = strlen("false");
|
|
||||||
|
|
||||||
destination = value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonParser::parseNumberTo(JsonValue &destination) {
|
void JsonParser::parseNumberTo(JsonValue &destination) {
|
||||||
@ -121,9 +127,10 @@ void JsonParser::parseNumberTo(JsonValue &destination) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void JsonParser::parseNullTo(JsonValue &destination) {
|
void JsonParser::parseNullTo(JsonValue &destination) {
|
||||||
_ptr += 4; // strlen("null")
|
if (skip("null"))
|
||||||
|
destination = static_cast<const char *>(NULL);
|
||||||
destination = static_cast<const char *>(NULL);
|
else
|
||||||
|
destination = JsonValue::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject &JsonParser::parseObject() {
|
JsonObject &JsonParser::parseObject() {
|
||||||
|
@ -143,6 +143,21 @@ TEST_F(JsonParser_Array_Tests, TwoNulls) {
|
|||||||
secondElementMustBe(nullCharPtr);
|
secondElementMustBe(nullCharPtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(JsonParser_Array_Tests, IncompleteNull) {
|
||||||
|
whenInputIs("[nul!]");
|
||||||
|
parseMustFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(JsonParser_Array_Tests, IncompleteTrue) {
|
||||||
|
whenInputIs("[tru!]");
|
||||||
|
parseMustFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(JsonParser_Array_Tests, IncompleteFalse) {
|
||||||
|
whenInputIs("[fals!]");
|
||||||
|
parseMustFail();
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(JsonParser_Array_Tests, TwoStrings) {
|
TEST_F(JsonParser_Array_Tests, TwoStrings) {
|
||||||
whenInputIs("[\"hello\",\"world\"]");
|
whenInputIs("[\"hello\",\"world\"]");
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user