Imported Upstream version 1.2.4
[platform/upstream/json-glib.git] / json-glib / json-node.c
1 /* json-node.c - JSON object model node
2  * 
3  * This file is part of JSON-GLib
4  * Copyright (C) 2007  OpenedHand Ltd.
5  * Copyright (C) 2009  Intel Corp.
6  * Copyright (C) 2015  Collabora Ltd.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  *
21  * Author:
22  *   Emmanuele Bassi  <ebassi@linux.intel.com>
23  *   Philip Withnall  <philip.withnall@collabora.co.uk>
24  */
25
26 #include "config.h"
27
28 #include <glib.h>
29
30 #include "json-types.h"
31 #include "json-types-private.h"
32 #include "json-debug.h"
33
34 /**
35  * SECTION:json-node
36  * @short_description: Node in a JSON object model
37  *
38  * A #JsonNode is a generic container of elements inside a JSON stream.
39  * It can contain fundamental types (integers, booleans, floating point
40  * numbers, strings) and complex types (arrays and objects).
41  *
42  * When parsing a JSON data stream you extract the root node and walk
43  * the node tree by retrieving the type of data contained inside the
44  * node with the %JSON_NODE_TYPE macro. If the node contains a fundamental
45  * type you can retrieve a copy of the #GValue holding it with the
46  * json_node_get_value() function, and then use the #GValue API to extract
47  * the data; if the node contains a complex type you can retrieve the
48  * #JsonObject or the #JsonArray using json_node_get_object() or
49  * json_node_get_array() respectively, and then retrieve the nodes
50  * they contain.
51  *
52  * A #JsonNode may be marked as immutable using json_node_seal(). This marks the
53  * node and all its descendents as read-only, and means that subsequent calls to
54  * setter functions (such as json_node_set_array()) on them will abort as a
55  * programmer error. By marking a node tree as immutable, it may be referenced
56  * in multiple places and its hash value cached for fast lookups, without the
57  * possibility of a value deep within the tree changing and affecting hash
58  * values. Immutable #JsonNodes may be passed to functions which retain a
59  * reference to them without needing to take a copy.
60  *
61  * #JsonNode supports two types of memory management: alloc/free semantics, and
62  * ref/unref semantics. The two may be mixed to a limited extent: nodes may be
63  * allocated (which gives them a reference count of 1), referenced zero or more
64  * times, unreferenced exactly that number of times (using json_node_unref()),
65  * then either unreferenced exactly once more or freed (using json_node_free())
66  * to destroy them. json_node_free() must not be used when a node might have a
67  * reference count not equal to 1. To this end, json-glib uses json_node_copy()
68  * and json_node_unref() internally.
69  */
70
71 G_DEFINE_BOXED_TYPE (JsonNode, json_node, json_node_copy, json_node_unref);
72
73 /**
74  * json_node_get_value_type:
75  * @node: a #JsonNode
76  *
77  * Returns the #GType of the payload of the node.
78  *
79  * Return value: a #GType for the payload.
80  *
81  * Since: 0.4
82  */
83 GType
84 json_node_get_value_type (JsonNode *node)
85 {
86   g_return_val_if_fail (node != NULL, G_TYPE_INVALID);
87
88   switch (node->type)
89     {
90     case JSON_NODE_OBJECT:
91       return JSON_TYPE_OBJECT;
92
93     case JSON_NODE_ARRAY:
94       return JSON_TYPE_ARRAY;
95
96     case JSON_NODE_NULL:
97       return G_TYPE_INVALID;
98
99     case JSON_NODE_VALUE:
100       if (node->data.value)
101         return JSON_VALUE_TYPE (node->data.value);
102       else
103         return G_TYPE_INVALID;
104
105     default:
106       g_assert_not_reached ();
107       return G_TYPE_INVALID;
108     }
109 }
110
111 /**
112  * json_node_alloc: (constructor)
113  *
114  * Allocates a new #JsonNode. Use json_node_init() and its variants
115  * to initialize the returned value.
116  *
117  * Return value: (transfer full): the newly allocated #JsonNode. Use
118  *   json_node_free() to free the resources allocated by this function
119  *
120  * Since: 0.16
121  */
122 JsonNode *
123 json_node_alloc (void)
124 {
125   JsonNode *node = NULL;
126
127   node = g_slice_new0 (JsonNode);
128   node->ref_count = 1;
129   node->allocated = TRUE;
130
131   return node;
132 }
133
134 static void
135 json_node_unset (JsonNode *node)
136 {
137   /* Note: Don't use JSON_NODE_IS_VALID here because this may legitimately be
138    * called with (node->ref_count == 0) from json_node_unref(). */
139   g_assert (node != NULL);
140
141   switch (node->type)
142     {
143     case JSON_NODE_OBJECT:
144       if (node->data.object)
145         json_object_unref (node->data.object);
146       break;
147
148     case JSON_NODE_ARRAY:
149       if (node->data.array)
150         json_array_unref (node->data.array);
151       break;
152
153     case JSON_NODE_VALUE:
154       if (node->data.value)
155         json_value_unref (node->data.value);
156       break;
157
158     case JSON_NODE_NULL:
159       break;
160     }
161 }
162
163 /**
164  * json_node_init:
165  * @node: the #JsonNode to initialize
166  * @type: the type of JSON node to initialize @node to
167  *
168  * Initializes a @node to a specific @type.
169  *
170  * If the node has already been initialized once, it will be reset to
171  * the given type, and any data contained will be cleared.
172  *
173  * Return value: (transfer none): the initialized #JsonNode
174  *
175  * Since: 0.16
176  */
177 JsonNode *
178 json_node_init (JsonNode *node,
179                 JsonNodeType type)
180 {
181   g_return_val_if_fail (type >= JSON_NODE_OBJECT &&
182                         type <= JSON_NODE_NULL, NULL);
183   g_return_val_if_fail (node->ref_count == 1, NULL);
184
185   json_node_unset (node);
186
187   node->type = type;
188
189   return node;
190 }
191
192 /**
193  * json_node_init_object:
194  * @node: the #JsonNode to initialize
195  * @object: (allow-none): the #JsonObject to initialize @node with, or %NULL
196  *
197  * Initializes @node to %JSON_NODE_OBJECT and sets @object into it.
198  *
199  * This function will take a reference on @object.
200  *
201  * If the node has already been initialized once, it will be reset to
202  * the given type, and any data contained will be cleared.
203  *
204  * Return value: (transfer none): the initialized #JsonNode
205  *
206  * Since: 0.16
207  */
208 JsonNode *
209 json_node_init_object (JsonNode   *node,
210                        JsonObject *object)
211 {
212   g_return_val_if_fail (node != NULL, NULL);
213   
214   json_node_init (node, JSON_NODE_OBJECT);
215   json_node_set_object (node, object);
216
217   return node;
218 }
219
220 /**
221  * json_node_init_array:
222  * @node: the #JsonNode to initialize
223  * @array: (allow-none): the #JsonArray to initialize @node with, or %NULL
224  *
225  * Initializes @node to %JSON_NODE_ARRAY and sets @array into it.
226  *
227  * This function will take a reference on @array.
228  *
229  * If the node has already been initialized once, it will be reset to
230  * the given type, and any data contained will be cleared.
231  *
232  * Return value: (transfer none): the initialized #JsonNode
233  *
234  * Since: 0.16
235  */
236 JsonNode *
237 json_node_init_array (JsonNode  *node,
238                       JsonArray *array)
239 {
240   g_return_val_if_fail (node != NULL, NULL);
241
242   json_node_init (node, JSON_NODE_ARRAY);
243   json_node_set_array (node, array);
244
245   return node;
246 }
247
248 /**
249  * json_node_init_int:
250  * @node: the #JsonNode to initialize
251  * @value: an integer
252  *
253  * Initializes @node to %JSON_NODE_VALUE and sets @value into it.
254  *
255  * If the node has already been initialized once, it will be reset to
256  * the given type, and any data contained will be cleared.
257  *
258  * Return value: (transfer none): the initialized #JsonNode
259  *
260  * Since: 0.16
261  */
262 JsonNode *
263 json_node_init_int (JsonNode *node,
264                     gint64    value)
265 {
266   g_return_val_if_fail (node != NULL, NULL);
267
268   json_node_init (node, JSON_NODE_VALUE);
269   json_node_set_int (node, value);
270
271   return node;
272 }
273
274 /**
275  * json_node_init_double:
276  * @node: the #JsonNode to initialize
277  * @value: a floating point value
278  *
279  * Initializes @node to %JSON_NODE_VALUE and sets @value into it.
280  *
281  * If the node has already been initialized once, it will be reset to
282  * the given type, and any data contained will be cleared.
283  *
284  * Return value: (transfer none): the initialized #JsonNode
285  *
286  * Since: 0.16
287  */
288 JsonNode *
289 json_node_init_double (JsonNode *node,
290                        gdouble   value)
291 {
292   g_return_val_if_fail (node != NULL, NULL);
293
294   json_node_init (node, JSON_NODE_VALUE);
295   json_node_set_double (node, value);
296
297   return node;
298 }
299
300 /**
301  * json_node_init_boolean:
302  * @node: the #JsonNode to initialize
303  * @value: a boolean value
304  *
305  * Initializes @node to %JSON_NODE_VALUE and sets @value into it.
306  *
307  * If the node has already been initialized once, it will be reset to
308  * the given type, and any data contained will be cleared.
309  *
310  * Return value: (transfer none): the initialized #JsonNode
311  *
312  * Since: 0.16
313  */
314 JsonNode *
315 json_node_init_boolean (JsonNode *node,
316                         gboolean  value)
317 {
318   g_return_val_if_fail (node != NULL, NULL);
319
320   json_node_init (node, JSON_NODE_VALUE);
321   json_node_set_boolean (node, value);
322
323   return node;
324 }
325
326 /**
327  * json_node_init_string:
328  * @node: the #JsonNode to initialize
329  * @value: (allow-none): a string value
330  *
331  * Initializes @node to %JSON_NODE_VALUE and sets @value into it.
332  *
333  * If the node has already been initialized once, it will be reset to
334  * the given type, and any data contained will be cleared.
335  *
336  * Return value: (transfer none): the initialized #JsonNode
337  *
338  * Since: 0.16
339  */
340 JsonNode *
341 json_node_init_string (JsonNode   *node,
342                        const char *value)
343 {
344   g_return_val_if_fail (node != NULL, NULL);
345
346   json_node_init (node, JSON_NODE_VALUE);
347   json_node_set_string (node, value);
348
349   return node;
350 }
351
352 /**
353  * json_node_init_null:
354  * @node: the #JsonNode to initialize
355  *
356  * Initializes @node to %JSON_NODE_NULL.
357  *
358  * If the node has already been initialized once, it will be reset to
359  * the given type, and any data contained will be cleared.
360  *
361  * Return value: (transfer none): the initialized #JsonNode
362  *
363  * Since: 0.16
364  */
365 JsonNode *
366 json_node_init_null (JsonNode *node)
367 {
368   g_return_val_if_fail (node != NULL, NULL);
369
370   return json_node_init (node, JSON_NODE_NULL);
371 }
372
373 /**
374  * json_node_new: (constructor)
375  * @type: a #JsonNodeType
376  *
377  * Creates a new #JsonNode of @type.
378  *
379  * This is a convenience function for json_node_alloc() and json_node_init(),
380  * and it's the equivalent of:
381  *
382  * |[<!-- language="C" -->
383      json_node_init (json_node_alloc (), type);
384  * ]|
385  *
386  * Return value: (transfer full): the newly created #JsonNode
387  */
388 JsonNode *
389 json_node_new (JsonNodeType type)
390 {
391   g_return_val_if_fail (type >= JSON_NODE_OBJECT &&
392                         type <= JSON_NODE_NULL, NULL);
393
394   return json_node_init (json_node_alloc (), type);
395 }
396
397 /**
398  * json_node_copy:
399  * @node: a #JsonNode
400  *
401  * Copies @node. If the node contains complex data types, their reference
402  * counts are increased, regardless of whether the node is mutable or
403  * immutable.
404  *
405  * The copy will be immutable if, and only if, @node is immutable. However,
406  * there should be no need to copy an immutable node.
407  *
408  * Return value: (transfer full): the copied #JsonNode
409  */
410 JsonNode *
411 json_node_copy (JsonNode *node)
412 {
413   JsonNode *copy;
414
415   g_return_val_if_fail (JSON_NODE_IS_VALID (node), NULL);
416
417   copy = json_node_alloc ();
418   copy->type = node->type;
419   copy->immutable = node->immutable;
420
421 #ifdef JSON_ENABLE_DEBUG
422   if (node->immutable)
423     {
424       JSON_NOTE (NODE, "Copying immutable JsonNode %p of type %s",
425                  node,
426                  json_node_type_name (node));
427     }
428 #endif
429
430   switch (copy->type)
431     {
432     case JSON_NODE_OBJECT:
433       copy->data.object = json_node_dup_object (node);
434       break;
435
436     case JSON_NODE_ARRAY:
437       copy->data.array = json_node_dup_array (node);
438       break;
439
440     case JSON_NODE_VALUE:
441       if (node->data.value)
442         copy->data.value = json_value_ref (node->data.value);
443       break;
444
445     case JSON_NODE_NULL:
446       break;
447
448     default:
449       g_assert_not_reached ();
450     }
451
452   return copy;
453 }
454
455 /**
456  * json_node_ref:
457  * @node: a #JsonNode
458  *
459  * Increment the reference count of @node.
460  *
461  * Since: 1.2
462  * Returns: (transfer full): a pointer to @node
463  */
464 JsonNode *
465 json_node_ref (JsonNode *node)
466 {
467   g_return_val_if_fail (JSON_NODE_IS_VALID (node), NULL);
468
469   g_atomic_int_inc (&node->ref_count);
470
471   return node;
472 }
473
474 /**
475  * json_node_unref:
476  * @node: (transfer full): a #JsonNode
477  *
478  * Decrement the reference count of @node. If it reaches zero, the node is
479  * freed.
480  *
481  * Since: 1.2
482  */
483 void
484 json_node_unref (JsonNode *node)
485 {
486   g_return_if_fail (JSON_NODE_IS_VALID (node));
487
488   if (g_atomic_int_dec_and_test (&node->ref_count))
489     {
490       json_node_unset (node);
491       if (node->allocated)
492         g_slice_free (JsonNode, node);
493     }
494 }
495
496 /**
497  * json_node_set_object:
498  * @node: a #JsonNode initialized to %JSON_NODE_OBJECT
499  * @object: (nullable): a #JsonObject
500  *
501  * Sets @objects inside @node. The reference count of @object is increased.
502  *
503  * If @object is %NULL, the node’s existing object is cleared.
504  *
505  * It is an error to call this on an immutable node.
506  */
507 void
508 json_node_set_object (JsonNode   *node,
509                       JsonObject *object)
510 {
511   g_return_if_fail (JSON_NODE_IS_VALID (node));
512   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT);
513   g_return_if_fail (!node->immutable);
514
515   if (node->data.object != NULL)
516     json_object_unref (node->data.object);
517
518   if (object)
519     node->data.object = json_object_ref (object);
520   else
521     node->data.object = NULL;
522 }
523
524 /**
525  * json_node_take_object:
526  * @node: a #JsonNode initialized to %JSON_NODE_OBJECT
527  * @object: (transfer full): a #JsonObject
528  *
529  * Sets @object inside @node. The reference count of @object is not increased.
530  *
531  * It is an error to call this on an immutable node.
532  */
533 void
534 json_node_take_object (JsonNode   *node,
535                        JsonObject *object)
536 {
537   g_return_if_fail (JSON_NODE_IS_VALID (node));
538   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT);
539   g_return_if_fail (!node->immutable);
540
541   if (node->data.object)
542     {
543       json_object_unref (node->data.object);
544       node->data.object = NULL;
545     }
546
547   if (object)
548     node->data.object = object;
549 }
550
551 /**
552  * json_node_get_object:
553  * @node: a #JsonNode
554  *
555  * Retrieves the #JsonObject stored inside a #JsonNode. If the node does not
556  * hold an object value, %NULL is returned.
557  *
558  * Return value: (transfer none) (nullable): the #JsonObject
559  */
560 JsonObject *
561 json_node_get_object (JsonNode *node)
562 {
563   g_return_val_if_fail (JSON_NODE_IS_VALID (node), NULL);
564   g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT, NULL);
565
566   return node->data.object;
567 }
568
569 /**
570  * json_node_dup_object:
571  * @node: a #JsonNode
572  *
573  * Retrieves the #JsonObject inside @node. The reference count of
574  * the returned object is increased. If the node does not hold an object value,
575  * %NULL is returned.
576  *
577  * Return value: (transfer full) (nullable): the #JsonObject
578  */
579 JsonObject *
580 json_node_dup_object (JsonNode *node)
581 {
582   g_return_val_if_fail (JSON_NODE_IS_VALID (node), NULL);
583   g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT, NULL);
584
585   if (node->data.object)
586     return json_object_ref (node->data.object);
587   
588   return NULL;
589 }
590
591 /**
592  * json_node_set_array:
593  * @node: a #JsonNode initialized to %JSON_NODE_ARRAY
594  * @array: a #JsonArray
595  *
596  * Sets @array inside @node and increases the #JsonArray reference count.
597  *
598  * It is an error to call this on an immutable node.
599  */
600 void
601 json_node_set_array (JsonNode  *node,
602                      JsonArray *array)
603 {
604   g_return_if_fail (JSON_NODE_IS_VALID (node));
605   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY);
606   g_return_if_fail (!node->immutable);
607
608   if (node->data.array)
609     json_array_unref (node->data.array);
610
611   if (array)
612     node->data.array = json_array_ref (array);
613   else
614     node->data.array = NULL;
615 }
616
617 /**
618  * json_node_take_array:
619  * @node: a #JsonNode initialized to %JSON_NODE_ARRAY
620  * @array: (transfer full): a #JsonArray
621  *
622  * Sets @array into @node without increasing the #JsonArray reference count.
623  *
624  * It is an error to call this on an immutable node.
625  */
626 void
627 json_node_take_array (JsonNode  *node,
628                       JsonArray *array)
629 {
630   g_return_if_fail (JSON_NODE_IS_VALID (node));
631   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY);
632   g_return_if_fail (!node->immutable);
633
634   if (node->data.array)
635     {
636       json_array_unref (node->data.array);
637       node->data.array = NULL;
638     }
639
640   if (array)
641     node->data.array = array;
642 }
643
644 /**
645  * json_node_get_array:
646  * @node: a #JsonNode
647  *
648  * Retrieves the #JsonArray stored inside a #JsonNode. If the node does not
649  * hold an array value, %NULL is returned.
650  *
651  * Return value: (transfer none) (nullable): the #JsonArray
652  */
653 JsonArray *
654 json_node_get_array (JsonNode *node)
655 {
656   g_return_val_if_fail (JSON_NODE_IS_VALID (node), NULL);
657   g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY, NULL);
658
659   return node->data.array;
660 }
661
662 /**
663  * json_node_dup_array:
664  * @node: a #JsonNode
665  *
666  * Retrieves the #JsonArray stored inside a #JsonNode and returns it
667  * with its reference count increased by one. If the node does not hold an
668  * array value, %NULL is returned.
669  *
670  * Return value: (transfer full) (nullable): the #JsonArray with its reference
671  *   count increased.
672  */
673 JsonArray *
674 json_node_dup_array (JsonNode *node)
675 {
676   g_return_val_if_fail (JSON_NODE_IS_VALID (node), NULL);
677   g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY, NULL);
678
679   if (node->data.array)
680     return json_array_ref (node->data.array);
681
682   return NULL;
683 }
684
685 /**
686  * json_node_get_value:
687  * @node: a #JsonNode
688  * @value: (out caller-allocates): return location for an uninitialized value
689  *
690  * Retrieves a value from a #JsonNode and copies into @value. When done
691  * using it, call g_value_unset() on the #GValue. If the node does not hold a
692  * scalar value, @value is not modified.
693  */
694 void
695 json_node_get_value (JsonNode *node,
696                      GValue   *value)
697 {
698   g_return_if_fail (JSON_NODE_IS_VALID (node));
699   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
700
701   if (node->data.value)
702     {
703       g_value_init (value, JSON_VALUE_TYPE (node->data.value));
704       switch (JSON_VALUE_TYPE (node->data.value))
705         {
706         case G_TYPE_INT64:
707           g_value_set_int64 (value, json_value_get_int (node->data.value));
708           break;
709
710         case G_TYPE_DOUBLE:
711           g_value_set_double (value, json_value_get_double (node->data.value));
712           break;
713
714         case G_TYPE_BOOLEAN:
715           g_value_set_boolean (value, json_value_get_boolean (node->data.value));
716           break;
717
718         case G_TYPE_STRING:
719           g_value_set_string (value, json_value_get_string (node->data.value));
720           break;
721
722         default:
723           break;
724         }
725     }
726 }
727
728 /**
729  * json_node_set_value:
730  * @node: a #JsonNode initialized to %JSON_NODE_VALUE
731  * @value: the #GValue to set
732  *
733  * Sets @value inside @node. The passed #GValue is copied into the #JsonNode.
734  *
735  * It is an error to call this on an immutable node.
736  */
737 void
738 json_node_set_value (JsonNode     *node,
739                      const GValue *value)
740 {
741   g_return_if_fail (JSON_NODE_IS_VALID (node));
742   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
743   g_return_if_fail (G_VALUE_TYPE (value) != G_TYPE_INVALID);
744   g_return_if_fail (!node->immutable);
745
746   if (node->data.value == NULL)
747     node->data.value = json_value_alloc ();
748
749   switch (G_VALUE_TYPE (value))
750     {
751     /* auto-promote machine integers to 64 bit integers */
752     case G_TYPE_INT64:
753     case G_TYPE_INT:
754       json_value_init (node->data.value, JSON_VALUE_INT);
755       if (G_VALUE_TYPE (value) == G_TYPE_INT64)
756         json_value_set_int (node->data.value, g_value_get_int64 (value));
757       else
758         json_value_set_int (node->data.value, g_value_get_int (value));
759       break;
760
761     case G_TYPE_BOOLEAN:
762       json_value_init (node->data.value, JSON_VALUE_BOOLEAN);
763       json_value_set_boolean (node->data.value, g_value_get_boolean (value));
764       break;
765
766     /* auto-promote single-precision floats to double precision floats */
767     case G_TYPE_DOUBLE:
768     case G_TYPE_FLOAT:
769       json_value_init (node->data.value, JSON_VALUE_DOUBLE);
770       if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE)
771         json_value_set_double (node->data.value, g_value_get_double (value));
772       else
773         json_value_set_double (node->data.value, g_value_get_float (value));
774       break;
775
776     case G_TYPE_STRING:
777       json_value_init (node->data.value, JSON_VALUE_STRING);
778       json_value_set_string (node->data.value, g_value_get_string (value));
779       break;
780
781     default:
782       g_message ("Invalid value of type '%s'",
783                  g_type_name (G_VALUE_TYPE (value)));
784       return;
785     }
786
787 }
788
789 /**
790  * json_node_free:
791  * @node: a #JsonNode
792  *
793  * Frees the resources allocated by @node.
794  */
795 void
796 json_node_free (JsonNode *node)
797 {
798   g_return_if_fail (node == NULL || JSON_NODE_IS_VALID (node));
799   g_return_if_fail (node == NULL || node->allocated);
800
801   if (G_LIKELY (node))
802     {
803       if (node->ref_count > 1)
804         g_warning ("Freeing a JsonNode %p owned by other code.", node);
805
806       json_node_unset (node);
807       g_slice_free (JsonNode, node);
808     }
809 }
810
811 /**
812  * json_node_seal:
813  * @node: a #JsonNode
814  *
815  * Seals the #JsonNode, making it immutable to further changes. In order to be
816  * sealed, the @node must have a type and value set. The value will be
817  * recursively sealed — if the node holds an object, that #JsonObject will be
818  * sealed, etc.
819  *
820  * If the @node is already immutable, this is a no-op.
821  *
822  * Since: 1.2
823  */
824 void
825 json_node_seal (JsonNode *node)
826 {
827   g_return_if_fail (JSON_NODE_IS_VALID (node));
828
829   if (node->immutable)
830     return;
831
832   switch (node->type)
833     {
834     case JSON_NODE_OBJECT:
835       g_return_if_fail (node->data.object != NULL);
836       json_object_seal (node->data.object);
837       break;
838     case JSON_NODE_ARRAY:
839       g_return_if_fail (node->data.array != NULL);
840       json_array_seal (node->data.array);
841       break;
842     case JSON_NODE_NULL:
843       break;
844     case JSON_NODE_VALUE:
845       g_return_if_fail (node->data.value != NULL);
846       json_value_seal (node->data.value);
847       break;
848     default:
849       g_assert_not_reached ();
850     }
851
852   node->immutable = TRUE;
853 }
854
855 /**
856  * json_node_is_immutable:
857  * @node: a #JsonNode
858  *
859  * Check whether the given @node has been marked as immutable by calling
860  * json_node_seal() on it.
861  *
862  * Since: 1.2
863  * Returns: %TRUE if the @node is immutable
864  */
865 gboolean
866 json_node_is_immutable (JsonNode *node)
867 {
868   g_return_val_if_fail (JSON_NODE_IS_VALID (node), FALSE);
869
870   return node->immutable;
871 }
872
873 /**
874  * json_node_type_name:
875  * @node: a #JsonNode
876  *
877  * Retrieves the user readable name of the data type contained by @node.
878  *
879  * Return value: a string containing the name of the type. The returned string
880  *   is owned by the node and should never be modified or freed
881  */
882 const gchar *
883 json_node_type_name (JsonNode *node)
884 {
885   g_return_val_if_fail (node != NULL, "(null)");
886
887   switch (node->type)
888     {
889     case JSON_NODE_OBJECT:
890     case JSON_NODE_ARRAY:
891     case JSON_NODE_NULL:
892       return json_node_type_get_name (node->type);
893
894     case JSON_NODE_VALUE:
895       if (node->data.value)
896         return json_value_type_get_name (node->data.value->type);
897     }
898
899   return "unknown";
900 }
901
902 const gchar *
903 json_node_type_get_name (JsonNodeType node_type)
904 {
905   switch (node_type)
906     {
907     case JSON_NODE_OBJECT:
908       return "JsonObject";
909
910     case JSON_NODE_ARRAY:
911       return "JsonArray";
912
913     case JSON_NODE_NULL:
914       return "NULL";
915
916     case JSON_NODE_VALUE:
917       return "Value";
918
919     default:
920       g_assert_not_reached ();
921       break;
922     }
923
924   return "unknown";
925 }
926
927 /**
928  * json_node_set_parent:
929  * @node: a #JsonNode
930  * @parent: (transfer none): the parent #JsonNode of @node
931  *
932  * Sets the parent #JsonNode of @node.
933  *
934  * It is an error to call this with an immutable @parent. @node may be
935  * immutable.
936  *
937  * Since: 0.8
938  */
939 void
940 json_node_set_parent (JsonNode *node,
941                       JsonNode *parent)
942 {
943   g_return_if_fail (JSON_NODE_IS_VALID (node));
944   g_return_if_fail (parent == NULL ||
945                     !json_node_is_immutable (parent));
946
947   node->parent = parent;
948 }
949
950 /**
951  * json_node_get_parent:
952  * @node: a #JsonNode
953  *
954  * Retrieves the parent #JsonNode of @node.
955  *
956  * Return value: (transfer none) (nullable): the parent node, or %NULL if @node
957  *   is the root node
958  */
959 JsonNode *
960 json_node_get_parent (JsonNode *node)
961 {
962   g_return_val_if_fail (JSON_NODE_IS_VALID (node), NULL);
963
964   return node->parent;
965 }
966
967 /**
968  * json_node_set_string:
969  * @node: a #JsonNode initialized to %JSON_NODE_VALUE
970  * @value: a string value
971  *
972  * Sets @value as the string content of the @node, replacing any existing
973  * content.
974  *
975  * It is an error to call this on an immutable node.
976  */
977 void
978 json_node_set_string (JsonNode    *node,
979                       const gchar *value)
980 {
981   g_return_if_fail (JSON_NODE_IS_VALID (node));
982   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
983   g_return_if_fail (!node->immutable);
984
985   if (node->data.value == NULL)
986     node->data.value = json_value_init (json_value_alloc (), JSON_VALUE_STRING);
987   else
988     json_value_init (node->data.value, JSON_VALUE_STRING);
989
990   json_value_set_string (node->data.value, value);
991 }
992
993 /**
994  * json_node_get_string:
995  * @node: a #JsonNode of type %JSON_NODE_VALUE
996  *
997  * Gets the string value stored inside a #JsonNode. If the node does not hold a
998  * string value, %NULL is returned.
999  *
1000  * Return value: (nullable): a string value.
1001  */
1002 const gchar *
1003 json_node_get_string (JsonNode *node)
1004 {
1005   g_return_val_if_fail (JSON_NODE_IS_VALID (node), NULL);
1006
1007   if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
1008     return NULL;
1009
1010   if (JSON_VALUE_HOLDS_STRING (node->data.value))
1011     return json_value_get_string (node->data.value);
1012
1013   return NULL;
1014 }
1015
1016 /**
1017  * json_node_dup_string:
1018  * @node: a #JsonNode of type %JSON_NODE_VALUE
1019  *
1020  * Gets a copy of the string value stored inside a #JsonNode. If the node does
1021  * not hold a string value, %NULL is returned.
1022  *
1023  * Return value: (transfer full) (nullable): a newly allocated string
1024  *   containing a copy of the #JsonNode contents. Use g_free() to free the
1025  *   allocated resources
1026  */
1027 gchar *
1028 json_node_dup_string (JsonNode *node)
1029 {
1030   g_return_val_if_fail (JSON_NODE_IS_VALID (node), NULL);
1031
1032   return g_strdup (json_node_get_string (node));
1033 }
1034
1035 /**
1036  * json_node_set_int:
1037  * @node: a #JsonNode of type %JSON_NODE_VALUE
1038  * @value: an integer value
1039  *
1040  * Sets @value as the integer content of the @node, replacing any existing
1041  * content.
1042  *
1043  * It is an error to call this on an immutable node.
1044  */
1045 void
1046 json_node_set_int (JsonNode *node,
1047                    gint64    value)
1048 {
1049   g_return_if_fail (JSON_NODE_IS_VALID (node));
1050   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
1051   g_return_if_fail (!node->immutable);
1052
1053   if (node->data.value == NULL)
1054     node->data.value = json_value_init (json_value_alloc (), JSON_VALUE_INT);
1055   else
1056     json_value_init (node->data.value, JSON_VALUE_INT);
1057
1058   json_value_set_int (node->data.value, value);
1059 }
1060
1061 /**
1062  * json_node_get_int:
1063  * @node: a #JsonNode of type %JSON_NODE_VALUE
1064  *
1065  * Gets the integer value stored inside a #JsonNode. If the node holds a double
1066  * value, its integer component is returned. If the node holds a %FALSE boolean
1067  * value, `0` is returned; otherwise a non-zero integer is returned. If the
1068  * node holds a %JSON_NODE_NULL value or a value of another non-integer type,
1069  * `0` is returned.
1070  *
1071  * Return value: an integer value.
1072  */
1073 gint64
1074 json_node_get_int (JsonNode *node)
1075 {
1076   g_return_val_if_fail (JSON_NODE_IS_VALID (node), 0);
1077
1078   if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
1079     return 0;
1080
1081   if (JSON_VALUE_HOLDS_INT (node->data.value))
1082     return json_value_get_int (node->data.value);
1083
1084   if (JSON_VALUE_HOLDS_DOUBLE (node->data.value))
1085     return json_value_get_double (node->data.value);
1086
1087   if (JSON_VALUE_HOLDS_BOOLEAN (node->data.value))
1088     return json_value_get_boolean (node->data.value);
1089
1090   return 0;
1091 }
1092
1093 /**
1094  * json_node_set_double:
1095  * @node: a #JsonNode of type %JSON_NODE_VALUE
1096  * @value: a double value
1097  *
1098  * Sets @value as the double content of the @node, replacing any existing
1099  * content.
1100  *
1101  * It is an error to call this on an immutable node.
1102  */
1103 void
1104 json_node_set_double (JsonNode *node,
1105                       gdouble   value)
1106 {
1107   g_return_if_fail (JSON_NODE_IS_VALID (node));
1108   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
1109   g_return_if_fail (!node->immutable);
1110
1111   if (node->data.value == NULL)
1112     node->data.value = json_value_init (json_value_alloc (), JSON_VALUE_DOUBLE);
1113   else
1114     json_value_init (node->data.value, JSON_VALUE_DOUBLE);
1115
1116   json_value_set_double (node->data.value, value);
1117 }
1118
1119 /**
1120  * json_node_get_double:
1121  * @node: a #JsonNode of type %JSON_NODE_VALUE
1122  *
1123  * Gets the double value stored inside a #JsonNode. If the node holds an integer
1124  * value, it is returned as a double. If the node holds a %FALSE boolean value,
1125  * `0.0` is returned; otherwise a non-zero double is returned. If the node holds
1126  * a %JSON_NODE_NULL value or a value of another non-double type, `0.0` is
1127  * returned.
1128  *
1129  * Return value: a double value.
1130  */
1131 gdouble
1132 json_node_get_double (JsonNode *node)
1133 {
1134   g_return_val_if_fail (JSON_NODE_IS_VALID (node), 0.0);
1135
1136   if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
1137     return 0;
1138
1139   if (JSON_VALUE_HOLDS_DOUBLE (node->data.value))
1140     return json_value_get_double (node->data.value);
1141
1142   if (JSON_VALUE_HOLDS_INT (node->data.value))
1143     return (gdouble) json_value_get_int (node->data.value);
1144
1145   if (JSON_VALUE_HOLDS_BOOLEAN (node->data.value))
1146     return (gdouble) json_value_get_boolean (node->data.value);
1147
1148   return 0.0;
1149 }
1150
1151 /**
1152  * json_node_set_boolean:
1153  * @node: a #JsonNode of type %JSON_NODE_VALUE
1154  * @value: a boolean value
1155  *
1156  * Sets @value as the boolean content of the @node, replacing any existing
1157  * content.
1158  *
1159  * It is an error to call this on an immutable node.
1160  */
1161 void
1162 json_node_set_boolean (JsonNode *node,
1163                        gboolean  value)
1164 {
1165   g_return_if_fail (JSON_NODE_IS_VALID (node));
1166   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
1167   g_return_if_fail (!node->immutable);
1168
1169   if (node->data.value == NULL)
1170     node->data.value = json_value_init (json_value_alloc (), JSON_VALUE_BOOLEAN);
1171   else
1172     json_value_init (node->data.value, JSON_VALUE_BOOLEAN);
1173
1174   json_value_set_boolean (node->data.value, value);
1175 }
1176
1177 /**
1178  * json_node_get_boolean:
1179  * @node: a #JsonNode of type %JSON_NODE_VALUE
1180  *
1181  * Gets the boolean value stored inside a #JsonNode. If the node holds an
1182  * integer or double value which is zero, %FALSE is returned; otherwise %TRUE
1183  * is returned. If the node holds a %JSON_NODE_NULL value or a value of another
1184  * non-boolean type, %FALSE is returned.
1185  *
1186  * Return value: a boolean value.
1187  */
1188 gboolean
1189 json_node_get_boolean (JsonNode *node)
1190 {
1191   g_return_val_if_fail (JSON_NODE_IS_VALID (node), FALSE);
1192
1193   if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
1194     return FALSE;
1195
1196   if (JSON_VALUE_HOLDS_BOOLEAN (node->data.value))
1197     return json_value_get_boolean (node->data.value);
1198
1199   if (JSON_VALUE_HOLDS_INT (node->data.value))
1200     return json_value_get_int (node->data.value) != 0;
1201
1202   if (JSON_VALUE_HOLDS_DOUBLE (node->data.value))
1203     return json_value_get_double (node->data.value) != 0.0;
1204
1205   return FALSE;
1206 }
1207
1208 /**
1209  * json_node_get_node_type:
1210  * @node: a #JsonNode
1211  *
1212  * Retrieves the #JsonNodeType of @node
1213  *
1214  * Return value: the type of the node
1215  *
1216  * Since: 0.8
1217  */
1218 JsonNodeType
1219 json_node_get_node_type (JsonNode *node)
1220 {
1221   g_return_val_if_fail (JSON_NODE_IS_VALID (node), JSON_NODE_NULL);
1222
1223   return node->type;
1224 }
1225
1226 /**
1227  * json_node_is_null:
1228  * @node: a #JsonNode
1229  *
1230  * Checks whether @node is a %JSON_NODE_NULL.
1231  *
1232  * A %JSON_NODE_NULL node is not the same as a %NULL #JsonNode; a
1233  * %JSON_NODE_NULL represents a 'null' value in the JSON tree.
1234  *
1235  * Return value: %TRUE if the node is null
1236  *
1237  * Since: 0.8
1238  */
1239 gboolean
1240 json_node_is_null (JsonNode *node)
1241 {
1242   g_return_val_if_fail (JSON_NODE_IS_VALID (node), TRUE);
1243
1244   return node->type == JSON_NODE_NULL;
1245 }
1246
1247 /**
1248  * json_type_is_a:
1249  * @sub: sub-type
1250  * @super: super-type
1251  *
1252  * Check whether @sub is a sub-type of, or equal to, @super. The only sub-type
1253  * relationship in the JSON Schema type system is that
1254  * %WBL_PRIMITIVE_TYPE_INTEGER is a sub-type of %WBL_PRIMITIVE_TYPE_NUMBER.
1255  *
1256  * Formally, this function calculates: `@sub <: @super`.
1257  *
1258  * Reference: http://json-schema.org/latest/json-schema-core.html#rfc.section.3.5
1259  *
1260  * Returns: %TRUE if @sub is a sub-type of, or equal to, @super; %FALSE
1261  *    otherwise
1262  * Since: 1.2
1263  */
1264 static gboolean
1265 json_type_is_a (JsonNode  *sub,
1266                 JsonNode  *super)
1267 {
1268   if (super->type == JSON_NODE_VALUE && sub->type == JSON_NODE_VALUE)
1269     {
1270       JsonValueType super_value_type, sub_value_type;
1271
1272       if (super->data.value == NULL || sub->data.value == NULL)
1273         return FALSE;
1274
1275       super_value_type = super->data.value->type;
1276       sub_value_type = sub->data.value->type;
1277
1278       return (super_value_type == sub_value_type ||
1279               (super_value_type == JSON_VALUE_DOUBLE &&
1280                sub_value_type == JSON_VALUE_INT));
1281     }
1282
1283   return (super->type == sub->type);
1284 }
1285
1286 /**
1287  * json_string_hash:
1288  * @key: (type utf8): a JSON string to hash
1289  *
1290  * Calculate a hash value for the given @key (a UTF-8 JSON string).
1291  *
1292  * Note: Member names are compared byte-wise, without applying any Unicode
1293  * decomposition or normalisation. This is not explicitly mentioned in the JSON
1294  * standard (ECMA-404), but is assumed.
1295  *
1296  * Returns: hash value for @key
1297  * Since: 1.2
1298  */
1299 guint
1300 json_string_hash (gconstpointer key)
1301 {
1302   return g_str_hash (key);
1303 }
1304
1305 /**
1306  * json_string_equal:
1307  * @a: (type utf8): a JSON string
1308  * @b: (type utf8): another JSON string
1309  *
1310  * Check whether @a and @b are equal UTF-8 JSON strings.
1311  *
1312  * Returns: %TRUE if @a and @b are equal; %FALSE otherwise
1313  * Since: 1.2
1314  */
1315 gboolean
1316 json_string_equal (gconstpointer  a,
1317                    gconstpointer  b)
1318 {
1319   return g_str_equal (a, b);
1320 }
1321
1322 /**
1323  * json_string_compare:
1324  * @a: (type utf8): a JSON string
1325  * @b: (type utf8): another JSON string
1326  *
1327  * Check whether @a and @b are equal UTF-8 JSON strings and return an ordering
1328  * over them in strcmp() style.
1329  *
1330  * Returns: an integer less than zero if @a < @b, equal to zero if @a == @b, and
1331  *    greater than zero if @a > @b
1332  * Since: 1.2
1333  */
1334 gint
1335 json_string_compare (gconstpointer  a,
1336                      gconstpointer  b)
1337 {
1338   return g_strcmp0 (a, b);
1339 }
1340
1341 /**
1342  * json_node_hash:
1343  * @key: (type JsonNode): a JSON node to hash
1344  *
1345  * Calculate a hash value for the given @key (a #JsonNode).
1346  *
1347  * The hash is calculated over the node and its value, recursively. If the node
1348  * is immutable, this is a fast operation; otherwise, it scales proportionally
1349  * with the size of the node’s value (for example, with the number of members
1350  * in the #JsonObject if this node contains an object).
1351  *
1352  * Returns: hash value for @key
1353  * Since: 1.2
1354  */
1355 guint
1356 json_node_hash (gconstpointer key)
1357 {
1358   JsonNode *node;  /* unowned */
1359
1360   /* These are all randomly generated and arbitrary. */
1361   const guint value_hash = 0xc19e75ad;
1362   const guint array_hash = 0x865acfc2;
1363   const guint object_hash = 0x3c8f3135;
1364
1365   node = (JsonNode *) key;
1366
1367   /* XOR the hash values with a (constant) random number depending on the node’s
1368    * type so that empty values, arrays and objects do not all collide at the
1369    * hash value 0. */
1370   switch (node->type)
1371     {
1372     case JSON_NODE_NULL:
1373       return 0;
1374     case JSON_NODE_VALUE:
1375       return value_hash ^ json_value_hash (node->data.value);
1376     case JSON_NODE_ARRAY:
1377       return array_hash ^ json_array_hash (json_node_get_array (node));
1378     case JSON_NODE_OBJECT:
1379       return object_hash ^ json_object_hash (json_node_get_object (node));
1380     default:
1381       g_assert_not_reached ();
1382     }
1383 }
1384
1385 /**
1386  * json_node_equal:
1387  * @a: (type JsonNode): a JSON node
1388  * @b: (type JsonNode): another JSON node
1389  *
1390  * Check whether @a and @b are equal #JsonNodes, meaning they have the same
1391  * type and same values (checked recursively). Note that integer values are
1392  * compared numerically, ignoring type, so a double value 4.0 is equal to the
1393  * integer value 4.
1394  *
1395  * Returns: %TRUE if @a and @b are equal; %FALSE otherwise
1396  * Since: 1.2
1397  */
1398 gboolean
1399 json_node_equal (gconstpointer  a,
1400                  gconstpointer  b)
1401 {
1402   JsonNode *node_a, *node_b;  /* unowned */
1403
1404   node_a = (JsonNode *) a;
1405   node_b = (JsonNode *) b;
1406
1407   /* Identity comparison. */
1408   if (node_a == node_b)
1409     return TRUE;
1410
1411   /* Eliminate mismatched types rapidly. */
1412   if (!json_type_is_a (node_a, node_b) &&
1413       !json_type_is_a (node_b, node_a))
1414     {
1415       return FALSE;
1416     }
1417
1418   switch (node_a->type)
1419     {
1420     case JSON_NODE_NULL:
1421       /* Types match already. */
1422       return TRUE;
1423     case JSON_NODE_ARRAY:
1424       return json_array_equal (json_node_get_array (node_a),
1425                                json_node_get_array (node_b));
1426     case JSON_NODE_OBJECT:
1427       return json_object_equal (json_node_get_object (node_a),
1428                                 json_node_get_object (node_b));
1429     case JSON_NODE_VALUE:
1430       /* Handled below. */
1431       break;
1432     default:
1433       g_assert_not_reached ();
1434     }
1435
1436   /* Handle values. */
1437   switch (node_a->data.value->type)
1438     {
1439     case JSON_VALUE_NULL:
1440       /* Types already match. */
1441       return TRUE;
1442     case JSON_VALUE_BOOLEAN:
1443       return (json_node_get_boolean (node_a) == json_node_get_boolean (node_b));
1444     case JSON_VALUE_STRING:
1445       return json_string_equal (json_node_get_string (node_a),
1446                                 json_node_get_string (node_b));
1447     case JSON_VALUE_DOUBLE:
1448     case JSON_VALUE_INT: {
1449       gdouble val_a, val_b;
1450       JsonValueType value_type_a, value_type_b;
1451
1452       value_type_a = node_a->data.value->type;
1453       value_type_b = node_b->data.value->type;
1454
1455       /* Integer comparison doesn’t need to involve doubles… */
1456       if (value_type_a == JSON_VALUE_INT &&
1457           value_type_b == JSON_VALUE_INT)
1458         {
1459           return (json_node_get_int (node_a) ==
1460                   json_node_get_int (node_b));
1461         }
1462
1463       /* …but everything else does. We can use bitwise double equality here,
1464        * since we’re not doing any calculations which could introduce floating
1465        * point error. We expect that the doubles in the JSON nodes come directly
1466        * from strtod() or similar, so should be bitwise equal for equal string
1467        * representations.
1468        *
1469        * Interesting background reading:
1470        * http://randomascii.wordpress.com/2012/06/26/\
1471        *   doubles-are-not-floats-so-dont-compare-them/
1472        */
1473       if (value_type_a == JSON_VALUE_INT)
1474         val_a = json_node_get_int (node_a);
1475       else
1476         val_a = json_node_get_double (node_a);
1477
1478       if (value_type_b == JSON_VALUE_INT)
1479         val_b = json_node_get_int (node_b);
1480       else
1481         val_b = json_node_get_double (node_b);
1482
1483       return (val_a == val_b);
1484     }
1485     case JSON_VALUE_INVALID:
1486     default:
1487       g_assert_not_reached ();
1488     }
1489 }