Skip to content

Commit 694dbcb

Browse files
committed
update docs, writeString()
1 parent 732abb8 commit 694dbcb

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

doc/jsoncpp.dox

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,26 @@ preserved.
5353
Json::Value root; // 'root' will contain the root value after parsing.
5454
std::cin >> root;
5555

56+
// You can also read into a particular sub-value.
57+
std::cin >> root["subtree"];
58+
5659
// Get the value of the member of root named 'encoding', return 'UTF-8' if there is no
5760
// such member.
5861
std::string encoding = root.get("encoding", "UTF-8" ).asString();
59-
// Get the value of the member of root named 'encoding', return a 'null' value if
62+
// Get the value of the member of root named 'encoding'; return a 'null' value if
6063
// there is no such member.
6164
const Json::Value plugins = root["plug-ins"];
6265
for ( int index = 0; index < plugins.size(); ++index ) // Iterates over the sequence elements.
6366
loadPlugIn( plugins[index].asString() );
6467

65-
setIndentLength( root["indent"].get("length", 3).asInt() );
66-
setIndentUseSpace( root["indent"].get("use_space", true).asBool() );
68+
foo::setIndentLength( root["indent"].get("length", 3).asInt() );
69+
foo::setIndentUseSpace( root["indent"].get("use_space", true).asBool() );
6770

6871
// Since Json::Value has implicit constructor for all value types, it is not
6972
// necessary to explicitly construct the Json::Value object:
70-
root["encoding"] = getCurrentEncoding();
71-
root["indent"]["length"] = getCurrentIndentLength();
72-
root["indent"]["use_space"] = getCurrentIndentUseSpace();
73+
root["encoding"] = foo::getCurrentEncoding();
74+
root["indent"]["length"] = foo::getCurrentIndentLength();
75+
root["indent"]["use_space"] = foo::getCurrentIndentUseSpace();
7376

7477
// If you like the defaults, you can insert directly into a stream.
7578
std::cout << root;
@@ -80,27 +83,24 @@ std::cout << std::endl;
8083
\endcode
8184

8285
\section _advanced Advanced usage
83-
We are finalizing the new *Builder* API, which will be in versions
84-
`1.4.0` and `0.8.0` when released. Until then, you may continue to
85-
use the old API, include `Writer`, `Reader`, and `Feature`.
86-
\code
8786

88-
// EXPERIMENTAL
89-
// Or use `writeString()` for convenience, with a specialized builder.
90-
Json::StreamWriterBuilder wbuilder;
91-
builder.indentation_ = "\t";
92-
std::string document = Json::writeString(root, wbuilder);
87+
Configure *builders* to create *readers* and *writers*. For
88+
configuration, we use our own `Json::Value` (rather than
89+
standard setters/getters) so that we can add
90+
features without losing binary-compatibility.
9391

94-
// You can also read into a particular sub-value.
95-
std::cin >> root["subtree"];
92+
\code
93+
// For convenience, use `writeString()` with a specialized builder.
94+
Json::StreamWriterBuilder wbuilder;
95+
wbuilder.settings["indentation"] = "\t";
96+
std::string document = Json::writeString(wbuilder, root);
9697

97-
// EXPERIMENTAL
98-
// Here we use a specialized Builder, discard comments, and
99-
// record errors.
98+
// Here, using a specialized Builder, we discard comments and
99+
// record errors as we parse.
100100
Json::CharReaderBuilder rbuilder;
101-
rbuilder.collectComments_ = false;
101+
rbuilder.settings["collectComments"] = false;
102102
std::string errs;
103-
Json::parseFromStream(rbuilder, std::cin, &root["subtree"], &errs);
103+
bool ok = Json::parseFromStream(rbuilder, std::cin, &root, &errs);
104104
\endcode
105105

106106
\section _pbuild Build instructions

include/json/writer.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ class JSON_API StreamWriter {
6969
}; // Factory
7070
}; // StreamWriter
7171

72-
/// \brief Write into stringstream, then return string, for convenience.
73-
std::string writeString(Value const& root, StreamWriter::Factory const& factory);
72+
/** \brief Write into stringstream, then return string, for convenience.
73+
* A StreamWriter will be created from the factory, used, and then deleted.
74+
*/
75+
std::string writeString(StreamWriter::Factory const& factory, Value const& root);
7476

7577

7678
/** \brief Build a StreamWriter implementation.

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::StreamWriterBuilder builder;
188-
return writeString(root, builder);
188+
return Json::writeString(builder, root);
189189
}
190190
static int rewriteValueTree(
191191
const std::string& rewritePath,

src/lib_json/json_writer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,10 +1005,10 @@ StreamWriter* OldCompressingStreamWriterBuilder::newStreamWriter(
10051005
colonSymbol, nullSymbol, endingLineFeedSymbol);
10061006
}
10071007

1008-
std::string writeString(Value const& root, StreamWriter::Factory const& builder) {
1008+
std::string writeString(StreamWriter::Factory const& builder, Value const& root) {
10091009
std::ostringstream sout;
1010-
StreamWriterPtr const sw(builder.newStreamWriter(&sout));
1011-
sw->write(root);
1010+
StreamWriterPtr const writer(builder.newStreamWriter(&sout));
1011+
writer->write(root);
10121012
return sout.str();
10131013
}
10141014

0 commit comments

Comments
 (0)