Skip to content

Commit d49ab5a

Browse files
committed
use new BuiltStyledStreamWriter in operator<<()
1 parent 4d64940 commit d49ab5a

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/lib_json/json_writer.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,29 @@ bool StyledStreamWriter::hasCommentForValue(const Value& value) {
665665
value.hasComment(commentAfter);
666666
}
667667

668-
std::ostream& operator<<(std::ostream& sout, const Value& root) {
669-
Json::StyledStreamWriter writer;
670-
writer.write(sout, root);
671-
return sout;
672-
}
668+
struct BuiltStyledStreamWriter : public StreamWriter
669+
{
670+
mutable StyledStreamWriter old_;
673671

672+
BuiltStyledStreamWriter(
673+
std::ostream* sout,
674+
std::string const& indentation,
675+
StreamWriter::CommentStyle cs);
676+
virtual int write(Value const& root) const;
677+
};
678+
BuiltStyledStreamWriter::BuiltStyledStreamWriter(
679+
std::ostream* sout,
680+
std::string const& indentation,
681+
StreamWriter::CommentStyle cs)
682+
: StreamWriter(sout)
683+
, old_(indentation)
684+
{
685+
}
686+
int BuiltStyledStreamWriter::write(Value const& root) const
687+
{
688+
old_.write(sout_, root);
689+
return 0;
690+
}
674691
StreamWriter::StreamWriter(std::ostream* sout)
675692
: sout_(*sout)
676693
{
@@ -720,8 +737,7 @@ void StreamWriterBuilder::setIndentation(std::string v)
720737
}
721738
StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const
722739
{
723-
// return new StyledStreamWriter(stream);
724-
return nullptr;
740+
return new BuiltStyledStreamWriter(stream, indentation_, cs_);
725741
}
726742
StreamWriterBuilderFactory::~StreamWriterBuilderFactory()
727743
{
@@ -761,4 +777,13 @@ std::string writeString(Value const& root, StreamWriterBuilder const& builder) {
761777
return sout.str();
762778
}
763779

780+
std::ostream& operator<<(std::ostream& sout, const Value& root) {
781+
StreamWriterBuilderFactory f;
782+
StreamWriter::Builder builder(&f);
783+
builder.setCommentStyle(StreamWriter::CommentStyle::Some);
784+
std::shared_ptr<StreamWriter> writer(builder.newStreamWriter(&sout));
785+
writer->write(root);
786+
return sout;
787+
}
788+
764789
} // namespace Json

0 commit comments

Comments
 (0)