@@ -665,12 +665,29 @@ bool StyledStreamWriter::hasCommentForValue(const Value& value) {
665
665
value.hasComment (commentAfter);
666
666
}
667
667
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_;
673
671
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
+ }
674
691
StreamWriter::StreamWriter (std::ostream* sout)
675
692
: sout_(*sout)
676
693
{
@@ -720,8 +737,7 @@ void StreamWriterBuilder::setIndentation(std::string v)
720
737
}
721
738
StreamWriter* StreamWriterBuilder::newStreamWriter (std::ostream* stream) const
722
739
{
723
- // return new StyledStreamWriter(stream);
724
- return nullptr ;
740
+ return new BuiltStyledStreamWriter (stream, indentation_, cs_);
725
741
}
726
742
StreamWriterBuilderFactory::~StreamWriterBuilderFactory ()
727
743
{
@@ -761,4 +777,13 @@ std::string writeString(Value const& root, StreamWriterBuilder const& builder) {
761
777
return sout.str ();
762
778
}
763
779
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
+
764
789
} // namespace Json
0 commit comments