Skip to content

Commit 21b6309

Browse files
committed
explicit operator bool() for Json::Value
More convenient c++11 way to work with null Json::Value objects then isNull() or operator! http://en.wikibooks.org/wiki/More_C++_Idioms/Safe_bool#C.2B.2B11
1 parent cbe7e7c commit 21b6309

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

include/json/value.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,23 @@ Json::Value obj_value(Json::objectValue); // {}
347347
/// Return isNull()
348348
bool operator!() const;
349349

350+
/** \brief Return ! isNull();
351+
*
352+
* Example of usage:
353+
* \code
354+
* Json::Value root;
355+
* std::cin >> root;
356+
*
357+
* if (auto tag = root["tag"]) {
358+
* // Behavior if tag object with tag key exist
359+
* } else {
360+
* // Behavior if tag object with tag key absent
361+
* }
362+
* \endcode
363+
*
364+
*/
365+
explicit operator bool() const;
366+
350367
/// Remove all object members and array elements.
351368
/// \pre type() is arrayValue, objectValue, or nullValue
352369
/// \post type() is unchanged

src/lib_json/json_value.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,8 @@ bool Value::empty() const {
883883

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

886+
Value::operator bool() const { return ! isNull(); }
887+
886888
void Value::clear() {
887889
JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == arrayValue ||
888890
type_ == objectValue,

0 commit comments

Comments
 (0)