mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Added a tests of a string with a double quote in it
This commit is contained in:
		| @@ -30,6 +30,13 @@ namespace JsonGeneratorTests | |||||||
|             AssertJsonIs("[\"hello\"]"); |             AssertJsonIs("[\"hello\"]"); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         TEST_METHOD(AddOneStringContainingDoubleQuote) | ||||||
|  |         { | ||||||
|  |             arr.add("\""); | ||||||
|  |  | ||||||
|  |             AssertJsonIs("[\"\\\"\"]"); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         TEST_METHOD(AddTwoStrings) |         TEST_METHOD(AddTwoStrings) | ||||||
|         { |         { | ||||||
|             arr.add("hello"); |             arr.add("hello"); | ||||||
|   | |||||||
| @@ -6,7 +6,7 @@ void JsonObjectBase::writeObjectTo(ObjectContainer& obj, StringBuilder& sb) | |||||||
|     { |     { | ||||||
|     case JSON_STRING: |     case JSON_STRING: | ||||||
|         if (obj.value.string) |         if (obj.value.string) | ||||||
|             sb.append("\"%s\"", obj.value.string); |             sb.appendEscaped(obj.value.string); | ||||||
|         else |         else | ||||||
|             sb.append("null"); |             sb.append("null"); | ||||||
|         break; |         break; | ||||||
|   | |||||||
| @@ -11,4 +11,23 @@ void StringBuilder::append(const char* format, ...) | |||||||
|     va_end(args); |     va_end(args); | ||||||
|  |  | ||||||
|     length += strlen(tail); |     length += strlen(tail); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void StringBuilder::appendEscaped(const char* s) | ||||||
|  | { | ||||||
|  |     if (length > capacity - 3) return; | ||||||
|  |  | ||||||
|  |     buffer[length++] = '"'; | ||||||
|  |  | ||||||
|  |     while (*s && length<capacity-2) | ||||||
|  |     { | ||||||
|  |         if (*s == '"') | ||||||
|  |             buffer[length++] = '\\'; | ||||||
|  |  | ||||||
|  |         buffer[length++] = *s; | ||||||
|  |  | ||||||
|  |         s++; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     buffer[length++] = '"'; | ||||||
| } | } | ||||||
| @@ -18,6 +18,7 @@ public: | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     void append(const char* format, ...); |     void append(const char* format, ...); | ||||||
|  |     void appendEscaped(const char* s); | ||||||
|  |  | ||||||
| private: | private: | ||||||
|     char* buffer; |     char* buffer; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user