const stuff

This commit is contained in:
Christopher Dunn 2015-01-23 08:38:32 -06:00
parent beb6f35c63
commit 9243d602fe
3 changed files with 17 additions and 8 deletions

View File

@ -76,12 +76,12 @@ public:
void setIndentation(std::string indentation); void setIndentation(std::string indentation);
/// 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); StreamWriter* newStreamWriter(std::ostream* sout) const;
}; };
}; };
/// \brief Write into stringstream, then return string, for convenience. /// \brief Write into stringstream, then return string, for convenience.
std::string writeString(Value const& root, StreamWriterBuilder const& builder); std::string writeString(Value const& root, StreamWriter::Builder const& builder);
/** \brief Abstract class for writers. /** \brief Abstract class for writers.

View File

@ -151,7 +151,6 @@ static int parseAndSaveValueTree(const std::string& input,
reader.getFormattedErrorMessages().c_str()); reader.getFormattedErrorMessages().c_str());
return 1; return 1;
} }
if (!parseOnly) { if (!parseOnly) {
FILE* factual = fopen(actual.c_str(), "wt"); FILE* factual = fopen(actual.c_str(), "wt");
if (!factual) { if (!factual) {
@ -182,6 +181,14 @@ static std::string useStyledStreamWriter(
writer.write(sout, root); writer.write(sout, root);
return sout.str(); return sout.str();
} }
static std::string useBuiltStyledStreamWriter(
Json::Value const& root)
{
Json::StreamWriterBuilderFactory f;
Json::StreamWriter::Builder builder(&f);
builder.setCommentStyle(Json::StreamWriter::CommentStyle::All);
return writeString(root, builder);
}
static int rewriteValueTree( static int rewriteValueTree(
const std::string& rewritePath, const std::string& rewritePath,
const Json::Value& root, const Json::Value& root,
@ -248,6 +255,8 @@ static int parseCommandLine(
opts->write = &useStyledWriter; opts->write = &useStyledWriter;
} else if (writerName == "StyledStreamWriter") { } else if (writerName == "StyledStreamWriter") {
opts->write = &useStyledStreamWriter; opts->write = &useStyledStreamWriter;
} else if (writerName == "BuiltStyledStreamWriter") {
opts->write = &useBuiltStyledStreamWriter;
} else { } else {
printf("Unknown '--json-writer %s'\n", writerName.c_str()); printf("Unknown '--json-writer %s'\n", writerName.c_str());
return 4; return 4;

View File

@ -772,8 +772,8 @@ void BuiltStyledStreamWriter::writeArrayValue(Value const& value) {
if (size == 0) if (size == 0)
pushValue("[]"); pushValue("[]");
else { else {
bool isArrayMultiLine = isMultineArray(value); bool isMultiLine = (cs_ == CommentStyle::All) || isMultineArray(value);
if (isArrayMultiLine) { if (isMultiLine) {
writeWithIndent("["); writeWithIndent("[");
indent(); indent();
bool hasChildValue = !childValues_.empty(); bool hasChildValue = !childValues_.empty();
@ -969,14 +969,14 @@ void StreamWriter::Builder::setIndentation(std::string v)
{ {
own_->setIndentation(v); own_->setIndentation(v);
} }
StreamWriter* StreamWriter::Builder::newStreamWriter(std::ostream* sout) StreamWriter* StreamWriter::Builder::newStreamWriter(std::ostream* sout) const
{ {
return own_->newStreamWriter(sout); return own_->newStreamWriter(sout);
} }
/// 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); StreamWriter* newStreamWriter(std::ostream* sout);
std::string writeString(Value const& root, StreamWriterBuilder const& builder) { std::string writeString(Value const& root, StreamWriter::Builder const& builder) {
std::ostringstream sout; std::ostringstream sout;
std::unique_ptr<StreamWriter> const sw(builder.newStreamWriter(&sout)); std::unique_ptr<StreamWriter> const sw(builder.newStreamWriter(&sout));
sw->write(root); sw->write(root);
@ -986,7 +986,7 @@ std::string writeString(Value const& root, StreamWriterBuilder const& builder) {
std::ostream& operator<<(std::ostream& sout, Value const& root) { std::ostream& operator<<(std::ostream& sout, Value const& root) {
StreamWriterBuilderFactory f; StreamWriterBuilderFactory f;
StreamWriter::Builder builder(&f); StreamWriter::Builder builder(&f);
builder.setCommentStyle(StreamWriter::CommentStyle::Some); builder.setCommentStyle(StreamWriter::CommentStyle::All);
std::shared_ptr<StreamWriter> writer(builder.newStreamWriter(&sout)); std::shared_ptr<StreamWriter> writer(builder.newStreamWriter(&sout));
writer->write(root); writer->write(root);
return sout; return sout;