From 7530b5b03034e5a50e0712ab3907ae94cfc1fe0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfram=20R=C3=B6sler?= Date: Thu, 26 Oct 2017 11:15:34 +0200 Subject: [PATCH 1/3] Allow Json::Value to be used in a boolean context In a boolean context, a Value is "false" if it is null, and "true" if it is not null. This was already implemented partially by operator! and is now supported fully by adding implicit conversion of Value to bool (through "operator bool"). For example: Json::Value v; ... // We already had this before (through operator!): if (!v) { // Executed if v is null } // This commit adds the following (through the new operator bool): if (v) { // Executed if v is not null } Includes test coverage. --- include/json/value.h | 3 +++ src/lib_json/json_value.cpp | 2 ++ src/test_lib_json/main.cpp | 6 ++++++ 3 files changed, 11 insertions(+) diff --git a/include/json/value.h b/include/json/value.h index 2e533142f..4c341a1a7 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -403,6 +403,9 @@ Json::Value obj_value(Json::objectValue); // {} /// Return isNull() bool operator!() const; + /// Return !isNull() + operator bool() const; + /// Remove all object members and array elements. /// \pre type() is arrayValue, objectValue, or nullValue /// \post type() is unchanged diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 5197d1861..6605c303c 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -964,6 +964,8 @@ bool Value::empty() const { bool Value::operator!() const { return isNull(); } +Value::operator bool() const { return ! isNull(); } + void Value::clear() { JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == arrayValue || type_ == objectValue, diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 26e01cd05..2e5c76b0e 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -304,6 +304,12 @@ JSONTEST_FIXTURE(ValueTest, null) { JSONTEST_ASSERT_STRING_EQUAL("", null_.asString()); JSONTEST_ASSERT_EQUAL(Json::Value::null, null_); + + // Test using a Value in a boolean context (false iff null) + JSONTEST_ASSERT_EQUAL(null_,false); + JSONTEST_ASSERT_EQUAL(object1_,true); + JSONTEST_ASSERT_EQUAL(!null_,true); + JSONTEST_ASSERT_EQUAL(!object1_,false); } JSONTEST_FIXTURE(ValueTest, strings) { From 525f74ceca2d2a100515e39a2c15056ff0019730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfram=20R=C3=B6sler?= Date: Fri, 17 Nov 2017 09:08:35 +0100 Subject: [PATCH 2/3] Remove Value::operator! It's no longer needed since we now have operator bool which gives us operator! for free. --- include/json/value.h | 3 --- src/lib_json/json_value.cpp | 2 -- 2 files changed, 5 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index 4c341a1a7..349ed45af 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -400,9 +400,6 @@ Json::Value obj_value(Json::objectValue); // {} /// otherwise, false. bool empty() const; - /// Return isNull() - bool operator!() const; - /// Return !isNull() operator bool() const; diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 6605c303c..91d4802e6 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -962,8 +962,6 @@ bool Value::empty() const { return false; } -bool Value::operator!() const { return isNull(); } - Value::operator bool() const { return ! isNull(); } void Value::clear() { From e6f56e962d37ae9f1733197cb67a0e0b353632f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfram=20R=C3=B6sler?= Date: Tue, 5 Dec 2017 10:58:09 +0100 Subject: [PATCH 3/3] Mark operator bool as explicit We want to do things like: Json::Value v; ... if (v) ...; if (!v) ...; but not bool b = v; so the Value-to-bool conversion operator has to be marked `explicit`. --- include/json/value.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/json/value.h b/include/json/value.h index 349ed45af..535f59f7c 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -401,7 +401,7 @@ Json::Value obj_value(Json::objectValue); // {} bool empty() const; /// Return !isNull() - operator bool() const; + explicit operator bool() const; /// Remove all object members and array elements. /// \pre type() is arrayValue, objectValue, or nullValue