1 /* json-array.c - JSON array implementation
3 * This file is part of JSON-GLib
4 * Copyright (C) 2007 OpenedHand Ltd.
5 * Copyright (C) 2009 Intel Corp.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 * Emmanuele Bassi <ebassi@linux.intel.com>
28 #include "json-types-private.h"
32 * @short_description: a JSON array representation
34 * #JsonArray is the representation of the array type inside JSON. It contains
35 * #JsonNode<!-- -->s, which may contain fundamental types, other arrays or
38 * Since arrays can be expensive, they are reference counted. You can control
39 * the lifetime of a #JsonArray using json_array_ref() and json_array_unref().
41 * To append an element, use json_array_add_element().
42 * To extract an element at a given index, use json_array_get_element().
43 * To retrieve the entire array in list form, use json_array_get_elements().
44 * To retrieve the length of the array, use json_array_get_length().
47 G_DEFINE_BOXED_TYPE (JsonArray, json_array, json_array_ref, json_array_unref);
52 * Creates a new #JsonArray.
54 * Return value: the newly created #JsonArray
61 array = g_slice_new (JsonArray);
64 array->elements = g_ptr_array_new ();
70 * json_array_sized_new:
71 * @n_elements: number of slots to pre-allocate
73 * Creates a new #JsonArray with @n_elements slots already allocated.
75 * Return value: the newly created #JsonArray
78 json_array_sized_new (guint n_elements)
82 array = g_slice_new (JsonArray);
85 array->elements = g_ptr_array_sized_new (n_elements);
92 * @array: a #JsonArray
94 * Increase by one the reference count of a #JsonArray.
96 * Return value: the passed #JsonArray, with the reference count
100 json_array_ref (JsonArray *array)
102 g_return_val_if_fail (array != NULL, NULL);
103 g_return_val_if_fail (array->ref_count > 0, NULL);
105 g_atomic_int_add (&array->ref_count, 1);
112 * @array: a #JsonArray
114 * Decreases by one the reference count of a #JsonArray. If the
115 * reference count reaches zero, the array is destroyed and all
116 * its allocated resources are freed.
119 json_array_unref (JsonArray *array)
121 g_return_if_fail (array != NULL);
122 g_return_if_fail (array->ref_count > 0);
124 if (g_atomic_int_dec_and_test (&array->ref_count))
128 for (i = 0; i < array->elements->len; i++)
129 json_node_free (g_ptr_array_index (array->elements, i));
131 g_ptr_array_free (array->elements, TRUE);
132 array->elements = NULL;
134 g_slice_free (JsonArray, array);
139 * json_array_get_elements:
140 * @array: a #JsonArray
142 * Gets the elements of a #JsonArray as a list of #JsonNode<!-- -->s.
144 * Return value: (element-type JsonNode) (transfer container): a #GList
145 * containing the elements of the array. The contents of the list are
146 * owned by the array and should never be modified or freed. Use
147 * g_list_free() on the returned list when done using it
150 json_array_get_elements (JsonArray *array)
155 g_return_val_if_fail (array != NULL, NULL);
158 for (i = 0; i < array->elements->len; i++)
159 retval = g_list_prepend (retval,
160 g_ptr_array_index (array->elements, i));
162 return g_list_reverse (retval);
166 * json_array_dup_element:
167 * @array: a #JsonArray
168 * @index_: the index of the element to retrieve
170 * Retrieves a copy of the #JsonNode containing the value of the
171 * element at @index_ inside a #JsonArray
173 * Return value: (transfer full): a copy of the #JsonNode at the requested
174 * index. Use json_node_free() when done.
179 json_array_dup_element (JsonArray *array,
184 g_return_val_if_fail (array != NULL, NULL);
185 g_return_val_if_fail (index_ < array->elements->len, NULL);
187 retval = json_array_get_element (array, index_);
191 return json_node_copy (retval);
195 * json_array_get_element:
196 * @array: a #JsonArray
197 * @index_: the index of the element to retrieve
199 * Retrieves the #JsonNode containing the value of the element at @index_
200 * inside a #JsonArray.
202 * Return value: (transfer none): a pointer to the #JsonNode at the requested index
205 json_array_get_element (JsonArray *array,
208 g_return_val_if_fail (array != NULL, NULL);
209 g_return_val_if_fail (index_ < array->elements->len, NULL);
211 return g_ptr_array_index (array->elements, index_);
215 * json_array_get_int_element:
216 * @array: a #JsonArray
217 * @index_: the index of the element to retrieve
219 * Conveniently retrieves the integer value of the element at @index_
222 * See also: json_array_get_element(), json_node_get_int()
224 * Return value: the integer value
229 json_array_get_int_element (JsonArray *array,
234 g_return_val_if_fail (array != NULL, 0);
235 g_return_val_if_fail (index_ < array->elements->len, 0);
237 node = g_ptr_array_index (array->elements, index_);
238 g_return_val_if_fail (node != NULL, 0);
239 g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, 0);
241 return json_node_get_int (node);
245 * json_array_get_double_element:
246 * @array: a #JsonArray
247 * @index_: the index of the element to retrieve
249 * Conveniently retrieves the floating point value of the element at
250 * @index_ inside @array
252 * See also: json_array_get_element(), json_node_get_double()
254 * Return value: the floating point value
259 json_array_get_double_element (JsonArray *array,
264 g_return_val_if_fail (array != NULL, 0.0);
265 g_return_val_if_fail (index_ < array->elements->len, 0.0);
267 node = g_ptr_array_index (array->elements, index_);
268 g_return_val_if_fail (node != NULL, 0.0);
269 g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, 0.0);
271 return json_node_get_double (node);
275 * json_array_get_boolean_element:
276 * @array: a #JsonArray
277 * @index_: the index of the element to retrieve
279 * Conveniently retrieves the boolean value of the element at @index_
282 * See also: json_array_get_element(), json_node_get_boolean()
284 * Return value: the integer value
289 json_array_get_boolean_element (JsonArray *array,
294 g_return_val_if_fail (array != NULL, FALSE);
295 g_return_val_if_fail (index_ < array->elements->len, FALSE);
297 node = g_ptr_array_index (array->elements, index_);
298 g_return_val_if_fail (node != NULL, FALSE);
299 g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, FALSE);
301 return json_node_get_boolean (node);
305 * json_array_get_string_element:
306 * @array: a #JsonArray
307 * @index_: the index of the element to retrieve
309 * Conveniently retrieves the string value of the element at @index_
312 * See also: json_array_get_element(), json_node_get_string()
314 * Return value: the string value; the returned string is owned by
315 * the #JsonArray and should not be modified or freed
320 json_array_get_string_element (JsonArray *array,
325 g_return_val_if_fail (array != NULL, NULL);
326 g_return_val_if_fail (index_ < array->elements->len, NULL);
328 node = g_ptr_array_index (array->elements, index_);
329 g_return_val_if_fail (node != NULL, NULL);
330 g_return_val_if_fail (JSON_NODE_HOLDS_VALUE (node) || JSON_NODE_HOLDS_NULL (node), NULL);
332 if (JSON_NODE_HOLDS_NULL (node))
335 return json_node_get_string (node);
339 * json_array_get_null_element:
340 * @array: a #JsonArray
341 * @index_: the index of the element to retrieve
343 * Conveniently retrieves whether the element at @index_ is set to null
345 * See also: json_array_get_element(), JSON_NODE_TYPE(), %JSON_NODE_NULL
347 * Return value: %TRUE if the element is null
352 json_array_get_null_element (JsonArray *array,
357 g_return_val_if_fail (array != NULL, FALSE);
358 g_return_val_if_fail (index_ < array->elements->len, FALSE);
360 node = g_ptr_array_index (array->elements, index_);
361 g_return_val_if_fail (node != NULL, FALSE);
363 return JSON_NODE_TYPE (node) == JSON_NODE_NULL;
367 * json_array_get_array_element:
368 * @array: a #JsonArray
369 * @index_: the index of the element to retrieve
371 * Conveniently retrieves the array from the element at @index_
374 * See also: json_array_get_element(), json_node_get_array()
376 * Return value: (transfer none): the array
381 json_array_get_array_element (JsonArray *array,
386 g_return_val_if_fail (array != NULL, NULL);
387 g_return_val_if_fail (index_ < array->elements->len, NULL);
389 node = g_ptr_array_index (array->elements, index_);
390 g_return_val_if_fail (node != NULL, NULL);
391 g_return_val_if_fail (JSON_NODE_HOLDS_ARRAY (node) || JSON_NODE_HOLDS_NULL (node), NULL);
393 if (JSON_NODE_HOLDS_NULL (node))
396 return json_node_get_array (node);
400 * json_array_get_object_element:
401 * @array: a #JsonArray
402 * @index_: the index of the element to retrieve
404 * Conveniently retrieves the object from the element at @index_
407 * See also: json_array_get_element(), json_node_get_object()
409 * Return value: (transfer none): the object
414 json_array_get_object_element (JsonArray *array,
419 g_return_val_if_fail (array != NULL, NULL);
420 g_return_val_if_fail (index_ < array->elements->len, NULL);
422 node = g_ptr_array_index (array->elements, index_);
423 g_return_val_if_fail (node != NULL, NULL);
424 g_return_val_if_fail (JSON_NODE_HOLDS_OBJECT (node) || JSON_NODE_HOLDS_NULL (node), NULL);
426 if (JSON_NODE_HOLDS_NULL (node))
429 return json_node_get_object (node);
433 * json_array_get_length:
434 * @array: a #JsonArray
436 * Retrieves the length of a #JsonArray
438 * Return value: the length of the array
441 json_array_get_length (JsonArray *array)
443 g_return_val_if_fail (array != NULL, 0);
445 return array->elements->len;
449 * json_array_add_element:
450 * @array: a #JsonArray
451 * @node: (transfer full): a #JsonNode
453 * Appends @node inside @array. The array will take ownership of the
457 json_array_add_element (JsonArray *array,
460 g_return_if_fail (array != NULL);
461 g_return_if_fail (node != NULL);
463 g_ptr_array_add (array->elements, node);
467 * json_array_add_int_element:
468 * @array: a #JsonArray
469 * @value: an integer value
471 * Conveniently adds an integer @value into @array
473 * See also: json_array_add_element(), json_node_set_int()
478 json_array_add_int_element (JsonArray *array,
483 g_return_if_fail (array != NULL);
485 node = json_node_new (JSON_NODE_VALUE);
486 json_node_set_int (node, value);
488 g_ptr_array_add (array->elements, node);
492 * json_array_add_double_element:
493 * @array: a #JsonArray
494 * @value: a floating point value
496 * Conveniently adds a floating point @value into @array
498 * See also: json_array_add_element(), json_node_set_double()
503 json_array_add_double_element (JsonArray *array,
508 g_return_if_fail (array != NULL);
510 node = json_node_new (JSON_NODE_VALUE);
511 json_node_set_double (node, value);
513 g_ptr_array_add (array->elements, node);
517 * json_array_add_boolean_element:
518 * @array: a #JsonArray
519 * @value: a boolean value
521 * Conveniently adds a boolean @value into @array
523 * See also: json_array_add_element(), json_node_set_boolean()
528 json_array_add_boolean_element (JsonArray *array,
533 g_return_if_fail (array != NULL);
535 node = json_node_new (JSON_NODE_VALUE);
536 json_node_set_boolean (node, value);
538 g_ptr_array_add (array->elements, node);
542 * json_array_add_string_element:
543 * @array: a #JsonArray
544 * @value: a string value
546 * Conveniently adds a string @value into @array
548 * See also: json_array_add_element(), json_node_set_string()
553 json_array_add_string_element (JsonArray *array,
558 g_return_if_fail (array != NULL);
559 g_return_if_fail (value != NULL);
563 node = json_node_new (JSON_NODE_VALUE);
564 json_node_set_string (node, value);
567 node = json_node_new (JSON_NODE_NULL);
569 g_ptr_array_add (array->elements, node);
573 * json_array_add_null_element:
574 * @array: a #JsonArray
576 * Conveniently adds a null element into @array
578 * See also: json_array_add_element(), %JSON_NODE_NULL
583 json_array_add_null_element (JsonArray *array)
587 g_return_if_fail (array != NULL);
589 node = json_node_new (JSON_NODE_NULL);
591 g_ptr_array_add (array->elements, node);
595 * json_array_add_array_element:
596 * @array: a #JsonArray
597 * @value: (transfer full): a #JsonArray
599 * Conveniently adds an array into @array. The @array takes ownership
600 * of the newly added #JsonArray
602 * See also: json_array_add_element(), json_node_take_array()
607 json_array_add_array_element (JsonArray *array,
612 g_return_if_fail (array != NULL);
613 g_return_if_fail (value != NULL);
617 node = json_node_new (JSON_NODE_ARRAY);
618 json_node_take_array (node, value);
621 node = json_node_new (JSON_NODE_NULL);
623 g_ptr_array_add (array->elements, node);
627 * json_array_add_object_element:
628 * @array: a #JsonArray
629 * @value: (transfer full): a #JsonObject
631 * Conveniently adds an object into @array. The @array takes ownership
632 * of the newly added #JsonObject
634 * See also: json_array_add_element(), json_node_take_object()
639 json_array_add_object_element (JsonArray *array,
644 g_return_if_fail (array != NULL);
645 g_return_if_fail (value != NULL);
649 node = json_node_new (JSON_NODE_OBJECT);
650 json_node_take_object (node, value);
653 node = json_node_new (JSON_NODE_NULL);
655 g_ptr_array_add (array->elements, node);
659 * json_array_remove_element:
660 * @array: a #JsonArray
661 * @index_: the position of the element to be removed
663 * Removes the #JsonNode inside @array at @index_ freeing its allocated
667 json_array_remove_element (JsonArray *array,
670 g_return_if_fail (array != NULL);
671 g_return_if_fail (index_ < array->elements->len);
673 json_node_free (g_ptr_array_remove_index (array->elements, index_));
677 * json_array_foreach_element:
678 * @array: a #JsonArray
679 * @func: (scope call): the function to be called on each element
680 * @data: (closure): data to be passed to the function
682 * Iterates over all elements of @array and calls @func on
685 * It is safe to change the value of a #JsonNode of the @array
686 * from within the iterator @func, but it is not safe to add or
687 * remove elements from the @array.
692 json_array_foreach_element (JsonArray *array,
693 JsonArrayForeach func,
698 g_return_if_fail (array != NULL);
699 g_return_if_fail (func != NULL);
701 for (i = 0; i < array->elements->len; i++)
703 JsonNode *element_node;
705 element_node = g_ptr_array_index (array->elements, i);
707 (* func) (array, i, element_node, data);