diff --git a/JsonGenerator/IndentedPrintDecorator.cpp b/JsonGenerator/IndentedPrintDecorator.cpp index 55265f39..56fed033 100644 --- a/JsonGenerator/IndentedPrintDecorator.cpp +++ b/JsonGenerator/IndentedPrintDecorator.cpp @@ -11,33 +11,50 @@ size_t IndentedPrintDecorator::write(uint8_t c) { case '{': indent++; - emptyBlock = true; + previousChar = c; return sink.write(c); case '}': indent--; - if (emptyBlock) + if (previousChar == '{') + { + previousChar = c; + return sink.write(c); + } + else + { + previousChar = c; + return writeln() + sink.write(c); + } + + case ',': + previousChar = c; + if (isInAString) { return sink.write(c); } else { - return writeln() + sink.write(c); + return sink.write(c) + writeln(); } - case ',': - return sink.write(c) + writeln(); + case '\"': + if (previousChar != '\\') + { + isInAString = !isInAString; + } default: - if (emptyBlock) + if (previousChar == '{') { - emptyBlock = false; + previousChar = c; return writeln() + sink.write(c); } else { + previousChar = c; return sink.write(c); } } diff --git a/JsonGenerator/IndentedPrintDecorator.h b/JsonGenerator/IndentedPrintDecorator.h index c2ba4cc6..ee03faf0 100644 --- a/JsonGenerator/IndentedPrintDecorator.h +++ b/JsonGenerator/IndentedPrintDecorator.h @@ -13,16 +13,19 @@ class IndentedPrintDecorator : public Print public: IndentedPrintDecorator(Print& p) - : indent(0), sink(p), emptyBlock(false) + : indent(0), sink(p) { + previousChar = 0; + isInAString = false; } virtual size_t write(uint8_t); private: int indent; - bool emptyBlock; + uint8_t previousChar; Print& sink; + bool isInAString; size_t writeln(); }; diff --git a/JsonGeneratorTests/IntentedPrintTests.cpp b/JsonGeneratorTests/IntentedPrintTests.cpp index ec0b342e..78b94986 100644 --- a/JsonGeneratorTests/IntentedPrintTests.cpp +++ b/JsonGeneratorTests/IntentedPrintTests.cpp @@ -56,6 +56,15 @@ namespace JsonGeneratorTests "}"); } + TEST_METHOD(ObjectTrickyCharacters) + { + whenInputIs("{\"key\":\":\\\"',\"}"); + outputMustBe( + "{\n" + " \"key\":\":\\\"',\"\n" + "}"); + } + private: void whenInputIs(const char input[])