Wizix Wizix - 6 months ago 10
JSON Question

Check if object is a boolean

I need to check if my Json.Object is a boolean or not. This is what I've done:

var media = root.get_member ("media");
if (!media.get_value_type ().is_a (bool)) {
this.media.set ("mp4", media.get_object ().get_string_member ("mp4"));
this.media.set ("webm", media.get_object ().get_string_member ("webm"));
}


The JSON looks like this:

"media": {
"mp4": "http:\/\/img-9gag-fun.9cache.com\/photo\/EyVtjpq_460sv.mp4",
"webm": "http:\/\/img-9gag-fun.9cache.com\/photo\/EyVtjpq_460svwm.webm"
},


Or like this if it's a boolean:

"media": false,


But this condition is not working
!media.get_value_type ().is_a (bool)
, the method
is_a ()
can't take
bool
apparently. How can I check if my value is a boolean or not?

Answer

Should be this:

if (media.get_value_type () != typeof(bool))

You can use typeof () to get the GType of any data type in Vala and GType is what the function returns.

https://wiki.gnome.org/Projects/Vala/FAQ#How_do_I_get_the_GType_for_something.3F

https://developer.gnome.org/json-glib/stable/json-glib-JSON-Node.html#json-node-get-value-type

In C you would probably use G_TYPE_BOOLEAN:

https://developer.gnome.org/gobject/stable/gobject-Type-Information.html#G-TYPE-BOOLEAN:CAPS