mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-06-07 09:04:57 +02: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_;
|
std::string indentString_;
|
||||||
int rightMargin_;
|
int rightMargin_;
|
||||||
std::string indentation_;
|
std::string indentation_;
|
||||||
bool addChildValues_;
|
bool addChildValues_ : 1;
|
||||||
|
bool indented_ : 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
#if defined(JSON_HAS_INT64)
|
#if defined(JSON_HAS_INT64)
|
||||||
|
@ -463,6 +463,7 @@ void StyledStreamWriter::write(std::ostream& out, const Value& root) {
|
|||||||
document_ = &out;
|
document_ = &out;
|
||||||
addChildValues_ = false;
|
addChildValues_ = false;
|
||||||
indentString_ = "";
|
indentString_ = "";
|
||||||
|
indented_ = false;
|
||||||
writeCommentBeforeValue(root);
|
writeCommentBeforeValue(root);
|
||||||
writeValue(root);
|
writeValue(root);
|
||||||
writeCommentAfterValueOnSameLine(root);
|
writeCommentAfterValueOnSameLine(root);
|
||||||
@ -539,8 +540,10 @@ void StyledStreamWriter::writeArrayValue(const Value& value) {
|
|||||||
if (hasChildValue)
|
if (hasChildValue)
|
||||||
writeWithIndent(childValues_[index]);
|
writeWithIndent(childValues_[index]);
|
||||||
else {
|
else {
|
||||||
writeIndent();
|
if (!indented_) writeIndent();
|
||||||
|
indented_ = true;
|
||||||
writeValue(childValue);
|
writeValue(childValue);
|
||||||
|
indented_ = false;
|
||||||
}
|
}
|
||||||
if (++index == size) {
|
if (++index == size) {
|
||||||
writeCommentAfterValueOnSameLine(childValue);
|
writeCommentAfterValueOnSameLine(childValue);
|
||||||
@ -598,24 +601,17 @@ void StyledStreamWriter::pushValue(const std::string& value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void StyledStreamWriter::writeIndent() {
|
void StyledStreamWriter::writeIndent() {
|
||||||
/*
|
// blep intended this to look at the so-far-written string
|
||||||
Some comments in this method would have been nice. ;-)
|
// to determine whether we are already indented, but
|
||||||
|
// with a stream we cannot do that. So we rely on some saved state.
|
||||||
if ( !document_.empty() )
|
// The caller checks indented_.
|
||||||
{
|
|
||||||
char last = document_[document_.length()-1];
|
|
||||||
if ( last == ' ' ) // already indented
|
|
||||||
return;
|
|
||||||
if ( last != '\n' ) // Comments may add new-line
|
|
||||||
*document_ << '\n';
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
*document_ << '\n' << indentString_;
|
*document_ << '\n' << indentString_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void StyledStreamWriter::writeWithIndent(const std::string& value) {
|
void StyledStreamWriter::writeWithIndent(const std::string& value) {
|
||||||
writeIndent();
|
if (!indented_) writeIndent();
|
||||||
*document_ << value;
|
*document_ << value;
|
||||||
|
indented_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void StyledStreamWriter::indent() { indentString_ += indentation_; }
|
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
|
// Comments are stripped of trailing newlines, so add one here
|
||||||
*document_ << "\n";
|
*document_ << "\n";
|
||||||
|
indented_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void StyledStreamWriter::writeCommentAfterValueOnSameLine(const Value& root) {
|
void StyledStreamWriter::writeCommentAfterValueOnSameLine(const Value& root) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user