From 7819e630b8fd88d269dd75a2e0fb1aeb294aed96 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Sun, 15 Jul 2012 13:24:03 +0100 Subject: 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 --- json-glib/json-node.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'json-glib/json-node.c') 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; } -- cgit v1.2.1