clarify Builders

This commit is contained in:
Christopher Dunn 2015-02-09 01:29:43 -06:00
parent 249fd18114
commit 66a8ba255f
5 changed files with 26 additions and 23 deletions

View File

@ -50,16 +50,8 @@ preserved.
\section _example Code example \section _example Code example
\code \code
Json::Value root; // will contain the root value after parsing. Json::Value root; // 'root' will contain the root value after parsing.
Json::Reader reader; std::cin >> root; // Or see CharReaderBuilder.
bool parsingSuccessful = reader.parse( config_doc, root );
if ( !parsingSuccessful )
{
// report to the user the failure and their locations in the document.
std::cout << "Failed to parse configuration\n"
<< reader.getFormattedErrorMessages();
return;
}
// 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.
@ -80,7 +72,7 @@ root["indent"]["length"] = getCurrentIndentLength();
root["indent"]["use_space"] = getCurrentIndentUseSpace(); root["indent"]["use_space"] = getCurrentIndentUseSpace();
// (NEW IN 1.4.0) // (NEW IN 1.4.0)
// To write into a steam with minimal memory overhead, // To write into a stream with minimal memory overhead,
// create a Builder for a StreamWriter. // create a Builder for a StreamWriter.
Json::StreamWriterBuilder builder; Json::StreamWriterBuilder builder;
builder.indentation_ = " "; // or whatever you like builder.indentation_ = " "; // or whatever you like

View File

@ -274,21 +274,27 @@ public:
}; // Factory }; // Factory
}; // CharReader }; // CharReader
/** \brief Build a CharReader implementation.
Usage:
\code
using namespace Json;
CharReaderBuilder builder;
builder.collectComments_ = true;
std::shared_ptr<CharReader> reader(
builder.newCharReader());
Value value;
std::string errs;
bool ok = parseFromStream(std::cin, &value, &errs);
\endcode
*/
class CharReaderBuilder : public CharReader::Factory { class CharReaderBuilder : public CharReader::Factory {
public:
bool collectComments_; bool collectComments_;
Features features_; Features features_;
public:
CharReaderBuilder(); CharReaderBuilder();
virtual ~CharReaderBuilder();
CharReaderBuilder& withCollectComments(bool v) {
collectComments_ = v;
return *this;
}
CharReaderBuilder& withFeatures(Features const& v) {
features_ = v;
return *this;
}
virtual CharReader* newCharReader() const; virtual CharReader* newCharReader() const;
}; };

View File

@ -105,9 +105,10 @@ public:
std::string indentation_; std::string indentation_;
StreamWriterBuilder(); StreamWriterBuilder();
virtual ~StreamWriterBuilder();
/// Do not take ownership of sout, but maintain a reference. /// Do not take ownership of sout, but maintain a reference.
StreamWriter* newStreamWriter(std::ostream* sout) const; virtual StreamWriter* newStreamWriter(std::ostream* sout) const;
}; };
/** \brief Build a StreamWriter implementation. /** \brief Build a StreamWriter implementation.

View File

@ -915,6 +915,8 @@ CharReaderBuilder::CharReaderBuilder()
: collectComments_(true) : collectComments_(true)
, features_(Features::all()) , features_(Features::all())
{} {}
CharReaderBuilder::~CharReaderBuilder()
{}
CharReader* CharReaderBuilder::newCharReader() const CharReader* CharReaderBuilder::newCharReader() const
{ {
return new OldReader(collectComments_, features_); return new OldReader(collectComments_, features_);

View File

@ -971,6 +971,8 @@ StreamWriterBuilder::StreamWriterBuilder()
: cs_(StreamWriter::CommentStyle::All) : cs_(StreamWriter::CommentStyle::All)
, indentation_("\t") , indentation_("\t")
{} {}
StreamWriterBuilder::~StreamWriterBuilder()
{}
StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const
{ {
std::string colonSymbol = " : "; std::string colonSymbol = " : ";