@@ -673,7 +673,10 @@ struct BuiltStyledStreamWriter : public StreamWriter
673
673
BuiltStyledStreamWriter (
674
674
std::ostream* sout,
675
675
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);
677
680
virtual int write (Value const & root);
678
681
private:
679
682
void writeValue (Value const & value);
@@ -695,17 +698,26 @@ struct BuiltStyledStreamWriter : public StreamWriter
695
698
int rightMargin_;
696
699
std::string indentation_;
697
700
CommentStyle cs_;
701
+ std::string colonSymbol_;
702
+ std::string nullSymbol_;
703
+ std::string endingLineFeedSymbol_;
698
704
bool addChildValues_ : 1 ;
699
705
bool indented_ : 1 ;
700
706
};
701
707
BuiltStyledStreamWriter::BuiltStyledStreamWriter (
702
708
std::ostream* sout,
703
709
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)
705
714
: StreamWriter(sout)
706
715
, rightMargin_(74 )
707
716
, indentation_(indentation)
708
717
, cs_(cs)
718
+ , colonSymbol_(colonSymbol)
719
+ , nullSymbol_(nullSymbol)
720
+ , endingLineFeedSymbol_(endingLineFeedSymbol)
709
721
, addChildValues_(false )
710
722
, indented_(false )
711
723
{
@@ -720,15 +732,13 @@ int BuiltStyledStreamWriter::write(Value const& root)
720
732
indented_ = true ;
721
733
writeValue (root);
722
734
writeCommentAfterValueOnSameLine (root);
723
- if (!indentation_.empty ()) {
724
- sout_ << " \n " ;
725
- }
735
+ sout_ << endingLineFeedSymbol_;
726
736
return 0 ;
727
737
}
728
738
void BuiltStyledStreamWriter::writeValue (Value const & value) {
729
739
switch (value.type ()) {
730
740
case nullValue:
731
- pushValue (" null " );
741
+ pushValue (nullSymbol_ );
732
742
break ;
733
743
case intValue:
734
744
pushValue (valueToString (value.asLargestInt ()));
@@ -761,9 +771,7 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
761
771
Value const & childValue = value[name];
762
772
writeCommentBeforeValue (childValue);
763
773
writeWithIndent (valueToQuotedString (name.c_str ()));
764
- if (!indentation_.empty ()) sout_ << " " ;
765
- sout_ << " :" ;
766
- if (!indentation_.empty ()) sout_ << " " ;
774
+ sout_ << colonSymbol_;
767
775
writeValue (childValue);
768
776
if (++it == members.end ()) {
769
777
writeCommentAfterValueOnSameLine (childValue);
@@ -955,16 +963,25 @@ class StreamWriterBuilder {
955
963
typedef StreamWriter::CommentStyle CommentStyle;
956
964
CommentStyle cs_;
957
965
std::string indentation_;
966
+ bool dropNullPlaceholders_;
967
+ bool omitEndingLineFeed_;
968
+ bool enableYAMLCompatibility_;
958
969
public:
959
970
StreamWriterBuilder ();
960
971
virtual ~StreamWriterBuilder ();
961
972
virtual void setCommentStyle (CommentStyle cs);
962
973
virtual void setIndentation (std::string indentation);
974
+ virtual void setDropNullPlaceholders (bool v);
975
+ virtual void setOmitEndingLineFeed (bool v);
976
+ virtual void setEnableYAMLCompatibility (bool v);
963
977
virtual StreamWriter* newStreamWriter (std::ostream* sout) const ;
964
978
};
965
979
StreamWriterBuilder::StreamWriterBuilder ()
966
980
: cs_(CommentStyle::All)
967
981
, indentation_(" \t " )
982
+ , dropNullPlaceholders_(false )
983
+ , omitEndingLineFeed_(false )
984
+ , enableYAMLCompatibility_(false )
968
985
{
969
986
}
970
987
StreamWriterBuilder::~StreamWriterBuilder ()
@@ -979,9 +996,39 @@ void StreamWriterBuilder::setIndentation(std::string v)
979
996
indentation_ = v;
980
997
if (indentation_.empty ()) cs_ = CommentStyle::None;
981
998
}
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
+ }
982
1011
StreamWriter* StreamWriterBuilder::newStreamWriter (std::ostream* stream) const
983
1012
{
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);
985
1032
}
986
1033
987
1034
// This might become public someday.
@@ -1019,13 +1066,23 @@ void StreamWriter::Builder::setIndentation(std::string v)
1019
1066
{
1020
1067
own_->setIndentation (v);
1021
1068
}
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
+ }
1022
1081
StreamWriter* StreamWriter::Builder::newStreamWriter (std::ostream* sout) const
1023
1082
{
1024
1083
return own_->newStreamWriter (sout);
1025
1084
}
1026
1085
1027
- // / Do not take ownership of sout, but maintain a reference.
1028
- StreamWriter* newStreamWriter (std::ostream* sout);
1029
1086
std::string writeString (Value const & root, StreamWriter::Builder const & builder) {
1030
1087
std::ostringstream sout;
1031
1088
std::unique_ptr<StreamWriter> const sw (builder.newStreamWriter (&sout));
@@ -1036,6 +1093,7 @@ std::string writeString(Value const& root, StreamWriter::Builder const& builder)
1036
1093
std::ostream& operator <<(std::ostream& sout, Value const & root) {
1037
1094
StreamWriter::Builder builder;
1038
1095
builder.setCommentStyle (StreamWriter::CommentStyle::All);
1096
+ builder.setIndentation (" \t " );
1039
1097
std::shared_ptr<StreamWriter> writer (builder.newStreamWriter (&sout));
1040
1098
writer->write (root);
1041
1099
return sout;
0 commit comments