Skip to content

Commit d78caa3

Browse files
committed
implement strange setting from FastWriter
1 parent c6e0688 commit d78caa3

File tree

2 files changed

+88
-12
lines changed

2 files changed

+88
-12
lines changed

include/json/writer.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ class JSON_API StreamWriter {
7474
Default: "\t"
7575
*/
7676
void setIndentation(std::string indentation);
77+
/** \brief Drop the "null" string from the writer's output for nullValues.
78+
* Strictly speaking, this is not valid JSON. But when the output is being
79+
* fed to a browser's Javascript, it makes for smaller output and the
80+
* browser can handle the output just fine.
81+
*/
82+
void setDropNullPlaceholders(bool v);
83+
/** \brief Do not add \n at end of document.
84+
* Normally, we add an extra newline, just because.
85+
*/
86+
void setOmitEndingLineFeed(bool v);
87+
/** \brief Add a space after ':'.
88+
* If indentation is non-empty, we surround colon with whitespace,
89+
* e.g. " : "
90+
* This will add back the trailing space when there is no indentation.
91+
* This seems dubious when the entire document is on a single line,
92+
* but we leave this here to repduce the behavior of the old `FastWriter`.
93+
*/
94+
void setEnableYAMLCompatibility(bool v);
7795

7896
/// Do not take ownership of sout, but maintain a reference.
7997
StreamWriter* newStreamWriter(std::ostream* sout) const;

src/lib_json/json_writer.cpp

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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);
678681
private:
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
};
701707
BuiltStyledStreamWriter::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
}
728738
void 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_;
958969
public:
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
};
965979
StreamWriterBuilder::StreamWriterBuilder()
966980
: cs_(CommentStyle::All)
967981
, indentation_("\t")
982+
, dropNullPlaceholders_(false)
983+
, omitEndingLineFeed_(false)
984+
, enableYAMLCompatibility_(false)
968985
{
969986
}
970987
StreamWriterBuilder::~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+
}
9821011
StreamWriter* 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+
}
10221081
StreamWriter* 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);
10291086
std::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)
10361093
std::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

Comments
 (0)