diff --git a/src/ripple/json/impl/json_value.cpp b/src/ripple/json/impl/json_value.cpp index b3d6f160b2..cb22da2ac3 100644 --- a/src/ripple/json/impl/json_value.cpp +++ b/src/ripple/json/impl/json_value.cpp @@ -825,10 +825,15 @@ Value::size () const bool Value::empty () const { - if ( isNull () || isArray () || isObject () ) - return size () == 0u; - else - return false; + if (isNull ()) + return true; + + if (isString ()) { + auto s = asCString(); + return !(s && strlen(s)); + } + + return (isArray () || isObject ()) && ! size (); } diff --git a/src/ripple/json/tests/json_value.test.cpp b/src/ripple/json/tests/json_value.test.cpp index 3f21f4f507..83c7cdece3 100644 --- a/src/ripple/json/tests/json_value.test.cpp +++ b/src/ripple/json/tests/json_value.test.cpp @@ -28,6 +28,29 @@ namespace ripple { class json_value_test : public beast::unit_test::suite { public: + void test_empty() + { + expect (Json::Value().empty()); + + expect (Json::Value("").empty()); + + expect (! Json::Value("empty").empty()); + expect (! Json::Value(false).empty()); + expect (! Json::Value(true).empty()); + expect (! Json::Value(0).empty()); + expect (! Json::Value(1).empty()); + + Json::Value array (Json::arrayValue); + expect (array.empty()); + array.append(0); + expect (!array.empty()); + + Json::Value object (Json::objectValue); + expect (object.empty()); + object[""] = false; + expect (!object.empty()); + } + void test_bad_json () { char const* s ( @@ -197,6 +220,7 @@ public: void run () { + test_empty (); test_bad_json (); test_edge_cases (); test_copy ();