Move old FastWriter stuff out of new Builder

This commit is contained in:
Christopher Dunn 2015-01-26 10:43:39 -06:00
parent 177b7b8f22
commit 28a20917b0
3 changed files with 6 additions and 67 deletions

View File

@ -94,6 +94,9 @@ writer->write( root );
// If you like the defaults, you can insert directly into a stream. // If you like the defaults, you can insert directly into a stream.
std::cout << root; std::cout << root;
// If desired, remember to add a linefeed and flush.
std::cout << std::endl;
// Of course, you can write to `std::ostringstream` if you prefer. Or // Of course, you can write to `std::ostringstream` if you prefer. Or
// use `writeString()` for convenience. // use `writeString()` for convenience.
std::string document = Json::writeString( root, builder ); std::string document = Json::writeString( root, builder );

View File

@ -36,7 +36,7 @@ Usage:
std::shared_ptr<StreamWriter> writer( std::shared_ptr<StreamWriter> writer(
builder.newStreamWriter(&std::cout)); builder.newStreamWriter(&std::cout));
writer->write(value); writer->write(value);
std::cout.flush(); std::cout << std::endl; // add lf and flush
\endcode \endcode
*/ */
class JSON_API StreamWriter { class JSON_API StreamWriter {
@ -77,24 +77,6 @@ public:
Default: "\t" Default: "\t"
*/ */
Builder& withIndentation(std::string indentation); Builder& withIndentation(std::string indentation);
/** \brief Drop the "null" string from the writer's output for nullValues.
* Strictly speaking, this is not valid JSON. But when the output is being
* fed to a browser's Javascript, it makes for smaller output and the
* browser can handle the output just fine.
*/
Builder& withDropNullPlaceholders(bool v);
/** \brief Do not add \n at end of document.
* Normally, we add an extra newline, just because.
*/
Builder& withOmitEndingLineFeed(bool v);
/** \brief Add a space after ':'.
* If indentation is non-empty, we surround colon with whitespace,
* e.g. " : "
* This will add back the trailing space when there is no indentation.
* This seems dubious when the entire document is on a single line,
* but we leave this here to repduce the behavior of the old `FastWriter`.
*/
Builder& withEnableYAMLCompatibility(bool v);
/// Do not take ownership of sout, but maintain a reference. /// Do not take ownership of sout, but maintain a reference.
StreamWriter* newStreamWriter(std::ostream* sout) const; StreamWriter* newStreamWriter(std::ostream* sout) const;

View File

@ -963,25 +963,16 @@ class StreamWriterBuilder {
typedef StreamWriter::CommentStyle CommentStyle; typedef StreamWriter::CommentStyle CommentStyle;
CommentStyle cs_; CommentStyle cs_;
std::string indentation_; std::string indentation_;
bool dropNullPlaceholders_;
bool omitEndingLineFeed_;
bool enableYAMLCompatibility_;
public: public:
StreamWriterBuilder(); StreamWriterBuilder();
virtual ~StreamWriterBuilder(); virtual ~StreamWriterBuilder();
virtual void setCommentStyle(CommentStyle cs); virtual void setCommentStyle(CommentStyle cs);
virtual void setIndentation(std::string indentation); virtual void setIndentation(std::string indentation);
virtual void setDropNullPlaceholders(bool v);
virtual void setOmitEndingLineFeed(bool v);
virtual void setEnableYAMLCompatibility(bool v);
virtual StreamWriter* newStreamWriter(std::ostream* sout) const; virtual StreamWriter* newStreamWriter(std::ostream* sout) const;
}; };
StreamWriterBuilder::StreamWriterBuilder() StreamWriterBuilder::StreamWriterBuilder()
: cs_(CommentStyle::All) : cs_(CommentStyle::All)
, indentation_("\t") , indentation_("\t")
, dropNullPlaceholders_(false)
, omitEndingLineFeed_(false)
, enableYAMLCompatibility_(false)
{ {
} }
StreamWriterBuilder::~StreamWriterBuilder() StreamWriterBuilder::~StreamWriterBuilder()
@ -996,36 +987,14 @@ void StreamWriterBuilder::setIndentation(std::string v)
indentation_ = v; indentation_ = v;
if (indentation_.empty()) cs_ = CommentStyle::None; if (indentation_.empty()) cs_ = CommentStyle::None;
} }
void StreamWriterBuilder::setDropNullPlaceholders(bool v)
{
dropNullPlaceholders_ = v;
}
void StreamWriterBuilder::setOmitEndingLineFeed(bool v)
{
omitEndingLineFeed_ = v;
}
void StreamWriterBuilder::setEnableYAMLCompatibility(bool v)
{
enableYAMLCompatibility_ = v;
}
StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const
{ {
std::string colonSymbol = " : "; std::string colonSymbol = " : ";
if (indentation_.empty()) { if (indentation_.empty()) {
if (enableYAMLCompatibility_) { colonSymbol = ":";
colonSymbol = ": ";
} else {
colonSymbol = ":";
}
} }
std::string nullSymbol = "null"; std::string nullSymbol = "null";
if (dropNullPlaceholders_) { std::string endingLineFeedSymbol = "";
nullSymbol = "";
}
std::string endingLineFeedSymbol = "\n";
if (omitEndingLineFeed_) {
endingLineFeedSymbol = "";
}
return new BuiltStyledStreamWriter(stream, return new BuiltStyledStreamWriter(stream,
indentation_, cs_, indentation_, cs_,
colonSymbol, nullSymbol, endingLineFeedSymbol); colonSymbol, nullSymbol, endingLineFeedSymbol);
@ -1068,21 +1037,6 @@ StreamWriter::Builder& StreamWriter::Builder::withIndentation(std::string v)
own_->setIndentation(v); own_->setIndentation(v);
return *this; return *this;
} }
StreamWriter::Builder& StreamWriter::Builder::withDropNullPlaceholders(bool v)
{
own_->setDropNullPlaceholders(v);
return *this;
}
StreamWriter::Builder& StreamWriter::Builder::withOmitEndingLineFeed(bool v)
{
own_->setOmitEndingLineFeed(v);
return *this;
}
StreamWriter::Builder& StreamWriter::Builder::withEnableYAMLCompatibility(bool v)
{
own_->setEnableYAMLCompatibility(v);
return *this;
}
StreamWriter* StreamWriter::Builder::newStreamWriter( StreamWriter* StreamWriter::Builder::newStreamWriter(
std::ostream* sout) const std::ostream* sout) const
{ {