From c6e0688e5a67c17ef03a0d1ee1ee434af72ad766 Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Sun, 25 Jan 2015 17:32:11 -0600 Subject: [PATCH] implement CommentStyle::None/indentation_=="" --- src/lib_json/json_writer.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index eea17b6..43166ac 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -720,7 +720,9 @@ int BuiltStyledStreamWriter::write(Value const& root) indented_ = true; writeValue(root); writeCommentAfterValueOnSameLine(root); - sout_ << "\n"; + if (!indentation_.empty()) { + sout_ << "\n"; + } return 0; } void BuiltStyledStreamWriter::writeValue(Value const& value) { @@ -759,7 +761,9 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) { Value const& childValue = value[name]; writeCommentBeforeValue(childValue); writeWithIndent(valueToQuotedString(name.c_str())); - sout_ << " : "; + if (!indentation_.empty()) sout_ << " "; + sout_ << ":"; + if (!indentation_.empty()) sout_ << " "; writeValue(childValue); if (++it == members.end()) { writeCommentAfterValueOnSameLine(childValue); @@ -809,13 +813,15 @@ void BuiltStyledStreamWriter::writeArrayValue(Value const& value) { } else // output on a single line { assert(childValues_.size() == size); - sout_ << "[ "; + sout_ << "["; + if (!indentation_.empty()) sout_ << " "; for (unsigned index = 0; index < size; ++index) { if (index > 0) sout_ << ", "; sout_ << childValues_[index]; } - sout_ << " ]"; + if (!indentation_.empty()) sout_ << " "; + sout_ << "]"; } } } @@ -860,7 +866,11 @@ void BuiltStyledStreamWriter::writeIndent() { // to determine whether we are already indented, but // with a stream we cannot do that. So we rely on some saved state. // The caller checks indented_. - sout_ << '\n' << indentString_; + + if (!indentation_.empty()) { + // In this case, drop newlines too. + sout_ << '\n' << indentString_; + } } void BuiltStyledStreamWriter::writeWithIndent(std::string const& value) { @@ -877,6 +887,7 @@ void BuiltStyledStreamWriter::unindent() { } void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) { + if (cs_ == CommentStyle::None) return; if (!root.hasComment(commentBefore)) return; @@ -895,6 +906,7 @@ void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) { } void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(Value const& root) { + if (cs_ == CommentStyle::None) return; if (root.hasComment(commentAfterOnSameLine)) sout_ << " " + root.getComment(commentAfterOnSameLine);