From c312dd5ef7288d9f43c6acd8a39064f70200c3f2 Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Wed, 4 Mar 2015 14:56:37 -0600 Subject: [PATCH 1/2] Builder::operator[] plus tests --- include/json/reader.h | 7 ++++++- include/json/writer.h | 4 ++++ src/lib_json/json_reader.cpp | 4 ++++ src/lib_json/json_writer.cpp | 4 ++++ src/test_lib_json/main.cpp | 4 ++-- 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/json/reader.h b/include/json/reader.h index 154acbd..64650ec 100644 --- a/include/json/reader.h +++ b/include/json/reader.h @@ -285,7 +285,7 @@ Usage: \code using namespace Json; CharReaderBuilder builder; - builder.settings_["collectComments"] = false; + builder["collectComments"] = false; Value value; std::string errs; bool ok = parseFromStream(builder, std::cin, &value, &errs); @@ -337,6 +337,11 @@ public: * otherwise, indicate bad settings via 'invalid'. */ 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_. * \pre 'settings' != NULL (but Json::null is fine) * \remark Defaults: diff --git a/include/json/writer.h b/include/json/writer.h index 36ed4b9..4c7a8bd 100644 --- a/include/json/writer.h +++ b/include/json/writer.h @@ -119,6 +119,10 @@ public: * otherwise, indicate bad settings via 'invalid'. */ 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_. * \pre 'settings' != NULL (but Json::null is fine) * \remark Defaults: diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index 90b4fdb..b04b60a 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -1927,6 +1927,10 @@ bool CharReaderBuilder::validate(Json::Value* invalid) const } return 0u == inv.size(); } +Value& CharReaderBuilder::operator[](std::string key) +{ + return settings_[key]; +} // static void CharReaderBuilder::strictMode(Json::Value* settings) { diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index 9dbe53e..69323df 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -1122,6 +1122,10 @@ bool StreamWriterBuilder::validate(Json::Value* invalid) const } return 0u == inv.size(); } +Value& StreamWriterBuilder::operator[](std::string key) +{ + return settings_[key]; +} // static void StreamWriterBuilder::setDefaults(Json::Value* settings) { diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 3a32519..69ecae3 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -2182,7 +2182,7 @@ JSONTEST_FIXTURE(BuilderTest, settings) { Json::CharReaderBuilder rb; JSONTEST_ASSERT_EQUAL(false, rb.settings_.isMember("foo")); 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(false, rb.validate(&errs)); } @@ -2191,7 +2191,7 @@ JSONTEST_FIXTURE(BuilderTest, settings) { Json::StreamWriterBuilder wb; JSONTEST_ASSERT_EQUAL(false, wb.settings_.isMember("foo")); 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(false, wb.validate(&errs)); } From 37dde9d29d5e102af74ec2c7dd608938d93026cc Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Wed, 4 Mar 2015 15:04:19 -0600 Subject: [PATCH 2/2] fix example in docs --- doc/jsoncpp.dox | 4 ++-- include/json/writer.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/jsoncpp.dox b/doc/jsoncpp.dox index 639c909..47efc8a 100644 --- a/doc/jsoncpp.dox +++ b/doc/jsoncpp.dox @@ -96,13 +96,13 @@ features without losing binary-compatibility. \code // For convenience, use `writeString()` with a specialized builder. Json::StreamWriterBuilder wbuilder; -wbuilder.settings_["indentation"] = "\t"; // simple Json::Value +wbuilder["indentation"] = "\t"; std::string document = Json::writeString(wbuilder, root); // Here, using a specialized Builder, we discard comments and // record errors as we parse. Json::CharReaderBuilder rbuilder; -rbuilder.settings_["collectComments"] = false; // simple Json::Value +rbuilder["collectComments"] = false; std::string errs; bool ok = Json::parseFromStream(rbuilder, std::cin, &root, &errs); \endcode diff --git a/include/json/writer.h b/include/json/writer.h index 4c7a8bd..ed21c9b 100644 --- a/include/json/writer.h +++ b/include/json/writer.h @@ -76,8 +76,8 @@ Usage: using namespace Json; Value value = ...; StreamWriterBuilder builder; - builder.settings_["commentStyle"] = "None"; - builder.settings_["indentation"] = " "; // or whatever you like + builder["commentStyle"] = "None"; + builder["indentation"] = " "; // or whatever you like std::unique_ptr writer( builder.newStreamWriter()); writer->write(value, &std::cout);