[json-glib] Allow NULL as a value for strings, arrays and objects



commit 3f8c8f99126dc1a70d847eded13afd4b64395250
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Wed Jun 16 11:54:55 2010 +0100

    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'.

 json-glib/json-array.c        |   42 ++++++++++++++++++++++++++++++--------
 json-glib/json-object.c       |   45 ++++++++++++++++++++++++++++++++--------
 json-glib/tests/object-test.c |    4 +++
 3 files changed, 73 insertions(+), 18 deletions(-)
---
diff --git a/json-glib/json-array.c b/json-glib/json-array.c
index 5149aad..6e0dada 100644
--- a/json-glib/json-array.c
+++ b/json-glib/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/json-glib/json-object.c b/json-glib/json-object.c
index 1490d7b..6948f67 100644
--- a/json-glib/json-object.c
+++ b/json-glib/json-object.c
@@ -313,8 +313,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);
 }
 
@@ -368,8 +374,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);
 }
 
@@ -398,8 +410,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);
 }
 
@@ -654,7 +672,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);
 }
@@ -684,7 +705,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);
 }
@@ -714,7 +738,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);
 }
diff --git a/json-glib/tests/object-test.c b/json-glib/tests/object-test.c
index 3d7b4d1..7f5b092 100644
--- a/json-glib/tests/object-test.c
+++ b/json-glib/tests/object-test.c
@@ -112,6 +112,10 @@ test_empty_member (void)
   g_assert (json_object_has_member (object, "string"));
   g_assert_cmpstr (json_object_get_string_member (object, "string"), ==, "");
 
+  json_object_set_string_member (object, "null", NULL);
+  g_assert (json_object_has_member (object, "null"));
+  g_assert (json_object_get_string_member (object, "null") == NULL);
+
   json_object_unref (object);
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]