Skip to content

Commit 4d64940

Browse files
committed
setIndentation()
1 parent 489707f commit 4d64940

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

include/json/writer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ class JSON_API StreamWriter {
6565
~Builder(); // delete underlying StreamWriterBuilder
6666

6767
void setCommentStyle(CommentStyle cs); /// default: All
68+
/** \brief Write in human-friendly style.
69+
70+
If "", then skip all indentation, newlines, and comments,
71+
which implies CommentStyle::None.
72+
Default: "\t"
73+
*/
74+
void setIndentation(std::string indentation);
6875

6976
/// Do not take ownership of sout, but maintain a reference.
7077
StreamWriter* newStreamWriter(std::ostream* sout);

src/lib_json/json_writer.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -699,17 +699,24 @@ int MyStreamWriter::write(Value const& root) const
699699
class StreamWriterBuilder {
700700
typedef StreamWriter::CommentStyle CommentStyle;
701701
CommentStyle cs_;
702+
std::string indentation_;
702703
public:
703704
virtual ~StreamWriterBuilder();
704705
virtual void setCommentStyle(CommentStyle cs);
706+
virtual void setIndentation(std::string indentation);
705707
virtual StreamWriter* newStreamWriter(std::ostream* sout) const;
706708
};
707709
StreamWriterBuilder::~StreamWriterBuilder()
708710
{
709711
}
710-
void StreamWriterBuilder::setCommentStyle(CommentStyle cs)
712+
void StreamWriterBuilder::setCommentStyle(CommentStyle v)
711713
{
712-
cs_ = cs;
714+
cs_ = v;
715+
}
716+
void StreamWriterBuilder::setIndentation(std::string v)
717+
{
718+
indentation_ = v;
719+
if (indentation_.empty()) cs_ = CommentStyle::None;
713720
}
714721
StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const
715722
{
@@ -732,9 +739,17 @@ StreamWriter::Builder::~Builder()
732739
{
733740
delete own_;
734741
}
735-
void StreamWriter::Builder::setCommentStyle(CommentStyle cs)
742+
void StreamWriter::Builder::setCommentStyle(CommentStyle v)
743+
{
744+
own_->setCommentStyle(v);
745+
}
746+
void StreamWriter::Builder::setIndentation(std::string v)
747+
{
748+
own_->setIndentation(v);
749+
}
750+
StreamWriter* StreamWriter::Builder::newStreamWriter(std::ostream* sout)
736751
{
737-
own_->setCommentStyle(cs);
752+
return own_->newStreamWriter(sout);
738753
}
739754

740755
/// Do not take ownership of sout, but maintain a reference.

0 commit comments

Comments
 (0)