Handle empty strings in Json::Value::empty()

This commit is contained in:
Tom Ritchford
2015-06-18 15:31:20 -04:00
committed by Nik Bougalis
parent e4f585b7fe
commit a5a9242f4e
2 changed files with 33 additions and 4 deletions

View File

@@ -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 ();
}

View File

@@ -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 ();