mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-10-22 08:20:47 +08:00
set output stream in write(), not in builder
This commit is contained in:
@@ -679,13 +679,12 @@ bool StyledStreamWriter::hasCommentForValue(const Value& value) {
|
||||
struct BuiltStyledStreamWriter : public StreamWriter
|
||||
{
|
||||
BuiltStyledStreamWriter(
|
||||
std::ostream* sout,
|
||||
std::string const& indentation,
|
||||
StreamWriter::CommentStyle::Enum cs,
|
||||
std::string const& colonSymbol,
|
||||
std::string const& nullSymbol,
|
||||
std::string const& endingLineFeedSymbol);
|
||||
virtual int write(Value const& root);
|
||||
virtual int write(Value const& root, std::ostream* sout);
|
||||
private:
|
||||
void writeValue(Value const& value);
|
||||
void writeArrayValue(Value const& value);
|
||||
@@ -713,14 +712,12 @@ private:
|
||||
bool indented_ : 1;
|
||||
};
|
||||
BuiltStyledStreamWriter::BuiltStyledStreamWriter(
|
||||
std::ostream* sout,
|
||||
std::string const& indentation,
|
||||
StreamWriter::CommentStyle::Enum cs,
|
||||
std::string const& colonSymbol,
|
||||
std::string const& nullSymbol,
|
||||
std::string const& endingLineFeedSymbol)
|
||||
: StreamWriter(sout)
|
||||
, rightMargin_(74)
|
||||
: rightMargin_(74)
|
||||
, indentation_(indentation)
|
||||
, cs_(cs)
|
||||
, colonSymbol_(colonSymbol)
|
||||
@@ -730,8 +727,9 @@ BuiltStyledStreamWriter::BuiltStyledStreamWriter(
|
||||
, indented_(false)
|
||||
{
|
||||
}
|
||||
int BuiltStyledStreamWriter::write(Value const& root)
|
||||
int BuiltStyledStreamWriter::write(Value const& root, std::ostream* sout)
|
||||
{
|
||||
sout_ = sout;
|
||||
addChildValues_ = false;
|
||||
indented_ = true;
|
||||
indentString_ = "";
|
||||
@@ -740,7 +738,8 @@ int BuiltStyledStreamWriter::write(Value const& root)
|
||||
indented_ = true;
|
||||
writeValue(root);
|
||||
writeCommentAfterValueOnSameLine(root);
|
||||
sout_ << endingLineFeedSymbol_;
|
||||
*sout_ << endingLineFeedSymbol_;
|
||||
sout_ = NULL;
|
||||
return 0;
|
||||
}
|
||||
void BuiltStyledStreamWriter::writeValue(Value const& value) {
|
||||
@@ -779,13 +778,13 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
|
||||
Value const& childValue = value[name];
|
||||
writeCommentBeforeValue(childValue);
|
||||
writeWithIndent(valueToQuotedString(name.c_str()));
|
||||
sout_ << colonSymbol_;
|
||||
*sout_ << colonSymbol_;
|
||||
writeValue(childValue);
|
||||
if (++it == members.end()) {
|
||||
writeCommentAfterValueOnSameLine(childValue);
|
||||
break;
|
||||
}
|
||||
sout_ << ",";
|
||||
*sout_ << ",";
|
||||
writeCommentAfterValueOnSameLine(childValue);
|
||||
}
|
||||
unindent();
|
||||
@@ -821,7 +820,7 @@ void BuiltStyledStreamWriter::writeArrayValue(Value const& value) {
|
||||
writeCommentAfterValueOnSameLine(childValue);
|
||||
break;
|
||||
}
|
||||
sout_ << ",";
|
||||
*sout_ << ",";
|
||||
writeCommentAfterValueOnSameLine(childValue);
|
||||
}
|
||||
unindent();
|
||||
@@ -829,15 +828,15 @@ void BuiltStyledStreamWriter::writeArrayValue(Value const& value) {
|
||||
} else // output on a single line
|
||||
{
|
||||
assert(childValues_.size() == size);
|
||||
sout_ << "[";
|
||||
if (!indentation_.empty()) sout_ << " ";
|
||||
*sout_ << "[";
|
||||
if (!indentation_.empty()) *sout_ << " ";
|
||||
for (unsigned index = 0; index < size; ++index) {
|
||||
if (index > 0)
|
||||
sout_ << ", ";
|
||||
sout_ << childValues_[index];
|
||||
*sout_ << ", ";
|
||||
*sout_ << childValues_[index];
|
||||
}
|
||||
if (!indentation_.empty()) sout_ << " ";
|
||||
sout_ << "]";
|
||||
if (!indentation_.empty()) *sout_ << " ";
|
||||
*sout_ << "]";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -874,7 +873,7 @@ void BuiltStyledStreamWriter::pushValue(std::string const& value) {
|
||||
if (addChildValues_)
|
||||
childValues_.push_back(value);
|
||||
else
|
||||
sout_ << value;
|
||||
*sout_ << value;
|
||||
}
|
||||
|
||||
void BuiltStyledStreamWriter::writeIndent() {
|
||||
@@ -885,13 +884,13 @@ void BuiltStyledStreamWriter::writeIndent() {
|
||||
|
||||
if (!indentation_.empty()) {
|
||||
// In this case, drop newlines too.
|
||||
sout_ << '\n' << indentString_;
|
||||
*sout_ << '\n' << indentString_;
|
||||
}
|
||||
}
|
||||
|
||||
void BuiltStyledStreamWriter::writeWithIndent(std::string const& value) {
|
||||
if (!indented_) writeIndent();
|
||||
sout_ << value;
|
||||
*sout_ << value;
|
||||
indented_ = false;
|
||||
}
|
||||
|
||||
@@ -911,11 +910,11 @@ void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
|
||||
const std::string& comment = root.getComment(commentBefore);
|
||||
std::string::const_iterator iter = comment.begin();
|
||||
while (iter != comment.end()) {
|
||||
sout_ << *iter;
|
||||
*sout_ << *iter;
|
||||
if (*iter == '\n' &&
|
||||
(iter != comment.end() && *(iter + 1) == '/'))
|
||||
// writeIndent(); // would write extra newline
|
||||
sout_ << indentString_;
|
||||
*sout_ << indentString_;
|
||||
++iter;
|
||||
}
|
||||
indented_ = false;
|
||||
@@ -924,11 +923,11 @@ void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
|
||||
void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(Value const& root) {
|
||||
if (cs_ == CommentStyle::None) return;
|
||||
if (root.hasComment(commentAfterOnSameLine))
|
||||
sout_ << " " + root.getComment(commentAfterOnSameLine);
|
||||
*sout_ << " " + root.getComment(commentAfterOnSameLine);
|
||||
|
||||
if (root.hasComment(commentAfter)) {
|
||||
writeIndent();
|
||||
sout_ << root.getComment(commentAfter);
|
||||
*sout_ << root.getComment(commentAfter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -942,8 +941,8 @@ bool BuiltStyledStreamWriter::hasCommentForValue(const Value& value) {
|
||||
///////////////
|
||||
// StreamWriter
|
||||
|
||||
StreamWriter::StreamWriter(std::ostream* sout)
|
||||
: sout_(*sout)
|
||||
StreamWriter::StreamWriter()
|
||||
: sout_(NULL)
|
||||
{
|
||||
}
|
||||
StreamWriter::~StreamWriter()
|
||||
@@ -957,7 +956,7 @@ StreamWriterBuilder::StreamWriterBuilder()
|
||||
}
|
||||
StreamWriterBuilder::~StreamWriterBuilder()
|
||||
{}
|
||||
StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const
|
||||
StreamWriter* StreamWriterBuilder::newStreamWriter() const
|
||||
{
|
||||
if (!validate(NULL)) throw std::runtime_error("invalid settings");
|
||||
// TODO: Maybe serialize the invalid settings into the exception.
|
||||
@@ -978,7 +977,7 @@ StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const
|
||||
}
|
||||
std::string nullSymbol = "null";
|
||||
std::string endingLineFeedSymbol = "";
|
||||
return new BuiltStyledStreamWriter(stream,
|
||||
return new BuiltStyledStreamWriter(
|
||||
indentation, cs,
|
||||
colonSymbol, nullSymbol, endingLineFeedSymbol);
|
||||
}
|
||||
@@ -1015,24 +1014,7 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings)
|
||||
//! [StreamWriterBuilderDefaults]
|
||||
}
|
||||
|
||||
/*
|
||||
// This might become public someday.
|
||||
class StreamWriterBuilderFactory {
|
||||
public:
|
||||
virtual ~StreamWriterBuilderFactory();
|
||||
virtual StreamWriterBuilder* newStreamWriterBuilder() const;
|
||||
};
|
||||
StreamWriterBuilderFactory::~StreamWriterBuilderFactory()
|
||||
{
|
||||
}
|
||||
StreamWriterBuilder* StreamWriterBuilderFactory::newStreamWriterBuilder() const
|
||||
{
|
||||
return new StreamWriterBuilder;
|
||||
}
|
||||
*/
|
||||
|
||||
StreamWriter* OldCompressingStreamWriterBuilder::newStreamWriter(
|
||||
std::ostream* stream) const
|
||||
StreamWriter* OldCompressingStreamWriterBuilder::newStreamWriter() const
|
||||
{
|
||||
std::string colonSymbol = " : ";
|
||||
if (enableYAMLCompatibility_) {
|
||||
@@ -1048,22 +1030,22 @@ StreamWriter* OldCompressingStreamWriterBuilder::newStreamWriter(
|
||||
if (omitEndingLineFeed_) {
|
||||
endingLineFeedSymbol = "";
|
||||
}
|
||||
return new BuiltStyledStreamWriter(stream,
|
||||
return new BuiltStyledStreamWriter(
|
||||
"", StreamWriter::CommentStyle::None,
|
||||
colonSymbol, nullSymbol, endingLineFeedSymbol);
|
||||
}
|
||||
|
||||
std::string writeString(StreamWriter::Factory const& builder, Value const& root) {
|
||||
std::ostringstream sout;
|
||||
StreamWriterPtr const writer(builder.newStreamWriter(&sout));
|
||||
writer->write(root);
|
||||
StreamWriterPtr const writer(builder.newStreamWriter());
|
||||
writer->write(root, &sout);
|
||||
return sout.str();
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& sout, Value const& root) {
|
||||
StreamWriterBuilder builder;
|
||||
StreamWriterPtr const writer(builder.newStreamWriter(&sout));
|
||||
writer->write(root);
|
||||
StreamWriterPtr const writer(builder.newStreamWriter());
|
||||
writer->write(root, &sout);
|
||||
return sout;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user