[json-glib] node: Implicitly convert numeric types
- From: Emmanuele Bassi <ebassi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [json-glib] node: Implicitly convert numeric types
- Date: Sun, 15 Jul 2012 12:41:06 +0000 (UTC)
commit 7819e630b8fd88d269dd75a2e0fb1aeb294aed96
Author: Emmanuele Bassi <ebassi gnome org>
Date: Sun Jul 15 13:24:03 2012 +0100
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 ++++++++++++++++++
json-glib/tests/node.c | 35 +++++++++++++++++++++++++++++++++++
2 files changed, 53 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;
}
diff --git a/json-glib/tests/node.c b/json-glib/tests/node.c
index a50d980..51dc3ff 100644
--- a/json-glib/tests/node.c
+++ b/json-glib/tests/node.c
@@ -113,6 +113,39 @@ test_null (void)
}
static void
+test_get_int (void)
+{
+ JsonNode *node = json_node_new (JSON_NODE_VALUE);
+
+ json_node_set_int (node, 0);
+ g_assert_cmpint (json_node_get_int (node), ==, 0);
+ g_assert_cmpfloat (json_node_get_double (node), ==, 0.0);
+ g_assert (!json_node_get_boolean (node));
+ g_assert (!json_node_is_null (node));
+
+ json_node_set_int (node, 42);
+ g_assert_cmpint (json_node_get_int (node), ==, 42);
+ g_assert_cmpfloat (json_node_get_double (node), ==, 42.0);
+ g_assert (json_node_get_boolean (node));
+ g_assert (!json_node_is_null (node));
+
+ json_node_free (node);
+}
+
+static void
+test_get_double (void)
+{
+ JsonNode *node = json_node_new (JSON_NODE_VALUE);
+
+ json_node_set_double (node, 3.14);
+ g_assert_cmpfloat (json_node_get_double (node), ==, 3.14);
+ g_assert_cmpint (json_node_get_int (node), ==, 3);
+ g_assert (json_node_get_boolean (node));
+
+ json_node_free (node);
+}
+
+static void
test_gvalue (void)
{
JsonNode *node = json_node_new (JSON_NODE_VALUE);
@@ -224,6 +257,8 @@ main (int argc,
g_test_add_func ("/nodes/copy/null", test_copy_null);
g_test_add_func ("/nodes/copy/value", test_copy_value);
g_test_add_func ("/nodes/copy/object", test_copy_object);
+ g_test_add_func ("/nodes/get/int", test_get_int);
+ g_test_add_func ("/nodes/get/double", test_get_double);
g_test_add_func ("/nodes/gvalue", test_gvalue);
g_test_add_func ("/nodes/gvalue/autopromotion", test_gvalue_autopromotion);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]