RejectedSoftware Forums

Sign up

std_data_json and null values

I'm not sure of the best place to ask this question, but since stddatajson is related to vibe.d, I guess that I'll ask it here. But what is the correct way to check for null values in JSONValue? I can do is null or == null on the JSONValue, but that only works if the value is actually null or it's a type that converts to null. So, for instance, if I have a key-value pair in a JSON document where the value's type is supposed a bool, but it could be null, doing something like

auto value = key in json;
enforce(key !is null, "error msg");
if(*value is null)
{ do something }
else
{ do something else }

will work as long as the value is null but will result in an exception being thrown if the value is true or false. As far as I can tell, there is not "isNull" check or equivalent that I can do on JSONValue. So, what is the correct way to go about checking whether a JSONValue is null?

Re: std_data_json and null values

Am 24.05.2016 um 07:54 schrieb Jonathan M Davis:

I'm not sure of the best place to ask this question, but since stddatajson is related to vibe.d, I guess that I'll ask it here. But what is the correct way to check for null values in JSONValue? I can do is null or == null on the JSONValue, but that only works if the value is actually null or it's a type that converts to null. So, for instance, if I have a key-value pair in a JSON document where the value's type is supposed a bool, but it could be null, doing something like

auto value = key in json;
enforce(key !is null, "error msg");
if(*value is null)
{ do something }
else
{ do something else }

will work as long as the value is null but will result in an exception being thrown if the value is true or false. As far as I can tell, there is not "isNull" check or equivalent that I can do on JSONValue. So, what is the correct way to go about checking whether a JSONValue is null?

The generic way is to use value.kind == JSONValue.Kind.null_. But I
guess a convenience method really makes sense in this case. I'll add
isNull.

Re: std_data_json and null values

On Tue, 24 May 2016 08:11:09 +0200, Sönke Ludwig wrote:

The generic way is to use value.kind == JSONValue.Kind.null_. But I
guess a convenience method really makes sense in this case. I'll add
isNull.

Cool. Thanks! There isn't even anything in stdx.data.json.value which mentions kind or Kind, so I definitely wasn't stumbling on that. On a related note, it would be nice to have a function similar to get which returned Nullable!T for those cases where you want to operate on a Nullable!T rather than just checking for null and doing something special for null. In either case, having isNull would be a great addition.

  • Jonathan M Davis