mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-01-07 09:48:03 +01:00
update docs, writeString()
This commit is contained in:
parent
732abb80ef
commit
694dbcb328
@ -53,23 +53,26 @@ preserved.
|
|||||||
Json::Value root; // 'root' will contain the root value after parsing.
|
Json::Value root; // 'root' will contain the root value after parsing.
|
||||||
std::cin >> root;
|
std::cin >> root;
|
||||||
|
|
||||||
|
// You can also read into a particular sub-value.
|
||||||
|
std::cin >> root["subtree"];
|
||||||
|
|
||||||
// Get the value of the member of root named 'encoding', return 'UTF-8' if there is no
|
// Get the value of the member of root named 'encoding', return 'UTF-8' if there is no
|
||||||
// such member.
|
// such member.
|
||||||
std::string encoding = root.get("encoding", "UTF-8" ).asString();
|
std::string encoding = root.get("encoding", "UTF-8" ).asString();
|
||||||
// Get the value of the member of root named 'encoding', return a 'null' value if
|
// Get the value of the member of root named 'encoding'; return a 'null' value if
|
||||||
// there is no such member.
|
// there is no such member.
|
||||||
const Json::Value plugins = root["plug-ins"];
|
const Json::Value plugins = root["plug-ins"];
|
||||||
for ( int index = 0; index < plugins.size(); ++index ) // Iterates over the sequence elements.
|
for ( int index = 0; index < plugins.size(); ++index ) // Iterates over the sequence elements.
|
||||||
loadPlugIn( plugins[index].asString() );
|
loadPlugIn( plugins[index].asString() );
|
||||||
|
|
||||||
setIndentLength( root["indent"].get("length", 3).asInt() );
|
foo::setIndentLength( root["indent"].get("length", 3).asInt() );
|
||||||
setIndentUseSpace( root["indent"].get("use_space", true).asBool() );
|
foo::setIndentUseSpace( root["indent"].get("use_space", true).asBool() );
|
||||||
|
|
||||||
// Since Json::Value has implicit constructor for all value types, it is not
|
// Since Json::Value has implicit constructor for all value types, it is not
|
||||||
// necessary to explicitly construct the Json::Value object:
|
// necessary to explicitly construct the Json::Value object:
|
||||||
root["encoding"] = getCurrentEncoding();
|
root["encoding"] = foo::getCurrentEncoding();
|
||||||
root["indent"]["length"] = getCurrentIndentLength();
|
root["indent"]["length"] = foo::getCurrentIndentLength();
|
||||||
root["indent"]["use_space"] = getCurrentIndentUseSpace();
|
root["indent"]["use_space"] = foo::getCurrentIndentUseSpace();
|
||||||
|
|
||||||
// If you like the defaults, you can insert directly into a stream.
|
// If you like the defaults, you can insert directly into a stream.
|
||||||
std::cout << root;
|
std::cout << root;
|
||||||
@ -80,27 +83,24 @@ std::cout << std::endl;
|
|||||||
\endcode
|
\endcode
|
||||||
|
|
||||||
\section _advanced Advanced usage
|
\section _advanced Advanced usage
|
||||||
We are finalizing the new *Builder* API, which will be in versions
|
|
||||||
`1.4.0` and `0.8.0` when released. Until then, you may continue to
|
Configure *builders* to create *readers* and *writers*. For
|
||||||
use the old API, include `Writer`, `Reader`, and `Feature`.
|
configuration, we use our own `Json::Value` (rather than
|
||||||
|
standard setters/getters) so that we can add
|
||||||
|
features without losing binary-compatibility.
|
||||||
|
|
||||||
\code
|
\code
|
||||||
|
// For convenience, use `writeString()` with a specialized builder.
|
||||||
// EXPERIMENTAL
|
|
||||||
// Or use `writeString()` for convenience, with a specialized builder.
|
|
||||||
Json::StreamWriterBuilder wbuilder;
|
Json::StreamWriterBuilder wbuilder;
|
||||||
builder.indentation_ = "\t";
|
wbuilder.settings["indentation"] = "\t";
|
||||||
std::string document = Json::writeString(root, wbuilder);
|
std::string document = Json::writeString(wbuilder, root);
|
||||||
|
|
||||||
// You can also read into a particular sub-value.
|
// Here, using a specialized Builder, we discard comments and
|
||||||
std::cin >> root["subtree"];
|
// record errors as we parse.
|
||||||
|
|
||||||
// EXPERIMENTAL
|
|
||||||
// Here we use a specialized Builder, discard comments, and
|
|
||||||
// record errors.
|
|
||||||
Json::CharReaderBuilder rbuilder;
|
Json::CharReaderBuilder rbuilder;
|
||||||
rbuilder.collectComments_ = false;
|
rbuilder.settings["collectComments"] = false;
|
||||||
std::string errs;
|
std::string errs;
|
||||||
Json::parseFromStream(rbuilder, std::cin, &root["subtree"], &errs);
|
bool ok = Json::parseFromStream(rbuilder, std::cin, &root, &errs);
|
||||||
\endcode
|
\endcode
|
||||||
|
|
||||||
\section _pbuild Build instructions
|
\section _pbuild Build instructions
|
||||||
|
@ -69,8 +69,10 @@ public:
|
|||||||
}; // Factory
|
}; // Factory
|
||||||
}; // StreamWriter
|
}; // StreamWriter
|
||||||
|
|
||||||
/// \brief Write into stringstream, then return string, for convenience.
|
/** \brief Write into stringstream, then return string, for convenience.
|
||||||
std::string writeString(Value const& root, StreamWriter::Factory const& factory);
|
* A StreamWriter will be created from the factory, used, and then deleted.
|
||||||
|
*/
|
||||||
|
std::string writeString(StreamWriter::Factory const& factory, Value const& root);
|
||||||
|
|
||||||
|
|
||||||
/** \brief Build a StreamWriter implementation.
|
/** \brief Build a StreamWriter implementation.
|
||||||
|
@ -185,7 +185,7 @@ static std::string useBuiltStyledStreamWriter(
|
|||||||
Json::Value const& root)
|
Json::Value const& root)
|
||||||
{
|
{
|
||||||
Json::StreamWriterBuilder builder;
|
Json::StreamWriterBuilder builder;
|
||||||
return writeString(root, builder);
|
return Json::writeString(builder, root);
|
||||||
}
|
}
|
||||||
static int rewriteValueTree(
|
static int rewriteValueTree(
|
||||||
const std::string& rewritePath,
|
const std::string& rewritePath,
|
||||||
|
@ -1005,10 +1005,10 @@ StreamWriter* OldCompressingStreamWriterBuilder::newStreamWriter(
|
|||||||
colonSymbol, nullSymbol, endingLineFeedSymbol);
|
colonSymbol, nullSymbol, endingLineFeedSymbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string writeString(Value const& root, StreamWriter::Factory const& builder) {
|
std::string writeString(StreamWriter::Factory const& builder, Value const& root) {
|
||||||
std::ostringstream sout;
|
std::ostringstream sout;
|
||||||
StreamWriterPtr const sw(builder.newStreamWriter(&sout));
|
StreamWriterPtr const writer(builder.newStreamWriter(&sout));
|
||||||
sw->write(root);
|
writer->write(root);
|
||||||
return sout.str();
|
return sout.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user