forked from bblanchon/ArduinoJson
Allow string to be enclosed in single quotes
This commit is contained in:
@ -29,7 +29,6 @@ private:
|
|||||||
inline bool isLong();
|
inline bool isLong();
|
||||||
inline bool isNull();
|
inline bool isNull();
|
||||||
inline bool isSpace();
|
inline bool isSpace();
|
||||||
inline bool isString();
|
|
||||||
|
|
||||||
inline void skipOneChar();
|
inline void skipOneChar();
|
||||||
inline void skipSpaces();
|
inline void skipSpaces();
|
||||||
|
@ -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
|
return c == '\"' || c == '\'';
|
||||||
char* readPtr = start;
|
}
|
||||||
char* writePtr = start;
|
|
||||||
|
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;
|
char c;
|
||||||
|
|
||||||
do
|
for (;;)
|
||||||
{
|
{
|
||||||
c = *readPtr++;
|
c = *readPtr++;
|
||||||
|
|
||||||
if (c == '\"')
|
if (c == 0)
|
||||||
|
{
|
||||||
|
// premature ending
|
||||||
|
*endPtr = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c == stopChar)
|
||||||
|
{
|
||||||
|
// closing quote
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (c == '\\')
|
if (c == '\\')
|
||||||
{
|
{
|
||||||
|
// replace char
|
||||||
c = unescapeChar(*readPtr++);
|
c = unescapeChar(*readPtr++);
|
||||||
}
|
}
|
||||||
|
|
||||||
*writePtr++ = c;
|
*writePtr++ = c;
|
||||||
} while (c != 0);
|
}
|
||||||
|
|
||||||
|
// end the string here
|
||||||
*writePtr = 0;
|
*writePtr = 0;
|
||||||
|
|
||||||
*end = readPtr;
|
// update end ptr
|
||||||
|
*endPtr = readPtr;
|
||||||
|
|
||||||
return start;
|
return startPtr;
|
||||||
}
|
}
|
@ -75,11 +75,6 @@ bool JsonParser::isSpace()
|
|||||||
return *_ptr == ' ' || *_ptr == '\t' || *_ptr == '\n' || *_ptr == '\r';
|
return *_ptr == ' ' || *_ptr == '\t' || *_ptr == '\n' || *_ptr == '\r';
|
||||||
}
|
}
|
||||||
|
|
||||||
bool JsonParser::isString()
|
|
||||||
{
|
|
||||||
return *_ptr == '\"';
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonParser::skipOneChar()
|
void JsonParser::skipOneChar()
|
||||||
{
|
{
|
||||||
_ptr++;
|
_ptr++;
|
||||||
@ -109,10 +104,7 @@ JsonNode* JsonParser::parseAnything()
|
|||||||
if (isNull())
|
if (isNull())
|
||||||
return parseNull();
|
return parseNull();
|
||||||
|
|
||||||
if (isString())
|
|
||||||
return parseString();
|
return parseString();
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonNode* JsonParser::parseArray()
|
JsonNode* JsonParser::parseArray()
|
||||||
|
@ -16,19 +16,26 @@ protected:
|
|||||||
EXPECT_STREQ(expected, _result);
|
EXPECT_STREQ(expected, _result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
char _jsonString[256];
|
char _jsonString[256];
|
||||||
StaticJsonBuffer<42> _jsonBuffer;
|
StaticJsonBuffer<42> _jsonBuffer;
|
||||||
const char* _result;
|
const char* _result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
TEST_F(JsonParser_String_Tests, EmptyString)
|
TEST_F(JsonParser_String_Tests, EmptyDoubleQuotedString)
|
||||||
{
|
{
|
||||||
whenInputIs("\"\"");
|
whenInputIs("\"\"");
|
||||||
outputMustBe("");
|
outputMustBe("");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonParser_String_Tests, SimpleString)
|
TEST_F(JsonParser_String_Tests, EmptySingleQuotedString)
|
||||||
|
{
|
||||||
|
whenInputIs("''");
|
||||||
|
outputMustBe("");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(JsonParser_String_Tests, SimpleDoubleQuotedString)
|
||||||
{
|
{
|
||||||
whenInputIs("\"hello world\"");
|
whenInputIs("\"hello world\"");
|
||||||
outputMustBe("hello world");
|
outputMustBe("hello world");
|
||||||
@ -72,31 +79,31 @@ TEST_F(JsonParser_String_Tests, EscapedReverseSolidus)
|
|||||||
|
|
||||||
TEST_F(JsonParser_String_Tests, EscapedBackspace)
|
TEST_F(JsonParser_String_Tests, EscapedBackspace)
|
||||||
{
|
{
|
||||||
whenInputIs("\"hello \\bworld\\b");
|
whenInputIs("\"hello \\bworld\\b\"");
|
||||||
outputMustBe("hello \bworld\b");
|
outputMustBe("hello \bworld\b");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonParser_String_Tests, EscapedFormfeed)
|
TEST_F(JsonParser_String_Tests, EscapedFormfeed)
|
||||||
{
|
{
|
||||||
whenInputIs("\"hello \\fworld\\f");
|
whenInputIs("\"hello \\fworld\\f\"");
|
||||||
outputMustBe("hello \fworld\f");
|
outputMustBe("hello \fworld\f");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonParser_String_Tests, EscapedNewline)
|
TEST_F(JsonParser_String_Tests, EscapedNewline)
|
||||||
{
|
{
|
||||||
whenInputIs("\"hello \\nworld\\n");
|
whenInputIs("\"hello \\nworld\\n\"");
|
||||||
outputMustBe("hello \nworld\n");
|
outputMustBe("hello \nworld\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonParser_String_Tests, EscapedCarriageReturn)
|
TEST_F(JsonParser_String_Tests, EscapedCarriageReturn)
|
||||||
{
|
{
|
||||||
whenInputIs("\"hello \\rworld\\r");
|
whenInputIs("\"hello \\rworld\\r\"");
|
||||||
outputMustBe("hello \rworld\r");
|
outputMustBe("hello \rworld\r");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonParser_String_Tests, EscapedTab)
|
TEST_F(JsonParser_String_Tests, EscapedTab)
|
||||||
{
|
{
|
||||||
whenInputIs("\"hello \\tworld\\t");
|
whenInputIs("\"hello \\tworld\\t\"");
|
||||||
outputMustBe("hello \tworld\t");
|
outputMustBe("hello \tworld\t");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user