Merge pull request #199 from open-source-parsers/set-builders

Setting builders
This commit is contained in:
Christopher Dunn 2015-03-04 16:09:37 -06:00
commit 2aa4557e79
6 changed files with 24 additions and 7 deletions

View File

@ -96,13 +96,13 @@ features without losing binary-compatibility.
\code \code
// For convenience, use `writeString()` with a specialized builder. // For convenience, use `writeString()` with a specialized builder.
Json::StreamWriterBuilder wbuilder; Json::StreamWriterBuilder wbuilder;
wbuilder.settings_["indentation"] = "\t"; // simple Json::Value wbuilder["indentation"] = "\t";
std::string document = Json::writeString(wbuilder, root); std::string document = Json::writeString(wbuilder, root);
// Here, using a specialized Builder, we discard comments and // Here, using a specialized Builder, we discard comments and
// record errors as we parse. // record errors as we parse.
Json::CharReaderBuilder rbuilder; Json::CharReaderBuilder rbuilder;
rbuilder.settings_["collectComments"] = false; // simple Json::Value rbuilder["collectComments"] = false;
std::string errs; std::string errs;
bool ok = Json::parseFromStream(rbuilder, std::cin, &root, &errs); bool ok = Json::parseFromStream(rbuilder, std::cin, &root, &errs);
\endcode \endcode

View File

@ -285,7 +285,7 @@ Usage:
\code \code
using namespace Json; using namespace Json;
CharReaderBuilder builder; CharReaderBuilder builder;
builder.settings_["collectComments"] = false; builder["collectComments"] = false;
Value value; Value value;
std::string errs; std::string errs;
bool ok = parseFromStream(builder, std::cin, &value, &errs); bool ok = parseFromStream(builder, std::cin, &value, &errs);
@ -337,6 +337,11 @@ public:
* otherwise, indicate bad settings via 'invalid'. * otherwise, indicate bad settings via 'invalid'.
*/ */
bool validate(Json::Value* invalid) const; bool validate(Json::Value* invalid) const;
/** A simple way to update a specific setting.
*/
Value& operator[](std::string key);
/** Called by ctor, but you can use this to reset settings_. /** Called by ctor, but you can use this to reset settings_.
* \pre 'settings' != NULL (but Json::null is fine) * \pre 'settings' != NULL (but Json::null is fine)
* \remark Defaults: * \remark Defaults:

View File

@ -76,8 +76,8 @@ Usage:
using namespace Json; using namespace Json;
Value value = ...; Value value = ...;
StreamWriterBuilder builder; StreamWriterBuilder builder;
builder.settings_["commentStyle"] = "None"; builder["commentStyle"] = "None";
builder.settings_["indentation"] = " "; // or whatever you like builder["indentation"] = " "; // or whatever you like
std::unique_ptr<Json::StreamWriter> writer( std::unique_ptr<Json::StreamWriter> writer(
builder.newStreamWriter()); builder.newStreamWriter());
writer->write(value, &std::cout); writer->write(value, &std::cout);
@ -119,6 +119,10 @@ public:
* otherwise, indicate bad settings via 'invalid'. * otherwise, indicate bad settings via 'invalid'.
*/ */
bool validate(Json::Value* invalid) const; bool validate(Json::Value* invalid) const;
/** A simple way to update a specific setting.
*/
Value& operator[](std::string key);
/** Called by ctor, but you can use this to reset settings_. /** Called by ctor, but you can use this to reset settings_.
* \pre 'settings' != NULL (but Json::null is fine) * \pre 'settings' != NULL (but Json::null is fine)
* \remark Defaults: * \remark Defaults:

View File

@ -1927,6 +1927,10 @@ bool CharReaderBuilder::validate(Json::Value* invalid) const
} }
return 0u == inv.size(); return 0u == inv.size();
} }
Value& CharReaderBuilder::operator[](std::string key)
{
return settings_[key];
}
// static // static
void CharReaderBuilder::strictMode(Json::Value* settings) void CharReaderBuilder::strictMode(Json::Value* settings)
{ {

View File

@ -1122,6 +1122,10 @@ bool StreamWriterBuilder::validate(Json::Value* invalid) const
} }
return 0u == inv.size(); return 0u == inv.size();
} }
Value& StreamWriterBuilder::operator[](std::string key)
{
return settings_[key];
}
// static // static
void StreamWriterBuilder::setDefaults(Json::Value* settings) void StreamWriterBuilder::setDefaults(Json::Value* settings)
{ {

View File

@ -2182,7 +2182,7 @@ JSONTEST_FIXTURE(BuilderTest, settings) {
Json::CharReaderBuilder rb; Json::CharReaderBuilder rb;
JSONTEST_ASSERT_EQUAL(false, rb.settings_.isMember("foo")); JSONTEST_ASSERT_EQUAL(false, rb.settings_.isMember("foo"));
JSONTEST_ASSERT_EQUAL(true, rb.validate(&errs)); JSONTEST_ASSERT_EQUAL(true, rb.validate(&errs));
rb.settings_["foo"] = "bar"; rb["foo"] = "bar";
JSONTEST_ASSERT_EQUAL(true, rb.settings_.isMember("foo")); JSONTEST_ASSERT_EQUAL(true, rb.settings_.isMember("foo"));
JSONTEST_ASSERT_EQUAL(false, rb.validate(&errs)); JSONTEST_ASSERT_EQUAL(false, rb.validate(&errs));
} }
@ -2191,7 +2191,7 @@ JSONTEST_FIXTURE(BuilderTest, settings) {
Json::StreamWriterBuilder wb; Json::StreamWriterBuilder wb;
JSONTEST_ASSERT_EQUAL(false, wb.settings_.isMember("foo")); JSONTEST_ASSERT_EQUAL(false, wb.settings_.isMember("foo"));
JSONTEST_ASSERT_EQUAL(true, wb.validate(&errs)); JSONTEST_ASSERT_EQUAL(true, wb.validate(&errs));
wb.settings_["foo"] = "bar"; wb["foo"] = "bar";
JSONTEST_ASSERT_EQUAL(true, wb.settings_.isMember("foo")); JSONTEST_ASSERT_EQUAL(true, wb.settings_.isMember("foo"));
JSONTEST_ASSERT_EQUAL(false, wb.validate(&errs)); JSONTEST_ASSERT_EQUAL(false, wb.validate(&errs));
} }