Skip to content

Commit 9079422

Browse files
wolframroeslercdunn2001
authored andcommitted
Allow Json::Value to be used in a boolean context (open-source-parsers#695)
Must bump soversion too.
1 parent c39aa29 commit 9079422

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

include/json/value.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ Json::Value obj_value(Json::objectValue); // {}
400400
/// otherwise, false.
401401
bool empty() const;
402402

403-
/// Return isNull()
404-
bool operator!() const;
403+
/// Return !isNull()
404+
explicit operator bool() const;
405405

406406
/// Remove all object members and array elements.
407407
/// \pre type() is arrayValue, objectValue, or nullValue

src/lib_json/json_value.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ bool Value::empty() const {
962962
return false;
963963
}
964964

965-
bool Value::operator!() const { return isNull(); }
965+
Value::operator bool() const { return ! isNull(); }
966966

967967
void Value::clear() {
968968
JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == arrayValue ||

src/test_lib_json/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,12 @@ JSONTEST_FIXTURE(ValueTest, null) {
308308
JSONTEST_ASSERT_STRING_EQUAL("", null_.asString());
309309

310310
JSONTEST_ASSERT_EQUAL(Json::Value::null, null_);
311+
312+
// Test using a Value in a boolean context (false iff null)
313+
JSONTEST_ASSERT_EQUAL(null_,false);
314+
JSONTEST_ASSERT_EQUAL(object1_,true);
315+
JSONTEST_ASSERT_EQUAL(!null_,true);
316+
JSONTEST_ASSERT_EQUAL(!object1_,false);
311317
}
312318

313319
JSONTEST_FIXTURE(ValueTest, strings) {

0 commit comments

Comments
 (0)