@@ -673,7 +673,10 @@ struct BuiltStyledStreamWriter : public StreamWriter
673673 BuiltStyledStreamWriter (
674674 std::ostream* sout,
675675 std::string const & indentation,
676- StreamWriter::CommentStyle cs);
676+ StreamWriter::CommentStyle cs,
677+ std::string const & colonSymbol,
678+ std::string const & nullSymbol,
679+ std::string const & endingLineFeedSymbol);
677680 virtual int write (Value const & root);
678681private:
679682 void writeValue (Value const & value);
@@ -695,17 +698,26 @@ struct BuiltStyledStreamWriter : public StreamWriter
695698 int rightMargin_;
696699 std::string indentation_;
697700 CommentStyle cs_;
701+ std::string colonSymbol_;
702+ std::string nullSymbol_;
703+ std::string endingLineFeedSymbol_;
698704 bool addChildValues_ : 1 ;
699705 bool indented_ : 1 ;
700706};
701707BuiltStyledStreamWriter::BuiltStyledStreamWriter (
702708 std::ostream* sout,
703709 std::string const & indentation,
704- StreamWriter::CommentStyle cs)
710+ StreamWriter::CommentStyle cs,
711+ std::string const & colonSymbol,
712+ std::string const & nullSymbol,
713+ std::string const & endingLineFeedSymbol)
705714 : StreamWriter(sout)
706715 , rightMargin_(74 )
707716 , indentation_(indentation)
708717 , cs_(cs)
718+ , colonSymbol_(colonSymbol)
719+ , nullSymbol_(nullSymbol)
720+ , endingLineFeedSymbol_(endingLineFeedSymbol)
709721 , addChildValues_(false )
710722 , indented_(false )
711723{
@@ -720,15 +732,13 @@ int BuiltStyledStreamWriter::write(Value const& root)
720732 indented_ = true ;
721733 writeValue (root);
722734 writeCommentAfterValueOnSameLine (root);
723- if (!indentation_.empty ()) {
724- sout_ << " \n " ;
725- }
735+ sout_ << endingLineFeedSymbol_;
726736 return 0 ;
727737}
728738void BuiltStyledStreamWriter::writeValue (Value const & value) {
729739 switch (value.type ()) {
730740 case nullValue:
731- pushValue (" null " );
741+ pushValue (nullSymbol_ );
732742 break ;
733743 case intValue:
734744 pushValue (valueToString (value.asLargestInt ()));
@@ -761,9 +771,7 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
761771 Value const & childValue = value[name];
762772 writeCommentBeforeValue (childValue);
763773 writeWithIndent (valueToQuotedString (name.c_str ()));
764- if (!indentation_.empty ()) sout_ << " " ;
765- sout_ << " :" ;
766- if (!indentation_.empty ()) sout_ << " " ;
774+ sout_ << colonSymbol_;
767775 writeValue (childValue);
768776 if (++it == members.end ()) {
769777 writeCommentAfterValueOnSameLine (childValue);
@@ -955,16 +963,25 @@ class StreamWriterBuilder {
955963 typedef StreamWriter::CommentStyle CommentStyle;
956964 CommentStyle cs_;
957965 std::string indentation_;
966+ bool dropNullPlaceholders_;
967+ bool omitEndingLineFeed_;
968+ bool enableYAMLCompatibility_;
958969public:
959970 StreamWriterBuilder ();
960971 virtual ~StreamWriterBuilder ();
961972 virtual void setCommentStyle (CommentStyle cs);
962973 virtual void setIndentation (std::string indentation);
974+ virtual void setDropNullPlaceholders (bool v);
975+ virtual void setOmitEndingLineFeed (bool v);
976+ virtual void setEnableYAMLCompatibility (bool v);
963977 virtual StreamWriter* newStreamWriter (std::ostream* sout) const ;
964978};
965979StreamWriterBuilder::StreamWriterBuilder ()
966980 : cs_(CommentStyle::All)
967981 , indentation_(" \t " )
982+ , dropNullPlaceholders_(false )
983+ , omitEndingLineFeed_(false )
984+ , enableYAMLCompatibility_(false )
968985{
969986}
970987StreamWriterBuilder::~StreamWriterBuilder ()
@@ -979,9 +996,39 @@ void StreamWriterBuilder::setIndentation(std::string v)
979996 indentation_ = v;
980997 if (indentation_.empty ()) cs_ = CommentStyle::None;
981998}
999+ void StreamWriterBuilder::setDropNullPlaceholders (bool v)
1000+ {
1001+ dropNullPlaceholders_ = v;
1002+ }
1003+ void StreamWriterBuilder::setOmitEndingLineFeed (bool v)
1004+ {
1005+ omitEndingLineFeed_ = v;
1006+ }
1007+ void StreamWriterBuilder::setEnableYAMLCompatibility (bool v)
1008+ {
1009+ enableYAMLCompatibility_ = v;
1010+ }
9821011StreamWriter* StreamWriterBuilder::newStreamWriter (std::ostream* stream) const
9831012{
984- return new BuiltStyledStreamWriter (stream, indentation_, cs_);
1013+ std::string colonSymbol = " : " ;
1014+ if (indentation_.empty ()) {
1015+ if (enableYAMLCompatibility_) {
1016+ colonSymbol = " : " ;
1017+ } else {
1018+ colonSymbol = " :" ;
1019+ }
1020+ }
1021+ std::string nullSymbol = " null" ;
1022+ if (dropNullPlaceholders_) {
1023+ nullSymbol = " " ;
1024+ }
1025+ std::string endingLineFeedSymbol = " \n " ;
1026+ if (omitEndingLineFeed_) {
1027+ endingLineFeedSymbol = " " ;
1028+ }
1029+ return new BuiltStyledStreamWriter (stream,
1030+ indentation_, cs_,
1031+ colonSymbol, nullSymbol, endingLineFeedSymbol);
9851032}
9861033
9871034// This might become public someday.
@@ -1019,13 +1066,23 @@ void StreamWriter::Builder::setIndentation(std::string v)
10191066{
10201067 own_->setIndentation (v);
10211068}
1069+ void StreamWriter::Builder::setDropNullPlaceholders (bool v)
1070+ {
1071+ own_->setDropNullPlaceholders (v);
1072+ }
1073+ void StreamWriter::Builder::setOmitEndingLineFeed (bool v)
1074+ {
1075+ own_->setOmitEndingLineFeed (v);
1076+ }
1077+ void StreamWriter::Builder::setEnableYAMLCompatibility (bool v)
1078+ {
1079+ own_->setEnableYAMLCompatibility (v);
1080+ }
10221081StreamWriter* StreamWriter::Builder::newStreamWriter (std::ostream* sout) const
10231082{
10241083 return own_->newStreamWriter (sout);
10251084}
10261085
1027- // / Do not take ownership of sout, but maintain a reference.
1028- StreamWriter* newStreamWriter (std::ostream* sout);
10291086std::string writeString (Value const & root, StreamWriter::Builder const & builder) {
10301087 std::ostringstream sout;
10311088 std::unique_ptr<StreamWriter> const sw (builder.newStreamWriter (&sout));
@@ -1036,6 +1093,7 @@ std::string writeString(Value const& root, StreamWriter::Builder const& builder)
10361093std::ostream& operator <<(std::ostream& sout, Value const & root) {
10371094 StreamWriter::Builder builder;
10381095 builder.setCommentStyle (StreamWriter::CommentStyle::All);
1096+ builder.setIndentation (" \t " );
10391097 std::shared_ptr<StreamWriter> writer (builder.newStreamWriter (&sout));
10401098 writer->write (root);
10411099 return sout;
0 commit comments