From eb1a7747780312f5849dc2f8545bcc0e1467089a Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Mon, 25 Aug 2014 09:56:51 +0200 Subject: [PATCH] Fixed empty object output --- JsonGenerator/IndentedPrintDecorator.cpp | 26 ++++++++++++++++++++---- JsonGenerator/IndentedPrintDecorator.h | 3 ++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/JsonGenerator/IndentedPrintDecorator.cpp b/JsonGenerator/IndentedPrintDecorator.cpp index ca15c543..6c386367 100644 --- a/JsonGenerator/IndentedPrintDecorator.cpp +++ b/JsonGenerator/IndentedPrintDecorator.cpp @@ -10,15 +10,33 @@ size_t IndentedPrintDecorator::write(uint8_t c) switch (c) { case '{': - indent++; - return sink.write(c) + writeln(); + indent++; + emptyBlock = true; + return sink.write(c); case '}': indent--; - return writeln() + sink.write(c); + + if (emptyBlock) + { + return sink.write(c); + } + else + { + return writeln() + sink.write(c); + } default: - return sink.write(c); + + if (emptyBlock) + { + emptyBlock = false; + return writeln() + sink.write(c); + } + else + { + return sink.write(c); + } } } diff --git a/JsonGenerator/IndentedPrintDecorator.h b/JsonGenerator/IndentedPrintDecorator.h index e876b44a..c2ba4cc6 100644 --- a/JsonGenerator/IndentedPrintDecorator.h +++ b/JsonGenerator/IndentedPrintDecorator.h @@ -13,7 +13,7 @@ class IndentedPrintDecorator : public Print public: IndentedPrintDecorator(Print& p) - : indent(0), sink(p) + : indent(0), sink(p), emptyBlock(false) { } @@ -21,6 +21,7 @@ public: private: int indent; + bool emptyBlock; Print& sink; size_t writeln();