SourceForge has been redesigned. Learn more.
Close

#34 isObject and isArray behavior is confusing

0.5.0
closed-wont-fix
nobody
Value (7)
5
2015-03-06
2011-07-18
jmar8692
No

isObject and isArray methods do also return true if the value is in fact a nullValue, which a bit strange IMHO.

Consider a common use like this:
const Json::Value& name = json["name"];
const Json::Value& age = json["age"];
const Json::Value& details = json["details"];

if (!name.isString()) // OK -> works as expected for string.
throw Error("name is unset or not in valid format");

if (!age.isNumeric()) // OK -> works as expected for number.
throw Error("age is unset or not in valid format");

if (!details.isObject()) // ERROR -> will fail to detect missing object
throw Error("details are unset or not in valid format");

if (details.type() == Json::typeObject) // OK, but this workaround is inconsistent with other types.
throw Error("details are unset or not in valid format");

It seems to me that JSON grammar does not make the null type a special case for empty object/array. In fact it considers null, object and array all distinct types.

Discussion

  • Lekensteyn

    Lekensteyn - 2012-02-25

    It's definitely an error, null is just a value like a string or number as can be seen in http://json.org/.

     
  • Christopher Dunn

    I agree, but this is an old design decision. I'd be afraid to change it now.

     
  • Christopher Dunn

    • status: open --> closed-wont-fix
     

Log in to post a comment.