diff options
author | Emmanuele Bassi <ebassi@gnome.org> | 2012-07-15 13:24:03 +0100 |
---|---|---|
committer | Emmanuele Bassi <ebassi@gnome.org> | 2012-07-15 13:40:24 +0100 |
commit | 7819e630b8fd88d269dd75a2e0fb1aeb294aed96 (patch) | |
tree | 47c1241d6d2aa33002f49d5befcf4ed16864c458 /json-glib/json-node.c | |
parent | b1b50ec3ad76955651abe92696c4f4717e353840 (diff) | |
download | json-glib-7819e630b8fd88d269dd75a2e0fb1aeb294aed96.tar.gz |
node: Implicitly convert numeric types
When retrieving an int, a double, or a boolean, we can use the C rules
of implicit conversion to move from the actual stored type inside the
JsonValue to the requested C type.
https://bugzilla.gnome.org/show_bug.cgi?id=660795
Diffstat (limited to 'json-glib/json-node.c')
-rw-r--r-- | json-glib/json-node.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/json-glib/json-node.c b/json-glib/json-node.c index 3d434f3..c51ee7f 100644 --- a/json-glib/json-node.c +++ b/json-glib/json-node.c @@ -654,6 +654,12 @@ json_node_get_int (JsonNode *node) if (JSON_VALUE_HOLDS_INT (node->data.value)) return json_value_get_int (node->data.value); + if (JSON_VALUE_HOLDS_DOUBLE (node->data.value)) + return json_value_get_double (node->data.value); + + if (JSON_VALUE_HOLDS_BOOLEAN (node->data.value)) + return json_value_get_boolean (node->data.value); + return 0; } @@ -699,6 +705,12 @@ json_node_get_double (JsonNode *node) if (JSON_VALUE_HOLDS_DOUBLE (node->data.value)) return json_value_get_double (node->data.value); + if (JSON_VALUE_HOLDS_INT (node->data.value)) + return (gdouble) json_value_get_int (node->data.value); + + if (JSON_VALUE_HOLDS_BOOLEAN (node->data.value)) + return (gdouble) json_value_get_boolean (node->data.value); + return 0.0; } @@ -744,6 +756,12 @@ json_node_get_boolean (JsonNode *node) if (JSON_VALUE_HOLDS_BOOLEAN (node->data.value)) return json_value_get_boolean (node->data.value); + if (JSON_VALUE_HOLDS_INT (node->data.value)) + return json_value_get_int (node->data.value) != 0; + + if (JSON_VALUE_HOLDS_DOUBLE (node->data.value)) + return json_value_get_double (node->data.value) != 0.0; + return FALSE; } |