Skip to content

Commit c7b39c2

Browse files
committed
deprecate old Writers
also, use withers instead of setters, and update docs
1 parent d78caa3 commit c7b39c2

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

doc/jsoncpp.dox

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,31 @@ for ( int index = 0; index < plugins.size(); ++index ) // Iterates over the seq
7373
setIndentLength( root["indent"].get("length", 3).asInt() );
7474
setIndentUseSpace( root["indent"].get("use_space", true).asBool() );
7575

76-
// ...
77-
// At application shutdown to make the new configuration document:
7876
// Since Json::Value has implicit constructor for all value types, it is not
7977
// necessary to explicitly construct the Json::Value object:
8078
root["encoding"] = getCurrentEncoding();
8179
root["indent"]["length"] = getCurrentIndentLength();
8280
root["indent"]["use_space"] = getCurrentIndentUseSpace();
8381

84-
Json::StyledWriter writer;
82+
// To write into a steam with minimal memory overhead,
83+
// create a Builder for a StreamWriter.
84+
Json::StreamWriter::Builder builder;
85+
builder.withIndentation(" "); // or whatever you like
86+
87+
// Then build a StreamWriter.
88+
// (Of course, you can write to std::ostringstream if you prefer.)
89+
std::shared_ptr<Json::StreamWriter> writer(
90+
builder.newStreamWriter( &std::cout );
91+
8592
// Make a new JSON document for the configuration. Preserve original comments.
86-
std::string outputConfig = writer.write( root );
93+
writer->write( root );
94+
95+
// If you like the defaults, you can insert directly into a stream.
96+
std::cout << root;
8797

88-
// You can also use streams. This will put the contents of any JSON
98+
// You can also read from a stream. This will put the contents of any JSON
8999
// stream at a particular sub-value, if you'd like.
90100
std::cin >> root["subtree"];
91-
92-
// And you can write to a stream, using the StyledWriter automatically.
93-
std::cout << root;
94101
\endcode
95102

96103
\section _pbuild Build instructions

include/json/writer.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class StreamWriterBuilder;
3131
using namespace Json;
3232
Value value;
3333
StreamWriter::Builder builder;
34-
builder.setCommentStyle(StreamWriter::CommentStyle::None);
34+
builder.withCommentStyle(StreamWriter::CommentStyle::None);
3535
std::shared_ptr<StreamWriter> writer(
3636
builder.newStreamWriter(&std::cout));
3737
writer->write(value);
@@ -66,32 +66,32 @@ class JSON_API StreamWriter {
6666
Builder();
6767
~Builder(); // delete underlying StreamWriterBuilder
6868

69-
void setCommentStyle(CommentStyle cs); /// default: All
69+
Builder& withCommentStyle(CommentStyle cs); /// default: All
7070
/** \brief Write in human-friendly style.
7171
7272
If "", then skip all indentation, newlines, and comments,
7373
which implies CommentStyle::None.
7474
Default: "\t"
7575
*/
76-
void setIndentation(std::string indentation);
76+
Builder& withIndentation(std::string indentation);
7777
/** \brief Drop the "null" string from the writer's output for nullValues.
7878
* Strictly speaking, this is not valid JSON. But when the output is being
7979
* fed to a browser's Javascript, it makes for smaller output and the
8080
* browser can handle the output just fine.
8181
*/
82-
void setDropNullPlaceholders(bool v);
82+
Builder& withDropNullPlaceholders(bool v);
8383
/** \brief Do not add \n at end of document.
8484
* Normally, we add an extra newline, just because.
8585
*/
86-
void setOmitEndingLineFeed(bool v);
86+
Builder& withOmitEndingLineFeed(bool v);
8787
/** \brief Add a space after ':'.
8888
* If indentation is non-empty, we surround colon with whitespace,
8989
* e.g. " : "
9090
* This will add back the trailing space when there is no indentation.
9191
* This seems dubious when the entire document is on a single line,
9292
* but we leave this here to repduce the behavior of the old `FastWriter`.
9393
*/
94-
void setEnableYAMLCompatibility(bool v);
94+
Builder& withEnableYAMLCompatibility(bool v);
9595

9696
/// Do not take ownership of sout, but maintain a reference.
9797
StreamWriter* newStreamWriter(std::ostream* sout) const;
@@ -103,6 +103,7 @@ std::string writeString(Value const& root, StreamWriter::Builder const& builder)
103103

104104

105105
/** \brief Abstract class for writers.
106+
* \deprecated Use StreamWriter::Builder.
106107
*/
107108
class JSON_API Writer {
108109
public:
@@ -118,6 +119,7 @@ class JSON_API Writer {
118119
*consumption,
119120
* but may be usefull to support feature such as RPC where bandwith is limited.
120121
* \sa Reader, Value
122+
* \deprecated Use StreamWriter::Builder.
121123
*/
122124
class JSON_API FastWriter : public Writer {
123125
public:
@@ -169,6 +171,7 @@ class JSON_API FastWriter : public Writer {
169171
*#CommentPlacement.
170172
*
171173
* \sa Reader, Value, Value::setComment()
174+
* \deprecated Use StreamWriter::Builder.
172175
*/
173176
class JSON_API StyledWriter : public Writer {
174177
public:
@@ -230,6 +233,7 @@ class JSON_API StyledWriter : public Writer {
230233
*
231234
* \param indentation Each level will be indented by this amount extra.
232235
* \sa Reader, Value, Value::setComment()
236+
* \deprecated Use StreamWriter::Builder.
233237
*/
234238
class JSON_API StyledStreamWriter {
235239
public:

src/jsontestrunner/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static std::string useBuiltStyledStreamWriter(
185185
Json::Value const& root)
186186
{
187187
Json::StreamWriter::Builder builder;
188-
builder.setCommentStyle(Json::StreamWriter::CommentStyle::All);
188+
builder.withCommentStyle(Json::StreamWriter::CommentStyle::All);
189189
return writeString(root, builder);
190190
}
191191
static int rewriteValueTree(

src/lib_json/json_writer.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,25 +1058,30 @@ StreamWriter::Builder::Builder(Builder const&)
10581058
{abort();}
10591059
void StreamWriter::Builder::operator=(Builder const&)
10601060
{abort();}
1061-
void StreamWriter::Builder::setCommentStyle(CommentStyle v)
1061+
StreamWriter::Builder& StreamWriter::Builder::withCommentStyle(CommentStyle v)
10621062
{
10631063
own_->setCommentStyle(v);
1064+
return *this;
10641065
}
1065-
void StreamWriter::Builder::setIndentation(std::string v)
1066+
StreamWriter::Builder& StreamWriter::Builder::withIndentation(std::string v)
10661067
{
10671068
own_->setIndentation(v);
1069+
return *this;
10681070
}
1069-
void StreamWriter::Builder::setDropNullPlaceholders(bool v)
1071+
StreamWriter::Builder& StreamWriter::Builder::withDropNullPlaceholders(bool v)
10701072
{
10711073
own_->setDropNullPlaceholders(v);
1074+
return *this;
10721075
}
1073-
void StreamWriter::Builder::setOmitEndingLineFeed(bool v)
1076+
StreamWriter::Builder& StreamWriter::Builder::withOmitEndingLineFeed(bool v)
10741077
{
10751078
own_->setOmitEndingLineFeed(v);
1079+
return *this;
10761080
}
1077-
void StreamWriter::Builder::setEnableYAMLCompatibility(bool v)
1081+
StreamWriter::Builder& StreamWriter::Builder::withEnableYAMLCompatibility(bool v)
10781082
{
10791083
own_->setEnableYAMLCompatibility(v);
1084+
return *this;
10801085
}
10811086
StreamWriter* StreamWriter::Builder::newStreamWriter(std::ostream* sout) const
10821087
{
@@ -1092,8 +1097,8 @@ std::string writeString(Value const& root, StreamWriter::Builder const& builder)
10921097

10931098
std::ostream& operator<<(std::ostream& sout, Value const& root) {
10941099
StreamWriter::Builder builder;
1095-
builder.setCommentStyle(StreamWriter::CommentStyle::All);
1096-
builder.setIndentation("\t");
1100+
builder.withCommentStyle(StreamWriter::CommentStyle::All);
1101+
builder.withIndentation("\t");
10971102
std::shared_ptr<StreamWriter> writer(builder.newStreamWriter(&sout));
10981103
writer->write(root);
10991104
return sout;

0 commit comments

Comments
 (0)