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
\code
Json::Value root; // will contain the root value after parsing.
Json::Reader reader;
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;
}
Json::Value root; // 'root' will contain the root value after parsing.
std::cin >> root; // Or see CharReaderBuilder.
// Get the value of the member of root named 'encoding', return 'UTF-8' if there is no
// such member.
@ -80,7 +72,7 @@ root["indent"]["length"] = getCurrentIndentLength();
root["indent"]["use_space"] = getCurrentIndentUseSpace();
// (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.
Json::StreamWriterBuilder builder;
builder.indentation_ = " "; // or whatever you like

View File

@ -274,21 +274,27 @@ public:
}; // Factory
}; // 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 {
public:
bool collectComments_;
Features features_;
public:
CharReaderBuilder();
CharReaderBuilder& withCollectComments(bool v) {
collectComments_ = v;
return *this;
}
CharReaderBuilder& withFeatures(Features const& v) {
features_ = v;
return *this;
}
virtual ~CharReaderBuilder();
virtual CharReader* newCharReader() const;
};

View File

@ -105,9 +105,10 @@ public:
std::string indentation_;
StreamWriterBuilder();
virtual ~StreamWriterBuilder();
/// 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.

View File

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

View File

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