1 /* json-node.c - JSON object model node
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>
30 #include "json-types-private.h"
34 * @short_description: Node in a JSON object model
36 * A #JsonNode is a generic container of elements inside a JSON stream.
37 * It can contain fundamental types (integers, booleans, floating point
38 * numbers, strings) and complex types (arrays and objects).
40 * When parsing a JSON data stream you extract the root node and walk
41 * the node tree by retrieving the type of data contained inside the
42 * node with the %JSON_NODE_TYPE macro. If the node contains a fundamental
43 * type you can retrieve a copy of the #GValue holding it with the
44 * json_node_get_value() function, and then use the #GValue API to extract
45 * the data; if the node contains a complex type you can retrieve the
46 * #JsonObject or the #JsonArray using json_node_get_object() or
47 * json_node_get_array() respectively, and then retrieve the nodes
51 G_DEFINE_BOXED_TYPE (JsonNode, json_node, json_node_copy, json_node_free);
54 * json_node_get_value_type:
57 * Returns the #GType of the payload of the node.
59 * Return value: a #GType for the payload.
64 json_node_get_value_type (JsonNode *node)
66 g_return_val_if_fail (node != NULL, G_TYPE_INVALID);
70 case JSON_NODE_OBJECT:
71 return JSON_TYPE_OBJECT;
74 return JSON_TYPE_ARRAY;
77 return G_TYPE_INVALID;
80 return G_VALUE_TYPE (&(node->data.value));
83 g_assert_not_reached ();
84 return G_TYPE_INVALID;
90 * @type: a #JsonNodeType
92 * Creates a new #JsonNode of @type.
94 * Return value: the newly created #JsonNode
97 json_node_new (JsonNodeType type)
101 g_return_val_if_fail (type >= JSON_NODE_OBJECT &&
102 type <= JSON_NODE_NULL, NULL);
104 data = g_slice_new0 (JsonNode);
114 * Copies @node. If the node contains complex data types then the reference
115 * count of the objects is increased.
117 * Return value: (transfer full): the copied #JsonNode
120 json_node_copy (JsonNode *node)
124 g_return_val_if_fail (node != NULL, NULL);
126 copy = g_slice_new0 (JsonNode);
127 copy->type = node->type;
131 case JSON_NODE_OBJECT:
132 if (node->data.object)
133 copy->data.object = json_object_ref (node->data.object);
136 case JSON_NODE_ARRAY:
137 if (node->data.array)
138 copy->data.array = json_array_ref (node->data.array);
141 case JSON_NODE_VALUE:
142 if (G_VALUE_TYPE (&(node->data.value)) != G_TYPE_INVALID)
144 g_value_init (&(copy->data.value), G_VALUE_TYPE (&(node->data.value)));
145 g_value_copy (&(node->data.value), &(copy->data.value));
153 g_assert_not_reached ();
160 * json_node_set_object:
162 * @object: a #JsonObject
164 * Sets @objects inside @node. The reference count of @object is increased.
167 json_node_set_object (JsonNode *node,
170 g_return_if_fail (node != NULL);
171 g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT);
173 if (node->data.object)
174 json_object_unref (node->data.object);
177 node->data.object = json_object_ref (object);
179 node->data.object = NULL;
183 * json_node_take_object:
185 * @object: (transfer full): a #JsonObject
187 * Sets @object inside @node. The reference count of @object is not increased.
190 json_node_take_object (JsonNode *node,
193 g_return_if_fail (node != NULL);
194 g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT);
196 if (node->data.object)
198 json_object_unref (node->data.object);
199 node->data.object = NULL;
203 node->data.object = object;
207 * json_node_get_object:
210 * Retrieves the #JsonObject stored inside a #JsonNode
212 * Return value: (transfer none): the #JsonObject
215 json_node_get_object (JsonNode *node)
217 g_return_val_if_fail (node != NULL, NULL);
218 g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT, NULL);
220 return node->data.object;
224 * json_node_dup_object:
227 * Retrieves the #JsonObject inside @node. The reference count of
228 * the returned object is increased.
230 * Return value: (transfer full): the #JsonObject
233 json_node_dup_object (JsonNode *node)
235 g_return_val_if_fail (node != NULL, NULL);
236 g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT, NULL);
238 if (node->data.object)
239 return json_object_ref (node->data.object);
245 * json_node_set_array:
247 * @array: a #JsonArray
249 * Sets @array inside @node and increases the #JsonArray reference count
252 json_node_set_array (JsonNode *node,
255 g_return_if_fail (node != NULL);
256 g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY);
258 if (node->data.array)
259 json_array_unref (node->data.array);
262 node->data.array = json_array_ref (array);
264 node->data.array = NULL;
268 * json_node_take_array:
270 * @array: (transfer full): a #JsonArray
272 * Sets @array into @node without increasing the #JsonArray reference count.
275 json_node_take_array (JsonNode *node,
278 g_return_if_fail (node != NULL);
279 g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY);
281 if (node->data.array)
283 json_array_unref (node->data.array);
284 node->data.array = NULL;
288 node->data.array = array;
292 * json_node_get_array:
295 * Retrieves the #JsonArray stored inside a #JsonNode
297 * Return value: (transfer none): the #JsonArray
300 json_node_get_array (JsonNode *node)
302 g_return_val_if_fail (node != NULL, NULL);
303 g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY, NULL);
305 return node->data.array;
309 * json_node_dup_array
312 * Retrieves the #JsonArray stored inside a #JsonNode and returns it
313 * with its reference count increased by one.
315 * Return value: (transfer full): the #JsonArray with its reference
319 json_node_dup_array (JsonNode *node)
321 g_return_val_if_fail (node != NULL, NULL);
322 g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY, NULL);
324 if (node->data.array)
325 return json_array_ref (node->data.array);
331 * json_node_get_value:
333 * @value: (out caller-allocates): return location for an uninitialized value
335 * Retrieves a value from a #JsonNode and copies into @value. When done
336 * using it, call g_value_unset() on the #GValue.
339 json_node_get_value (JsonNode *node,
342 g_return_if_fail (node != NULL);
343 g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
345 if (G_VALUE_TYPE (&(node->data.value)) != G_TYPE_INVALID)
347 g_value_init (value, G_VALUE_TYPE (&(node->data.value)));
348 g_value_copy (&(node->data.value), value);
353 node_value_unset (JsonNode *node)
355 if (G_VALUE_TYPE (&(node->data.value)) != G_TYPE_INVALID)
356 g_value_unset (&(node->data.value));
360 * json_node_set_value:
362 * @value: the #GValue to set
364 * Sets @value inside @node. The passed #GValue is copied into the #JsonNode
367 json_node_set_value (JsonNode *node,
370 g_return_if_fail (node != NULL);
371 g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
372 g_return_if_fail (G_VALUE_TYPE (value) != G_TYPE_INVALID);
374 switch (G_VALUE_TYPE (value))
376 /* direct copy for the types we use */
381 node_value_unset (node);
382 g_value_init (&(node->data.value), G_VALUE_TYPE (value));
383 g_value_copy (value, &(node->data.value));
386 /* auto-promote ints to long longs */
388 node_value_unset (node);
389 g_value_init (&(node->data.value), G_TYPE_INT64);
390 g_value_set_int64 (&(node->data.value),
391 g_value_get_int (value));
394 /* auto-promote single precision to double precision */
396 node_value_unset (node);
397 g_value_init (&(node->data.value), G_TYPE_DOUBLE);
398 g_value_set_double (&(node->data.value),
399 g_value_get_float (value));
403 g_warning ("Invalid value of type '%s'",
404 g_type_name (G_VALUE_TYPE (value)));
414 * Frees the resources allocated by @node.
417 json_node_free (JsonNode *node)
423 case JSON_NODE_OBJECT:
424 if (node->data.object)
425 json_object_unref (node->data.object);
428 case JSON_NODE_ARRAY:
429 if (node->data.array)
430 json_array_unref (node->data.array);
433 case JSON_NODE_VALUE:
434 g_value_unset (&(node->data.value));
441 g_slice_free (JsonNode, node);
446 * json_node_type_name:
449 * Retrieves the user readable name of the data type contained by @node.
451 * Return value: a string containing the name of the type. The returned string
452 * is owned by the node and should never be modified or freed
455 json_node_type_name (JsonNode *node)
457 g_return_val_if_fail (node != NULL, "(null)");
461 case JSON_NODE_OBJECT:
462 case JSON_NODE_ARRAY:
464 return json_node_type_get_name (node->type);
466 case JSON_NODE_VALUE:
467 return g_type_name (G_VALUE_TYPE (&(node->data.value)));
474 json_node_type_get_name (JsonNodeType node_type)
478 case JSON_NODE_OBJECT:
481 case JSON_NODE_ARRAY:
487 case JSON_NODE_VALUE:
491 g_assert_not_reached ();
499 * json_node_set_parent:
501 * @parent: (transfer none): the parent #JsonNode of @node
503 * Sets the parent #JsonNode of @node
508 json_node_set_parent (JsonNode *node,
511 g_return_if_fail (node != NULL);
513 node->parent = parent;
517 * json_node_get_parent:
520 * Retrieves the parent #JsonNode of @node.
522 * Return value: (transfer none): the parent node, or %NULL if @node is
526 json_node_get_parent (JsonNode *node)
528 g_return_val_if_fail (node != NULL, NULL);
534 * json_node_set_string:
535 * @node: a #JsonNode of type %JSON_NODE_VALUE
536 * @value: a string value
538 * Sets @value as the string content of the @node, replacing any existing
542 json_node_set_string (JsonNode *node,
545 g_return_if_fail (node != NULL);
546 g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
548 if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_STRING)
549 g_value_set_string (&(node->data.value), value);
552 GValue copy = { 0, };
554 g_value_init (©, G_TYPE_STRING);
555 g_value_set_string (©, value);
557 json_node_set_value (node, ©);
559 g_value_unset (©);
564 * json_node_get_string:
565 * @node: a #JsonNode of type %JSON_NODE_VALUE
567 * Gets the string value stored inside a #JsonNode
569 * Return value: a string value.
572 json_node_get_string (JsonNode *node)
574 g_return_val_if_fail (node != NULL, NULL);
576 if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
579 if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_STRING)
580 return g_value_get_string (&(node->data.value));
586 * json_node_dup_string:
587 * @node: a #JsonNode of type %JSON_NODE_VALUE
589 * Gets a copy of the string value stored inside a #JsonNode
591 * Return value: (transfer full): a newly allocated string containing a copy
592 * of the #JsonNode contents. Use g_free() to free the allocated resources
595 json_node_dup_string (JsonNode *node)
597 g_return_val_if_fail (node != NULL, NULL);
599 if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
602 if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_STRING)
603 return g_value_dup_string (&(node->data.value));
610 * @node: a #JsonNode of type %JSON_NODE_VALUE
611 * @value: an integer value
613 * Sets @value as the integer content of the @node, replacing any existing
617 json_node_set_int (JsonNode *node,
620 g_return_if_fail (node != NULL);
621 g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
623 if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_INT64)
624 g_value_set_int64 (&(node->data.value), value);
627 GValue copy = { 0, };
629 g_value_init (©, G_TYPE_INT64);
630 g_value_set_int64 (©, value);
632 json_node_set_value (node, ©);
634 g_value_unset (©);
640 * @node: a #JsonNode of type %JSON_NODE_VALUE
642 * Gets the integer value stored inside a #JsonNode
644 * Return value: an integer value.
647 json_node_get_int (JsonNode *node)
649 g_return_val_if_fail (node != NULL, 0);
651 if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
654 if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_INT64)
655 return g_value_get_int64 (&(node->data.value));
661 * json_node_set_double:
662 * @node: a #JsonNode of type %JSON_NODE_VALUE
663 * @value: a double value
665 * Sets @value as the double content of the @node, replacing any existing
669 json_node_set_double (JsonNode *node,
672 g_return_if_fail (node != NULL);
673 g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
675 if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_DOUBLE)
676 g_value_set_double (&(node->data.value), value);
679 GValue copy = { 0, };
681 g_value_init (©, G_TYPE_DOUBLE);
682 g_value_set_double (©, value);
684 json_node_set_value (node, ©);
686 g_value_unset (©);
691 * json_node_get_double:
692 * @node: a #JsonNode of type %JSON_NODE_VALUE
694 * Gets the double value stored inside a #JsonNode
696 * Return value: a double value.
699 json_node_get_double (JsonNode *node)
701 g_return_val_if_fail (node != NULL, 0.0);
703 if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
706 if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_DOUBLE)
707 return g_value_get_double (&(node->data.value));
713 * json_node_set_boolean:
714 * @node: a #JsonNode of type %JSON_NODE_VALUE
715 * @value: a boolean value
717 * Sets @value as the boolean content of the @node, replacing any existing
721 json_node_set_boolean (JsonNode *node,
724 g_return_if_fail (node != NULL);
725 g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
727 if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_BOOLEAN)
728 g_value_set_boolean (&(node->data.value), value);
731 GValue copy = { 0, };
733 g_value_init (©, G_TYPE_BOOLEAN);
734 g_value_set_boolean (©, value);
736 json_node_set_value (node, ©);
738 g_value_unset (©);
743 * json_node_get_boolean:
744 * @node: a #JsonNode of type %JSON_NODE_VALUE
746 * Gets the boolean value stored inside a #JsonNode
748 * Return value: a boolean value.
751 json_node_get_boolean (JsonNode *node)
753 g_return_val_if_fail (node != NULL, FALSE);
755 if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
758 if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_BOOLEAN)
759 return g_value_get_boolean (&(node->data.value));
765 * json_node_get_node_type:
768 * Retrieves the #JsonNodeType of @node
770 * Return value: the type of the node
775 json_node_get_node_type (JsonNode *node)
777 g_return_val_if_fail (node != NULL, JSON_NODE_NULL);
786 * Checks whether @node is a %JSON_NODE_NULL
788 * <note>A null node is not the same as a %NULL #JsonNode</note>
790 * Return value: %TRUE if the node is null
795 json_node_is_null (JsonNode *node)
797 g_return_val_if_fail (node != NULL, TRUE);
799 return node->type == JSON_NODE_NULL;