diff --git a/JsonGenerator/EscapedString.cpp b/JsonGenerator/EscapedString.cpp new file mode 100644 index 00000000..ac4605a4 --- /dev/null +++ b/JsonGenerator/EscapedString.cpp @@ -0,0 +1,65 @@ +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#include "EscapedString.h" + + +size_t EscapedString::printTo(Print& p) const +{ + const char* s = rawString; + + if (!s) + { + return p.print("null"); + } + + size_t n = 0; + + n += p.write('\"'); + + while (*s) + { + switch (*s) + { + case '"': + n += p.print("\\\""); + break; + + case '\\': + n += p.print("\\\\"); + break; + + case '\b': + n += p.print("\\b"); + break; + + case '\f': + n += p.print("\\f"); + break; + + case '\n': + n += p.print("\\n"); + break; + + case '\r': + n += p.print("\\r"); + break; + + case '\t': + n += p.print("\\t"); + break; + + default: + n += p.write(*s); + break; + } + + s++; + } + + n += p.write('\"'); + + return n; +} \ No newline at end of file diff --git a/JsonGenerator/EscapedString.h b/JsonGenerator/EscapedString.h new file mode 100644 index 00000000..91f90d0a --- /dev/null +++ b/JsonGenerator/EscapedString.h @@ -0,0 +1,24 @@ +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#pragma once + +#include "Print.h" + +class EscapedString +{ +public: + EscapedString(const char* s) + : rawString(s) + { + + } + + size_t printTo(Print&) const; + +private: + const char* rawString; +}; + diff --git a/JsonGenerator/JsonValue.cpp b/JsonGenerator/JsonValue.cpp index 41d74e64..e8f11316 100644 --- a/JsonGenerator/JsonValue.cpp +++ b/JsonGenerator/JsonValue.cpp @@ -3,6 +3,7 @@ * Benoit Blanchon 2014 - MIT License */ +#include "EscapedString.h" #include "JsonValue.h" using namespace ArduinoJson::Generator; @@ -27,58 +28,6 @@ size_t JsonValue::printPrintableTo(const Content& c, Print& p) size_t JsonValue::printStringTo(const Content& c, Print& p) { - const char* s = c.asString; - - if (!s) - { - return p.print("null"); - } - - size_t n = 0; - - n += p.write('\"'); - - while (*s) - { - switch (*s) - { - case '"': - n += p.print("\\\""); - break; - - case '\\': - n += p.print("\\\\"); - break; - - case '\b': - n += p.print("\\b"); - break; - - case '\f': - n += p.print("\\f"); - break; - - case '\n': - n += p.print("\\n"); - break; - - case '\r': - n += p.print("\\r"); - break; - - case '\t': - n += p.print("\\t"); - break; - - default: - n += p.write(*s); - break; - } - - s++; - } - - n += p.write('\"'); - - return n; + EscapedString s(c.asString); + return s.printTo(p); } \ No newline at end of file diff --git a/JsonGeneratorTests/JsonGeneratorTests.vcxproj b/JsonGeneratorTests/JsonGeneratorTests.vcxproj index fb4d2a4e..b7c0ac24 100644 --- a/JsonGeneratorTests/JsonGeneratorTests.vcxproj +++ b/JsonGeneratorTests/JsonGeneratorTests.vcxproj @@ -84,6 +84,7 @@ + @@ -93,6 +94,7 @@ + diff --git a/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters b/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters index 128d8b3e..99116900 100644 --- a/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters +++ b/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters @@ -36,6 +36,9 @@ Source Files + + Source Files + @@ -59,5 +62,8 @@ Header Files + + Header Files + \ No newline at end of file