summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@linux.intel.com>2009-05-17 19:44:41 +0100
committerEmmanuele Bassi <ebassi@linux.intel.com>2009-05-17 19:44:41 +0100
commit930fdf4c4dff1f5310a946c2a9f5b6860f7c8ba8 (patch)
treeea8fbd8f41fce7bbb41069817630d5af90ed349d
parent3057a1722e27a13b39ddec4754fb6abda1aea199 (diff)
downloadjson-glib-930fdf4c4dff1f5310a946c2a9f5b6860f7c8ba8.tar.gz
Add JsonArray iteration function
Similarly to commit 3057a172 for JsonObject, the newly added json_array_foreach_element() iterates over a JSON array data type.
-rw-r--r--doc/reference/json-glib-sections.txt2
-rw-r--r--json-glib/json-array.c35
-rw-r--r--json-glib/json-types.h21
-rw-r--r--json-glib/tests/array-test.c48
4 files changed, 106 insertions, 0 deletions
diff --git a/doc/reference/json-glib-sections.txt b/doc/reference/json-glib-sections.txt
index 368af77..6872eaa 100644
--- a/doc/reference/json-glib-sections.txt
+++ b/doc/reference/json-glib-sections.txt
@@ -56,6 +56,8 @@ json_array_dup_element
json_array_get_elements
json_array_get_length
json_array_remove_element
+JsonArrayForeach
+json_array_foreach_element
<SUBSECTION>
json_array_add_array_element
diff --git a/json-glib/json-array.c b/json-glib/json-array.c
index 13ac3b8..301e050 100644
--- a/json-glib/json-array.c
+++ b/json-glib/json-array.c
@@ -667,3 +667,38 @@ json_array_remove_element (JsonArray *array,
json_node_free (g_ptr_array_remove_index (array->elements, index_));
}
+
+/**
+ * json_array_foreach_element:
+ * @array: a #JsonArray
+ * @func: the function to be called on each element
+ * @data: data to be passed to the function
+ *
+ * Iterates over all elements of @array and calls @func on
+ * each one of them.
+ *
+ * It is safe to change the value of a #JsonNode of the @array
+ * from within the iterator @func, but it is not safe to add or
+ * remove elements from the @array.
+ *
+ * Since: 0.8
+ */
+void
+json_array_foreach_element (JsonArray *array,
+ JsonArrayForeach func,
+ gpointer data)
+{
+ gint i;
+
+ g_return_if_fail (array != NULL);
+ g_return_if_fail (func != NULL);
+
+ for (i = 0; i < array->elements->len; i++)
+ {
+ JsonNode *element_node;
+
+ element_node = g_ptr_array_index (array->elements, i);
+
+ (* func) (array, i, element_node, data);
+ }
+}
diff --git a/json-glib/json-types.h b/json-glib/json-types.h
index da5a268..f797c05 100644
--- a/json-glib/json-types.h
+++ b/json-glib/json-types.h
@@ -89,6 +89,24 @@ typedef void (* JsonObjectForeach) (JsonObject *object,
gpointer user_data);
/**
+ * JsonArrayForeach:
+ * @array: the iterated #JsonArray
+ * @index_: the index of the element
+ * @element_node: a #JsonNode containing the value at @index_
+ * @user_data: data passed to the function
+ *
+ * The function to be passed to json_array_foreach_element(). You
+ * should not add or remove elements to and from @array within
+ * this function. It is safe to change the value of @element_node.
+ *
+ * Since: 0.8
+ */
+typedef void (* JsonArrayForeach) (JsonArray *array,
+ guint index_,
+ JsonNode *element_node,
+ gpointer user_data);
+
+/**
* JsonNode:
* @type: the type of node
*
@@ -261,6 +279,9 @@ JsonNode * json_array_dup_element (JsonArray *array,
void json_array_remove_element (JsonArray *array,
guint index_);
guint json_array_get_length (JsonArray *array);
+void json_array_foreach_element (JsonArray *array,
+ JsonArrayForeach func,
+ gpointer data);
G_END_DECLS
diff --git a/json-glib/tests/array-test.c b/json-glib/tests/array-test.c
index f091ecd..3d3bf20 100644
--- a/json-glib/tests/array-test.c
+++ b/json-glib/tests/array-test.c
@@ -47,6 +47,53 @@ test_remove_element (void)
json_array_unref (array);
}
+typedef struct _TestForeachFixture
+{
+ gint n_elements;
+} TestForeachFixture;
+
+static const struct {
+ JsonNodeType element_type;
+ GType element_gtype;
+} type_verify[] = {
+ { JSON_NODE_VALUE, G_TYPE_INT },
+ { JSON_NODE_VALUE, G_TYPE_BOOLEAN },
+ { JSON_NODE_VALUE, G_TYPE_STRING },
+ { JSON_NODE_NULL, G_TYPE_INVALID }
+};
+
+static void
+verify_foreach (JsonArray *array,
+ guint index_,
+ JsonNode *element_node,
+ gpointer user_data)
+{
+ TestForeachFixture *fixture = user_data;
+
+ g_assert (json_node_get_node_type (element_node) == type_verify[index_].element_type);
+ g_assert (json_node_get_value_type (element_node) == type_verify[index_].element_gtype);
+
+ fixture->n_elements += 1;
+}
+
+static void
+test_foreach_element (void)
+{
+ JsonArray *array = json_array_new ();
+ TestForeachFixture fixture = { 0, };
+
+ json_array_add_int_element (array, 42);
+ json_array_add_boolean_element (array, TRUE);
+ json_array_add_string_element (array, "hello");
+ json_array_add_null_element (array);
+
+ json_array_foreach_element (array, verify_foreach, &fixture);
+
+ g_assert_cmpint (fixture.n_elements, ==, json_array_get_length (array));
+
+ json_array_unref (array);
+}
+
int
main (int argc,
char *argv[])
@@ -57,6 +104,7 @@ main (int argc,
g_test_add_func ("/array/empty-array", test_empty_array);
g_test_add_func ("/array/add-element", test_add_element);
g_test_add_func ("/array/remove-element", test_remove_element);
+ g_test_add_func ("/array/foreach-element", test_foreach_element);
return g_test_run ();
}