diff options
author | Emmanuele Bassi <ebassi@openedhand.com> | 2007-10-05 18:24:27 +0100 |
---|---|---|
committer | Emmanuele Bassi <ebassi@openedhand.com> | 2007-10-05 18:24:27 +0100 |
commit | bd5a60ca658257752993ccea47950b97cdc45246 (patch) | |
tree | 363e12a30b6af1e9c2c232ef96b9d618a2368592 /json-glib/json-node.c | |
parent | ad95c8bf8e4103058d42ae71f47e6980e3b52c34 (diff) | |
download | json-glib-bd5a60ca658257752993ccea47950b97cdc45246.tar.gz |
Add convenience accessors for fundamental types in JsonNode
This commit adds some convenience accessors for setting and
getting fundamental types in and from a JsonNode, to avoid
jumping through all the GValue hoops.
Diffstat (limited to 'json-glib/json-node.c')
-rw-r--r-- | json-glib/json-node.c | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/json-glib/json-node.c b/json-glib/json-node.c index 3935782..8e7d5ad 100644 --- a/json-glib/json-node.c +++ b/json-glib/json-node.c @@ -396,3 +396,204 @@ json_node_get_parent (JsonNode *node) return node->parent; } + +/** + * json_node_set_string: + * @node: a #JsonNode of type %JSON_NODE_VALUE + * @value: a string value + * + * Sets @value as the string content of the @node, replacing any existing + * content. + */ +void +json_node_set_string (JsonNode *node, + const gchar *value) +{ + g_return_if_fail (node != NULL); + g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE); + + if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_STRING) + g_value_set_string (&(node->data.value), value); + else + { + GValue copy = { 0, }; + + g_value_init (©, G_TYPE_STRING); + g_value_set_string (©, value); + + json_node_set_value (node, ©); + + g_value_unset (©); + } +} + +/** + * json_node_get_string: + * @node: a #JsonNode of type %JSON_NODE_VALUE + * + * Gets the string value stored inside a #JsonNode + * + * Return value: a string value. + */ +G_CONST_RETURN gchar * +json_node_get_string (JsonNode *node) +{ + g_return_val_if_fail (node != NULL, NULL); + g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, NULL); + + if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_STRING) + return g_value_get_string (&(node->data.value)); + + return NULL; +} + +/** + * json_node_set_int: + * @node: a #JsonNode of type %JSON_NODE_VALUE + * @value: an integer value + * + * Sets @value as the integer content of the @node, replacing any existing + * content. + */ +void +json_node_set_int (JsonNode *node, + gint value) +{ + g_return_if_fail (node != NULL); + g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE); + + if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_INT) + g_value_set_int (&(node->data.value), value); + else + { + GValue copy = { 0, }; + + g_value_init (©, G_TYPE_INT); + g_value_set_int (©, value); + + json_node_set_value (node, ©); + + g_value_unset (©); + } +} + +/** + * json_node_get_int: + * @node: a #JsonNode of type %JSON_NODE_VALUE + * + * Gets the integer value stored inside a #JsonNode + * + * Return value: an integer value. + */ +gint +json_node_get_int (JsonNode *node) +{ + g_return_val_if_fail (node != NULL, 0); + g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, 0); + + if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_INT) + return g_value_get_int (&(node->data.value)); + + return 0; +} + +/** + * json_node_set_double: + * @node: a #JsonNode of type %JSON_NODE_VALUE + * @value: a double value + * + * Sets @value as the double content of the @node, replacing any existing + * content. + */ +void +json_node_set_double (JsonNode *node, + gdouble value) +{ + g_return_if_fail (node != NULL); + g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE); + + if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_DOUBLE) + g_value_set_double (&(node->data.value), value); + else + { + GValue copy = { 0, }; + + g_value_init (©, G_TYPE_DOUBLE); + g_value_set_double (©, value); + + json_node_set_value (node, ©); + + g_value_unset (©); + } +} + +/** + * json_node_get_double: + * @node: a #JsonNode of type %JSON_NODE_VALUE + * + * Gets the double value stored inside a #JsonNode + * + * Return value: a double value. + */ +gdouble +json_node_get_double (JsonNode *node) +{ + g_return_val_if_fail (node != NULL, 0.0); + g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, 0.0); + + if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_DOUBLE) + return g_value_get_double (&(node->data.value)); + + return 0.0; +} + +/** + * json_node_set_boolean: + * @node: a #JsonNode of type %JSON_NODE_VALUE + * @value: a boolean value + * + * Sets @value as the boolean content of the @node, replacing any existing + * content. + */ +void +json_node_set_boolean (JsonNode *node, + gboolean value) +{ + g_return_if_fail (node != NULL); + g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE); + + if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_BOOLEAN) + g_value_set_boolean (&(node->data.value), value); + else + { + GValue copy = { 0, }; + + g_value_init (©, G_TYPE_BOOLEAN); + g_value_set_boolean (©, value); + + json_node_set_value (node, ©); + + g_value_unset (©); + } +} + +/** + * json_node_get_boolean: + * @node: a #JsonNode of type %JSON_NODE_VALUE + * + * Gets the boolean value stored inside a #JsonNode + * + * Return value: a boolean value. + */ +gboolean +json_node_get_boolean (JsonNode *node) +{ + g_return_val_if_fail (node != NULL, FALSE); + g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, FALSE); + + if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_BOOLEAN) + return g_value_get_boolean (&(node->data.value)); + + return FALSE; +} + |