diff --git a/include/json/writer.h b/include/json/writer.h index e4a761c..b207849 100644 --- a/include/json/writer.h +++ b/include/json/writer.h @@ -76,12 +76,12 @@ public: void setIndentation(std::string indentation); /// Do not take ownership of sout, but maintain a reference. - StreamWriter* newStreamWriter(std::ostream* sout); + StreamWriter* newStreamWriter(std::ostream* sout) const; }; }; /// \brief Write into stringstream, then return string, for convenience. -std::string writeString(Value const& root, StreamWriterBuilder const& builder); +std::string writeString(Value const& root, StreamWriter::Builder const& builder); /** \brief Abstract class for writers. diff --git a/src/jsontestrunner/main.cpp b/src/jsontestrunner/main.cpp index f6f12b8..61411b4 100644 --- a/src/jsontestrunner/main.cpp +++ b/src/jsontestrunner/main.cpp @@ -151,7 +151,6 @@ static int parseAndSaveValueTree(const std::string& input, reader.getFormattedErrorMessages().c_str()); return 1; } - if (!parseOnly) { FILE* factual = fopen(actual.c_str(), "wt"); if (!factual) { @@ -182,6 +181,14 @@ static std::string useStyledStreamWriter( writer.write(sout, root); return sout.str(); } +static std::string useBuiltStyledStreamWriter( + Json::Value const& root) +{ + Json::StreamWriterBuilderFactory f; + Json::StreamWriter::Builder builder(&f); + builder.setCommentStyle(Json::StreamWriter::CommentStyle::All); + return writeString(root, builder); +} static int rewriteValueTree( const std::string& rewritePath, const Json::Value& root, @@ -248,6 +255,8 @@ static int parseCommandLine( opts->write = &useStyledWriter; } else if (writerName == "StyledStreamWriter") { opts->write = &useStyledStreamWriter; + } else if (writerName == "BuiltStyledStreamWriter") { + opts->write = &useBuiltStyledStreamWriter; } else { printf("Unknown '--json-writer %s'\n", writerName.c_str()); return 4; diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index 0518999..27ff6e3 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -772,8 +772,8 @@ void BuiltStyledStreamWriter::writeArrayValue(Value const& value) { if (size == 0) pushValue("[]"); else { - bool isArrayMultiLine = isMultineArray(value); - if (isArrayMultiLine) { + bool isMultiLine = (cs_ == CommentStyle::All) || isMultineArray(value); + if (isMultiLine) { writeWithIndent("["); indent(); bool hasChildValue = !childValues_.empty(); @@ -969,14 +969,14 @@ void StreamWriter::Builder::setIndentation(std::string v) { own_->setIndentation(v); } -StreamWriter* StreamWriter::Builder::newStreamWriter(std::ostream* sout) +StreamWriter* StreamWriter::Builder::newStreamWriter(std::ostream* sout) const { return own_->newStreamWriter(sout); } /// Do not take ownership of sout, but maintain a reference. StreamWriter* newStreamWriter(std::ostream* sout); -std::string writeString(Value const& root, StreamWriterBuilder const& builder) { +std::string writeString(Value const& root, StreamWriter::Builder const& builder) { std::ostringstream sout; std::unique_ptr const sw(builder.newStreamWriter(&sout)); sw->write(root); @@ -986,7 +986,7 @@ std::string writeString(Value const& root, StreamWriterBuilder const& builder) { std::ostream& operator<<(std::ostream& sout, Value const& root) { StreamWriterBuilderFactory f; StreamWriter::Builder builder(&f); - builder.setCommentStyle(StreamWriter::CommentStyle::Some); + builder.setCommentStyle(StreamWriter::CommentStyle::All); std::shared_ptr writer(builder.newStreamWriter(&sout)); writer->write(root); return sout;