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.
This commit is contained in:
Emmanuele Bassi 2010-09-25 20:23:41 +01:00
parent 8fe9f3a37d
commit fda1f3a8ba
2 changed files with 69 additions and 18 deletions

View File

@ -343,7 +343,10 @@ json_array_get_string_element (JsonArray *array,
node = g_ptr_array_index (array->elements, index_); node = g_ptr_array_index (array->elements, index_);
g_return_val_if_fail (node != NULL, NULL); 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); 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_); node = g_ptr_array_index (array->elements, index_);
g_return_val_if_fail (node != NULL, NULL); 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); 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_); node = g_ptr_array_index (array->elements, index_);
g_return_val_if_fail (node != NULL, NULL); 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); 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 (array != NULL);
g_return_if_fail (value != NULL); g_return_if_fail (value != NULL);
node = json_node_new (JSON_NODE_VALUE); if (value != NULL)
json_node_set_string (node, value); {
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); 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 (array != NULL);
g_return_if_fail (value != NULL); g_return_if_fail (value != NULL);
node = json_node_new (JSON_NODE_ARRAY); if (value != NULL)
json_node_take_array (node, value); {
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); 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 (array != NULL);
g_return_if_fail (value != NULL); g_return_if_fail (value != NULL);
node = json_node_new (JSON_NODE_OBJECT); if (value != NULL)
json_node_take_object (node, value); {
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); g_ptr_array_add (array->elements, node);
} }

View File

@ -311,8 +311,14 @@ json_object_set_string_member (JsonObject *object,
g_return_if_fail (object != NULL); g_return_if_fail (object != NULL);
g_return_if_fail (member_name != NULL); g_return_if_fail (member_name != NULL);
node = json_node_new (JSON_NODE_VALUE); if (value != NULL)
json_node_set_string (node, value); {
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); 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 (object != NULL);
g_return_if_fail (member_name != NULL); g_return_if_fail (member_name != NULL);
node = json_node_new (JSON_NODE_ARRAY); if (value != NULL)
json_node_take_array (node, value); {
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); 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 (object != NULL);
g_return_if_fail (member_name != NULL); g_return_if_fail (member_name != NULL);
node = json_node_new (JSON_NODE_OBJECT); if (value != NULL)
json_node_take_object (node, value); {
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); 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); node = object_get_member_internal (object, member_name);
g_return_val_if_fail (node != NULL, NULL); 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); 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); node = object_get_member_internal (object, member_name);
g_return_val_if_fail (node != NULL, NULL); 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); 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); node = object_get_member_internal (object, member_name);
g_return_val_if_fail (node != NULL, NULL); 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); return json_node_get_object (node);
} }