diff --git a/JsonGenerator/IndentedPrintDecorator.cpp b/JsonGenerator/IndentedPrintDecorator.cpp new file mode 100644 index 00000000..d8680121 --- /dev/null +++ b/JsonGenerator/IndentedPrintDecorator.cpp @@ -0,0 +1,40 @@ +#include "IndentedPrintDecorator.h" + +void IndentedPrintDecorator::indent() +{ + currentTabCount++; +} + +void IndentedPrintDecorator::unindent() +{ + currentTabCount--; +} + +size_t IndentedPrintDecorator::write(uint8_t c) +{ + size_t n = 0; + + if (isNewLine) + n += writeCurrentTabs(); + + n += sink.write(c); + + isNewLine = c == '\n'; + + return n; +} + +size_t IndentedPrintDecorator::writeCurrentTabs() +{ + return writeTabs(currentTabCount); +} + +size_t IndentedPrintDecorator::writeTabs(int count) +{ + size_t n = 0; + + for (int i = 0; i + @@ -25,6 +26,7 @@ + diff --git a/JsonGenerator/JsonGenerator.vcxproj.filters b/JsonGenerator/JsonGenerator.vcxproj.filters index c01fc7a3..e7639326 100644 --- a/JsonGenerator/JsonGenerator.vcxproj.filters +++ b/JsonGenerator/JsonGenerator.vcxproj.filters @@ -48,6 +48,9 @@ Header Files + + Header Files + @@ -71,5 +74,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/JsonGenerator/JsonPrintable.h b/JsonGenerator/JsonPrintable.h index 3d8dfc9e..b2f1a47b 100644 --- a/JsonGenerator/JsonPrintable.h +++ b/JsonGenerator/JsonPrintable.h @@ -26,12 +26,18 @@ namespace ArduinoJson return printTo(sb); } - size_t prettyPrintTo(Print& p) const + size_t prettyPrintTo(IndentedPrintDecorator& p) const { PrettyPrintDecorator decorator(p); return printTo(decorator); } + size_t prettyPrintTo(Print& p) const + { + IndentedPrintDecorator decorator(p); + return printTo(decorator); + } + virtual size_t printTo(Print& p) const = 0; }; } diff --git a/JsonGenerator/PrettyPrintDecorator.cpp b/JsonGenerator/PrettyPrintDecorator.cpp index 4edf4326..83e07b36 100644 --- a/JsonGenerator/PrettyPrintDecorator.cpp +++ b/JsonGenerator/PrettyPrintDecorator.cpp @@ -51,18 +51,20 @@ size_t PrettyPrintDecorator::handleMarkupChar(uint8_t c) size_t PrettyPrintDecorator::handleBlockOpen(uint8_t c) { - size_t n = inEmptyBlock() ? breakThenWrite(c) : writeChar(c); - - indent++; - - return n; + return indentIfNeeded() + writeChar(c); } size_t PrettyPrintDecorator::handleBlockClose(uint8_t c) -{ - indent--; - - return inEmptyBlock() ? writeChar(c) : breakThenWrite(c); +{ + if (inEmptyBlock()) + { + return writeChar(c); + } + else + { + sink.unindent(); + return breakThenWrite(c); + } } size_t PrettyPrintDecorator::handleColumn() @@ -77,7 +79,7 @@ size_t PrettyPrintDecorator::handleComma() size_t PrettyPrintDecorator::handleQuoteOpen() { - size_t n = inEmptyBlock() ? breakThenWrite('"') : writeChar('"'); + size_t n = indentIfNeeded() + writeChar('"'); inString = true; @@ -86,15 +88,13 @@ size_t PrettyPrintDecorator::handleQuoteOpen() size_t PrettyPrintDecorator::handleNormalChar(uint8_t c) { - return inEmptyBlock() ? breakThenWrite(c) : writeChar(c); + return indentIfNeeded() + writeChar(c); } -size_t PrettyPrintDecorator::breakAndIndent() +size_t PrettyPrintDecorator::indentIfNeeded() { - size_t n = writeChar('\n'); + if (!inEmptyBlock()) return 0; - for (int i = 0; i < indent; i++) - n += writeChar(' '); - - return n; + sink.indent(); + return sink.println(); } \ No newline at end of file diff --git a/JsonGenerator/PrettyPrintDecorator.h b/JsonGenerator/PrettyPrintDecorator.h index f9ff538c..e0aa46e7 100644 --- a/JsonGenerator/PrettyPrintDecorator.h +++ b/JsonGenerator/PrettyPrintDecorator.h @@ -1,4 +1,3 @@ -#pragma once /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License @@ -7,6 +6,7 @@ #pragma once #include "Print.h" +#include "IndentedPrintDecorator.h" namespace ArduinoJson { @@ -16,8 +16,8 @@ namespace ArduinoJson { public: - PrettyPrintDecorator(Print& p) - : indent(0), sink(p) + PrettyPrintDecorator(IndentedPrintDecorator& p) + : sink(p) { previousChar = 0; inString = false; @@ -25,10 +25,9 @@ namespace ArduinoJson virtual size_t write(uint8_t); - private: - int indent; + private: uint8_t previousChar; - Print& sink; + IndentedPrintDecorator& sink; bool inString; bool inEmptyBlock() @@ -45,17 +44,16 @@ namespace ArduinoJson size_t handleComma(); size_t handleQuoteOpen(); size_t handleNormalChar(uint8_t); - - size_t breakAndIndent(); + size_t indentIfNeeded(); size_t breakThenWrite(uint8_t c) { - return breakAndIndent() + writeChar(c); + return sink.println() + writeChar(c); } size_t writeThenBreak(uint8_t c) { - return writeChar(c) + breakAndIndent(); + return writeChar(c) + sink.println(); } size_t writeChar(uint8_t c) diff --git a/JsonGenerator/Print.cpp b/JsonGenerator/Print.cpp index 9688914d..a0ae630c 100644 --- a/JsonGenerator/Print.cpp +++ b/JsonGenerator/Print.cpp @@ -32,4 +32,9 @@ size_t Print::print(long value) return print(tmp); } +size_t Print::println() +{ + return /*write('\r') +*/ write('\n'); +} + #endif \ No newline at end of file diff --git a/JsonGenerator/Print.h b/JsonGenerator/Print.h index 85bd8e7d..1fb62ba8 100644 --- a/JsonGenerator/Print.h +++ b/JsonGenerator/Print.h @@ -19,6 +19,7 @@ public: size_t print(const char[]); size_t print(double, int = 2); size_t print(long); + size_t println(); }; #else diff --git a/JsonGeneratorTests/PrettyPrint_Array_Tests.cpp b/JsonGeneratorTests/PrettyPrint_Array_Tests.cpp index 28c76bb8..b8d558bd 100644 --- a/JsonGeneratorTests/PrettyPrint_Array_Tests.cpp +++ b/JsonGeneratorTests/PrettyPrint_Array_Tests.cpp @@ -76,7 +76,8 @@ namespace JsonGeneratorTests void whenInputIs(const char input[]) { StringBuilder sb(buffer, sizeof(buffer)); - PrettyPrintDecorator decorator(sb); + IndentedPrintDecorator indentedPrint(sb); + PrettyPrintDecorator decorator(indentedPrint); returnValue = decorator.print(input); } diff --git a/JsonGeneratorTests/PrettyPrint_Object_Tests.cpp b/JsonGeneratorTests/PrettyPrint_Object_Tests.cpp index f181cabe..8986143b 100644 --- a/JsonGeneratorTests/PrettyPrint_Object_Tests.cpp +++ b/JsonGeneratorTests/PrettyPrint_Object_Tests.cpp @@ -74,7 +74,8 @@ namespace JsonGeneratorTests void whenInputIs(const char input[]) { StringBuilder sb(buffer, sizeof(buffer)); - PrettyPrintDecorator decorator(sb); + IndentedPrintDecorator indentedPrint(sb); + PrettyPrintDecorator decorator(indentedPrint); returnValue = decorator.print(input); } diff --git a/JsonGeneratorTests/PrettyPrint_String_Tests.cpp b/JsonGeneratorTests/PrettyPrint_String_Tests.cpp index 006a5b1c..6ce4a166 100644 --- a/JsonGeneratorTests/PrettyPrint_String_Tests.cpp +++ b/JsonGeneratorTests/PrettyPrint_String_Tests.cpp @@ -61,7 +61,8 @@ namespace JsonGeneratorTests void whenInputIs(const char input[]) { StringBuilder sb(buffer, sizeof(buffer)); - PrettyPrintDecorator decorator(sb); + IndentedPrintDecorator indentedPrint(sb); + PrettyPrintDecorator decorator(indentedPrint); returnValue = decorator.print(input); }