diff --git a/include/ArduinoJson/Internals/JsonParser.h b/include/ArduinoJson/Internals/JsonParser.h index 5bbca4e6..9302e7c1 100644 --- a/include/ArduinoJson/Internals/JsonParser.h +++ b/include/ArduinoJson/Internals/JsonParser.h @@ -29,7 +29,6 @@ private: inline bool isLong(); inline bool isNull(); inline bool isSpace(); - inline bool isString(); inline void skipOneChar(); inline void skipSpaces(); diff --git a/src/Internals/EscapedString.cpp b/src/Internals/EscapedString.cpp index 0d3692e4..fc7c816e 100644 --- a/src/Internals/EscapedString.cpp +++ b/src/Internals/EscapedString.cpp @@ -58,31 +58,63 @@ static char unescapeChar(char c) } } -char* EscapedString::extractFrom(char* input, char** end) +static inline bool isQuote(char c) { - char* start = input + 1; // skip quote - char* readPtr = start; - char* writePtr = start; + return c == '\"' || c == '\''; +} + +char* EscapedString::extractFrom(char* input, char** endPtr) +{ + char firstChar = *input; + char stopChar; + char* startPtr; + + if (isQuote(firstChar)) + { + stopChar = firstChar; // closing quote is the same as opening quote + startPtr = input + 1; // skip the quote + } + else + { + stopChar = ':'; // assume we're parsing a key in an object + startPtr = input; // no quote to skip + } + + char* readPtr = startPtr; + char* writePtr = startPtr; char c; - do + for (;;) { c = *readPtr++; - if (c == '\"') + if (c == 0) + { + // premature ending + *endPtr = 0; + return 0; + } + + if (c == stopChar) + { + // closing quote break; + } if (c == '\\') { + // replace char c = unescapeChar(*readPtr++); } *writePtr++ = c; - } while (c != 0); + } + // end the string here *writePtr = 0; - *end = readPtr; + // update end ptr + *endPtr = readPtr; - return start; + return startPtr; } \ No newline at end of file diff --git a/src/Internals/JsonParser.cpp b/src/Internals/JsonParser.cpp index 7953955b..4c986f36 100644 --- a/src/Internals/JsonParser.cpp +++ b/src/Internals/JsonParser.cpp @@ -75,11 +75,6 @@ bool JsonParser::isSpace() return *_ptr == ' ' || *_ptr == '\t' || *_ptr == '\n' || *_ptr == '\r'; } -bool JsonParser::isString() -{ - return *_ptr == '\"'; -} - void JsonParser::skipOneChar() { _ptr++; @@ -109,10 +104,7 @@ JsonNode* JsonParser::parseAnything() if (isNull()) return parseNull(); - if (isString()) - return parseString(); - - return 0; + return parseString(); } JsonNode* JsonParser::parseArray() diff --git a/test/JsonParser_String_Tests.cpp b/test/JsonParser_String_Tests.cpp index 488adb8b..31f828d9 100644 --- a/test/JsonParser_String_Tests.cpp +++ b/test/JsonParser_String_Tests.cpp @@ -16,19 +16,26 @@ protected: EXPECT_STREQ(expected, _result); } +private: char _jsonString[256]; StaticJsonBuffer<42> _jsonBuffer; const char* _result; }; -TEST_F(JsonParser_String_Tests, EmptyString) +TEST_F(JsonParser_String_Tests, EmptyDoubleQuotedString) { whenInputIs("\"\""); outputMustBe(""); } -TEST_F(JsonParser_String_Tests, SimpleString) +TEST_F(JsonParser_String_Tests, EmptySingleQuotedString) +{ + whenInputIs("''"); + outputMustBe(""); +} + +TEST_F(JsonParser_String_Tests, SimpleDoubleQuotedString) { whenInputIs("\"hello world\""); outputMustBe("hello world"); @@ -72,31 +79,31 @@ TEST_F(JsonParser_String_Tests, EscapedReverseSolidus) TEST_F(JsonParser_String_Tests, EscapedBackspace) { - whenInputIs("\"hello \\bworld\\b"); + whenInputIs("\"hello \\bworld\\b\""); outputMustBe("hello \bworld\b"); } TEST_F(JsonParser_String_Tests, EscapedFormfeed) { - whenInputIs("\"hello \\fworld\\f"); + whenInputIs("\"hello \\fworld\\f\""); outputMustBe("hello \fworld\f"); } TEST_F(JsonParser_String_Tests, EscapedNewline) { - whenInputIs("\"hello \\nworld\\n"); + whenInputIs("\"hello \\nworld\\n\""); outputMustBe("hello \nworld\n"); } TEST_F(JsonParser_String_Tests, EscapedCarriageReturn) { - whenInputIs("\"hello \\rworld\\r"); + whenInputIs("\"hello \\rworld\\r\""); outputMustBe("hello \rworld\r"); } TEST_F(JsonParser_String_Tests, EscapedTab) { - whenInputIs("\"hello \\tworld\\t"); + whenInputIs("\"hello \\tworld\\t\""); outputMustBe("hello \tworld\t"); }