From fda1f3a8ba053aaa98882788b6098acbf7875bd3 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Sat, 25 Sep 2010 20:23:41 +0100 Subject: [PATCH] json: Allow NULL as a value for strings, arrays and objects We should not warn when asking for a string, array or object if the contents were 'null'. Patch from JSON-GLib. --- clutter/json/json-array.c | 42 +++++++++++++++++++++++++++-------- clutter/json/json-object.c | 45 ++++++++++++++++++++++++++++++-------- 2 files changed, 69 insertions(+), 18 deletions(-) diff --git a/clutter/json/json-array.c b/clutter/json/json-array.c index 83e2e0245..f1e677e61 100644 --- a/clutter/json/json-array.c +++ b/clutter/json/json-array.c @@ -343,7 +343,10 @@ json_array_get_string_element (JsonArray *array, node = g_ptr_array_index (array->elements, index_); g_return_val_if_fail (node != NULL, NULL); - g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, NULL); + g_return_val_if_fail (JSON_NODE_HOLDS_VALUE (node) || JSON_NODE_HOLDS_NULL (node), NULL); + + if (JSON_NODE_HOLDS_NULL (node)) + return NULL; return json_node_get_string (node); } @@ -401,7 +404,10 @@ json_array_get_array_element (JsonArray *array, node = g_ptr_array_index (array->elements, index_); g_return_val_if_fail (node != NULL, NULL); - g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY, NULL); + g_return_val_if_fail (JSON_NODE_HOLDS_ARRAY (node) || JSON_NODE_HOLDS_NULL (node), NULL); + + if (JSON_NODE_HOLDS_NULL (node)) + return NULL; return json_node_get_array (node); } @@ -431,7 +437,10 @@ json_array_get_object_element (JsonArray *array, node = g_ptr_array_index (array->elements, index_); g_return_val_if_fail (node != NULL, NULL); - g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT, NULL); + g_return_val_if_fail (JSON_NODE_HOLDS_OBJECT (node) || JSON_NODE_HOLDS_NULL (node), NULL); + + if (JSON_NODE_HOLDS_NULL (node)) + return NULL; return json_node_get_object (node); } @@ -565,8 +574,13 @@ json_array_add_string_element (JsonArray *array, g_return_if_fail (array != NULL); g_return_if_fail (value != NULL); - node = json_node_new (JSON_NODE_VALUE); - json_node_set_string (node, value); + if (value != NULL) + { + node = json_node_new (JSON_NODE_VALUE); + json_node_set_string (node, value); + } + else + node = json_node_new (JSON_NODE_NULL); g_ptr_array_add (array->elements, node); } @@ -614,8 +628,13 @@ json_array_add_array_element (JsonArray *array, g_return_if_fail (array != NULL); g_return_if_fail (value != NULL); - node = json_node_new (JSON_NODE_ARRAY); - json_node_take_array (node, value); + if (value != NULL) + { + node = json_node_new (JSON_NODE_ARRAY); + json_node_take_array (node, value); + } + else + node = json_node_new (JSON_NODE_NULL); g_ptr_array_add (array->elements, node); } @@ -641,8 +660,13 @@ json_array_add_object_element (JsonArray *array, g_return_if_fail (array != NULL); g_return_if_fail (value != NULL); - node = json_node_new (JSON_NODE_OBJECT); - json_node_take_object (node, value); + if (value != NULL) + { + node = json_node_new (JSON_NODE_OBJECT); + json_node_take_object (node, value); + } + else + node = json_node_new (JSON_NODE_NULL); g_ptr_array_add (array->elements, node); } diff --git a/clutter/json/json-object.c b/clutter/json/json-object.c index f3324976b..b60b37b99 100644 --- a/clutter/json/json-object.c +++ b/clutter/json/json-object.c @@ -311,8 +311,14 @@ json_object_set_string_member (JsonObject *object, g_return_if_fail (object != NULL); g_return_if_fail (member_name != NULL); - node = json_node_new (JSON_NODE_VALUE); - json_node_set_string (node, value); + if (value != NULL) + { + node = json_node_new (JSON_NODE_VALUE); + json_node_set_string (node, value); + } + else + node = json_node_new (JSON_NODE_NULL); + object_set_member_internal (object, member_name, node); } @@ -366,8 +372,14 @@ json_object_set_array_member (JsonObject *object, g_return_if_fail (object != NULL); g_return_if_fail (member_name != NULL); - node = json_node_new (JSON_NODE_ARRAY); - json_node_take_array (node, value); + if (value != NULL) + { + node = json_node_new (JSON_NODE_ARRAY); + json_node_take_array (node, value); + } + else + node = json_node_new (JSON_NODE_NULL); + object_set_member_internal (object, member_name, node); } @@ -396,8 +408,14 @@ json_object_set_object_member (JsonObject *object, g_return_if_fail (object != NULL); g_return_if_fail (member_name != NULL); - node = json_node_new (JSON_NODE_OBJECT); - json_node_take_object (node, value); + if (value != NULL) + { + node = json_node_new (JSON_NODE_OBJECT); + json_node_take_object (node, value); + } + else + node = json_node_new (JSON_NODE_NULL); + object_set_member_internal (object, member_name, node); } @@ -688,7 +706,10 @@ json_object_get_string_member (JsonObject *object, node = object_get_member_internal (object, member_name); g_return_val_if_fail (node != NULL, NULL); - g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, NULL); + g_return_val_if_fail (JSON_NODE_HOLDS_VALUE (node) || JSON_NODE_HOLDS_NULL (node), NULL); + + if (JSON_NODE_HOLDS_NULL (node)) + return NULL; return json_node_get_string (node); } @@ -718,7 +739,10 @@ json_object_get_array_member (JsonObject *object, node = object_get_member_internal (object, member_name); g_return_val_if_fail (node != NULL, NULL); - g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY, NULL); + g_return_val_if_fail (JSON_NODE_HOLDS_ARRAY (node) || JSON_NODE_HOLDS_NULL (node), NULL); + + if (JSON_NODE_HOLDS_NULL (node)) + return NULL; return json_node_get_array (node); } @@ -748,7 +772,10 @@ json_object_get_object_member (JsonObject *object, node = object_get_member_internal (object, member_name); g_return_val_if_fail (node != NULL, NULL); - g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT, NULL); + g_return_val_if_fail (JSON_NODE_HOLDS_OBJECT (node) || JSON_NODE_HOLDS_NULL (node), NULL); + + if (JSON_NODE_HOLDS_NULL (node)) + return NULL; return json_node_get_object (node); }