Skip to content

Allow Json::Value to be used in a boolean context #695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 5, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ Json::Value obj_value(Json::objectValue); // {}
/// otherwise, false.
bool empty() const;

/// Return isNull()
bool operator!() const;
/// Return !isNull()
operator bool() const;

/// Remove all object members and array elements.
/// \pre type() is arrayValue, objectValue, or nullValue
Expand Down
2 changes: 1 addition & 1 deletion src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ bool Value::empty() const {
return false;
}

bool Value::operator!() const { return isNull(); }
Value::operator bool() const { return ! isNull(); }

void Value::clear() {
JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == arrayValue ||
Expand Down
6 changes: 6 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down