Skip to content

Commit dea6f8d

Browse files
committed
incorporate 'proper newlines for comments' into new StreamWriter
1 parent 648843d commit dea6f8d

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/lib_json/json_writer.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -713,9 +713,11 @@ BuiltStyledStreamWriter::BuiltStyledStreamWriter(
713713
int BuiltStyledStreamWriter::write(Value const& root)
714714
{
715715
addChildValues_ = false;
716-
indented_ = false;
716+
indented_ = true;
717717
indentString_ = "";
718718
writeCommentBeforeValue(root);
719+
if (!indented_) writeIndent();
720+
indented_ = true;
719721
writeValue(root);
720722
writeCommentAfterValueOnSameLine(root);
721723
sout_ << "\n";
@@ -878,20 +880,17 @@ void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
878880
if (!root.hasComment(commentBefore))
879881
return;
880882

881-
sout_ << "\n";
882-
writeIndent();
883+
if (!indented_) writeIndent();
883884
const std::string& comment = root.getComment(commentBefore);
884885
std::string::const_iterator iter = comment.begin();
885886
while (iter != comment.end()) {
886887
sout_ << *iter;
887888
if (*iter == '\n' &&
888889
(iter != comment.end() && *(iter + 1) == '/'))
889-
writeIndent();
890+
// writeIndent(); // would write extra newline
891+
sout_ << indentString_;
890892
++iter;
891893
}
892-
893-
// Comments are stripped of trailing newlines, so add one here
894-
sout_ << "\n";
895894
indented_ = false;
896895
}
897896

@@ -900,9 +899,8 @@ void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(Value const& root
900899
sout_ << " " + root.getComment(commentAfterOnSameLine);
901900

902901
if (root.hasComment(commentAfter)) {
903-
sout_ << "\n";
902+
writeIndent();
904903
sout_ << root.getComment(commentAfter);
905-
sout_ << "\n";
906904
}
907905
}
908906

0 commit comments

Comments
 (0)