improve docs

including `writeString()`
This commit is contained in:
Christopher Dunn 2015-01-25 19:20:43 -06:00
parent 54b8e6939a
commit 9da9f84903
2 changed files with 13 additions and 8 deletions

View File

@ -85,9 +85,8 @@ Json::StreamWriter::Builder builder;
builder.withIndentation(" "); // or whatever you like builder.withIndentation(" "); // or whatever you like
// Then build a StreamWriter. // Then build a StreamWriter.
// (Of course, you can write to std::ostringstream if you prefer.)
std::shared_ptr<Json::StreamWriter> writer( std::shared_ptr<Json::StreamWriter> writer(
builder.newStreamWriter( &std::cout ); builder.newStreamWriter( &std::cout ) );
// Make a new JSON document for the configuration. Preserve original comments. // Make a new JSON document for the configuration. Preserve original comments.
writer->write( root ); writer->write( root );
@ -95,6 +94,10 @@ writer->write( root );
// 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;
// Of course, you can write to `std::ostringstream` if you prefer. Or
// use `writeString()` for convenience.
std::string document = Json::writeString( root, builder );
// You can also read from a stream. This will put the contents of any JSON // You can also read from a stream. This will put the contents of any JSON
// stream at a particular sub-value, if you'd like. // stream at a particular sub-value, if you'd like.
std::cin >> root["subtree"]; std::cin >> root["subtree"];

View File

@ -27,7 +27,7 @@ class StreamWriterBuilder;
/** /**
Usage: Usage:
\code
using namespace Json; using namespace Json;
Value value; Value value;
StreamWriter::Builder builder; StreamWriter::Builder builder;
@ -36,16 +36,18 @@ Usage:
builder.newStreamWriter(&std::cout)); builder.newStreamWriter(&std::cout));
writer->write(value); writer->write(value);
std::cout.flush(); std::cout.flush();
\endcode
*/ */
class JSON_API StreamWriter { class JSON_API StreamWriter {
protected: protected:
std::ostream& sout_; // not owned; will not delete std::ostream& sout_; // not owned; will not delete
public: public:
/// `All`: Keep all comments. /// Decide whether to write comments.
/// `None`: Drop all comments. enum class CommentStyle {
/// Use `Most` to recover the odd behavior of previous versions. None, ///< Drop all comments.
/// Only `All` is currently implemented. Most, ///< Recover odd behavior of previous versions (not implemented yet).
enum class CommentStyle {None, Most, All}; All ///< Keep all comments.
};
/// Keep a reference, but do not take ownership of `sout`. /// Keep a reference, but do not take ownership of `sout`.
StreamWriter(std::ostream* sout); StreamWriter(std::ostream* sout);