Explorar o código

Allow string to be enclosed in single quotes

Benoit Blanchon %!s(int64=11) %!d(string=hai) anos
pai
achega
bbc2aa4f2a

+ 0 - 1
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();

+ 41 - 9
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;
 }

+ 1 - 9
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()

+ 14 - 7
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");
 }