mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2024-12-26 18:51:04 +08:00
avoid extra newlines in StyledStreamWriter
Add indented_ as a bitfield. (Verified that sizeof(StyledStreamWriter) remains 96 for binary compatibility. But the new symbol requires a minor version-bump.)
This commit is contained in:
parent
f8ca6cbb25
commit
d383056fbb
@ -187,7 +187,8 @@ private:
|
||||
std::string indentString_;
|
||||
int rightMargin_;
|
||||
std::string indentation_;
|
||||
bool addChildValues_;
|
||||
bool addChildValues_ : 1;
|
||||
bool indented_ : 1;
|
||||
};
|
||||
|
||||
#if defined(JSON_HAS_INT64)
|
||||
|
@ -463,6 +463,7 @@ void StyledStreamWriter::write(std::ostream& out, const Value& root) {
|
||||
document_ = &out;
|
||||
addChildValues_ = false;
|
||||
indentString_ = "";
|
||||
indented_ = false;
|
||||
writeCommentBeforeValue(root);
|
||||
writeValue(root);
|
||||
writeCommentAfterValueOnSameLine(root);
|
||||
@ -539,8 +540,10 @@ void StyledStreamWriter::writeArrayValue(const Value& value) {
|
||||
if (hasChildValue)
|
||||
writeWithIndent(childValues_[index]);
|
||||
else {
|
||||
writeIndent();
|
||||
if (!indented_) writeIndent();
|
||||
indented_ = true;
|
||||
writeValue(childValue);
|
||||
indented_ = false;
|
||||
}
|
||||
if (++index == size) {
|
||||
writeCommentAfterValueOnSameLine(childValue);
|
||||
@ -598,24 +601,17 @@ void StyledStreamWriter::pushValue(const std::string& value) {
|
||||
}
|
||||
|
||||
void StyledStreamWriter::writeIndent() {
|
||||
/*
|
||||
Some comments in this method would have been nice. ;-)
|
||||
|
||||
if ( !document_.empty() )
|
||||
{
|
||||
char last = document_[document_.length()-1];
|
||||
if ( last == ' ' ) // already indented
|
||||
return;
|
||||
if ( last != '\n' ) // Comments may add new-line
|
||||
*document_ << '\n';
|
||||
}
|
||||
*/
|
||||
// blep intended this to look at the so-far-written string
|
||||
// to determine whether we are already indented, but
|
||||
// with a stream we cannot do that. So we rely on some saved state.
|
||||
// The caller checks indented_.
|
||||
*document_ << '\n' << indentString_;
|
||||
}
|
||||
|
||||
void StyledStreamWriter::writeWithIndent(const std::string& value) {
|
||||
writeIndent();
|
||||
if (!indented_) writeIndent();
|
||||
*document_ << value;
|
||||
indented_ = false;
|
||||
}
|
||||
|
||||
void StyledStreamWriter::indent() { indentString_ += indentation_; }
|
||||
@ -643,6 +639,7 @@ void StyledStreamWriter::writeCommentBeforeValue(const Value& root) {
|
||||
|
||||
// Comments are stripped of trailing newlines, so add one here
|
||||
*document_ << "\n";
|
||||
indented_ = false;
|
||||
}
|
||||
|
||||
void StyledStreamWriter::writeCommentAfterValueOnSameLine(const Value& root) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user