Initial Import
[profile/ivi/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  *
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.
11  *
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.
16  *
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/>.
19  *
20  * Author:
21  *   Emmanuele Bassi  <ebassi@linux.intel.com>
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <glib.h>
29
30 #include "json-types-private.h"
31
32 /**
33  * SECTION:json-node
34  * @short_description: Node in a JSON object model
35  *
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).
39  *
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
48  * they contain.
49  */
50
51 G_DEFINE_BOXED_TYPE (JsonNode, json_node, json_node_copy, json_node_free);
52
53 /**
54  * json_node_get_value_type:
55  * @node: a #JsonNode
56  *
57  * Returns the #GType of the payload of the node.
58  *
59  * Return value: a #GType for the payload.
60  *
61  * Since: 0.4
62  */
63 GType
64 json_node_get_value_type (JsonNode *node)
65 {
66   g_return_val_if_fail (node != NULL, G_TYPE_INVALID);
67
68   switch (node->type)
69     {
70     case JSON_NODE_OBJECT:
71       return JSON_TYPE_OBJECT;
72
73     case JSON_NODE_ARRAY:
74       return JSON_TYPE_ARRAY;
75
76     case JSON_NODE_NULL:
77       return G_TYPE_INVALID;
78
79     case JSON_NODE_VALUE:
80       return G_VALUE_TYPE (&(node->data.value));
81
82     default:
83       g_assert_not_reached ();
84       return G_TYPE_INVALID;
85     }
86 }
87
88 /**
89  * json_node_new:
90  * @type: a #JsonNodeType
91  *
92  * Creates a new #JsonNode of @type.
93  *
94  * Return value: the newly created #JsonNode
95  */
96 JsonNode *
97 json_node_new (JsonNodeType type)
98 {
99   JsonNode *data;
100
101   g_return_val_if_fail (type >= JSON_NODE_OBJECT &&
102                         type <= JSON_NODE_NULL, NULL);
103
104   data = g_slice_new0 (JsonNode);
105   data->type = type;
106
107   return data;
108 }
109
110 /**
111  * json_node_copy:
112  * @node: a #JsonNode
113  *
114  * Copies @node. If the node contains complex data types then the reference
115  * count of the objects is increased.
116  *
117  * Return value: (transfer full): the copied #JsonNode
118  */
119 JsonNode *
120 json_node_copy (JsonNode *node)
121 {
122   JsonNode *copy;
123
124   g_return_val_if_fail (node != NULL, NULL);
125
126   copy = g_slice_new0 (JsonNode);
127   copy->type = node->type;
128
129   switch (copy->type)
130     {
131     case JSON_NODE_OBJECT:
132       if (node->data.object)
133         copy->data.object = json_object_ref (node->data.object);
134       break;
135
136     case JSON_NODE_ARRAY:
137       if (node->data.array)
138         copy->data.array = json_array_ref (node->data.array);
139       break;
140
141     case JSON_NODE_VALUE:
142       if (G_VALUE_TYPE (&(node->data.value)) != G_TYPE_INVALID)
143         {
144           g_value_init (&(copy->data.value), G_VALUE_TYPE (&(node->data.value)));
145           g_value_copy (&(node->data.value), &(copy->data.value));
146         }
147       break;
148
149     case JSON_NODE_NULL:
150       break;
151
152     default:
153       g_assert_not_reached ();
154     }
155
156   return copy;
157 }
158
159 /**
160  * json_node_set_object:
161  * @node: a #JsonNode
162  * @object: a #JsonObject
163  *
164  * Sets @objects inside @node. The reference count of @object is increased.
165  */
166 void
167 json_node_set_object (JsonNode   *node,
168                       JsonObject *object)
169 {
170   g_return_if_fail (node != NULL);
171   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT);
172
173   if (node->data.object)
174     json_object_unref (node->data.object);
175
176   if (object)
177     node->data.object = json_object_ref (object);
178   else
179     node->data.object = NULL;
180 }
181
182 /**
183  * json_node_take_object:
184  * @node: a #JsonNode
185  * @object: (transfer full): a #JsonObject
186  *
187  * Sets @object inside @node. The reference count of @object is not increased.
188  */
189 void
190 json_node_take_object (JsonNode   *node,
191                        JsonObject *object)
192 {
193   g_return_if_fail (node != NULL);
194   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT);
195
196   if (node->data.object)
197     {
198       json_object_unref (node->data.object);
199       node->data.object = NULL;
200     }
201
202   if (object)
203     node->data.object = object;
204 }
205
206 /**
207  * json_node_get_object:
208  * @node: a #JsonNode
209  *
210  * Retrieves the #JsonObject stored inside a #JsonNode
211  *
212  * Return value: (transfer none): the #JsonObject
213  */
214 JsonObject *
215 json_node_get_object (JsonNode *node)
216 {
217   g_return_val_if_fail (node != NULL, NULL);
218   g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT, NULL);
219
220   return node->data.object;
221 }
222
223 /**
224  * json_node_dup_object:
225  * @node: a #JsonNode
226  *
227  * Retrieves the #JsonObject inside @node. The reference count of
228  * the returned object is increased.
229  *
230  * Return value: (transfer full): the #JsonObject
231  */
232 JsonObject *
233 json_node_dup_object (JsonNode *node)
234 {
235   g_return_val_if_fail (node != NULL, NULL);
236   g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT, NULL);
237
238   if (node->data.object)
239     return json_object_ref (node->data.object);
240   
241   return NULL;
242 }
243
244 /**
245  * json_node_set_array:
246  * @node: a #JsonNode
247  * @array: a #JsonArray
248  *
249  * Sets @array inside @node and increases the #JsonArray reference count
250  */
251 void
252 json_node_set_array (JsonNode  *node,
253                      JsonArray *array)
254 {
255   g_return_if_fail (node != NULL);
256   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY);
257
258   if (node->data.array)
259     json_array_unref (node->data.array);
260
261   if (array)
262     node->data.array = json_array_ref (array);
263   else
264     node->data.array = NULL;
265 }
266
267 /**
268  * json_node_take_array:
269  * @node: a #JsonNode
270  * @array: (transfer full): a #JsonArray
271  *
272  * Sets @array into @node without increasing the #JsonArray reference count.
273  */
274 void
275 json_node_take_array (JsonNode  *node,
276                       JsonArray *array)
277 {
278   g_return_if_fail (node != NULL);
279   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY);
280
281   if (node->data.array)
282     {
283       json_array_unref (node->data.array);
284       node->data.array = NULL;
285     }
286
287   if (array)
288     node->data.array = array;
289 }
290
291 /**
292  * json_node_get_array:
293  * @node: a #JsonNode
294  *
295  * Retrieves the #JsonArray stored inside a #JsonNode
296  *
297  * Return value: (transfer none): the #JsonArray
298  */
299 JsonArray *
300 json_node_get_array (JsonNode *node)
301 {
302   g_return_val_if_fail (node != NULL, NULL);
303   g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY, NULL);
304
305   return node->data.array;
306 }
307
308 /**
309  * json_node_dup_array
310  * @node: a #JsonNode
311  *
312  * Retrieves the #JsonArray stored inside a #JsonNode and returns it
313  * with its reference count increased by one.
314  *
315  * Return value: (transfer full): the #JsonArray with its reference
316  *   count increased.
317  */
318 JsonArray *
319 json_node_dup_array (JsonNode *node)
320 {
321   g_return_val_if_fail (node != NULL, NULL);
322   g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY, NULL);
323
324   if (node->data.array)
325     return json_array_ref (node->data.array);
326
327   return NULL;
328 }
329
330 /**
331  * json_node_get_value:
332  * @node: a #JsonNode
333  * @value: (out caller-allocates): return location for an uninitialized value
334  *
335  * Retrieves a value from a #JsonNode and copies into @value. When done
336  * using it, call g_value_unset() on the #GValue.
337  */
338 void
339 json_node_get_value (JsonNode *node,
340                      GValue   *value)
341 {
342   g_return_if_fail (node != NULL);
343   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
344
345   if (G_VALUE_TYPE (&(node->data.value)) != G_TYPE_INVALID)
346     {
347       g_value_init (value, G_VALUE_TYPE (&(node->data.value)));
348       g_value_copy (&(node->data.value), value);
349     }
350 }
351
352 static void inline
353 node_value_unset (JsonNode *node)
354 {
355   if (G_VALUE_TYPE (&(node->data.value)) != G_TYPE_INVALID)
356     g_value_unset (&(node->data.value));
357 }
358
359 /**
360  * json_node_set_value:
361  * @node: a #JsonNode
362  * @value: the #GValue to set
363  *
364  * Sets @value inside @node. The passed #GValue is copied into the #JsonNode
365  */
366 void
367 json_node_set_value (JsonNode     *node,
368                      const GValue *value)
369 {
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);
373
374   switch (G_VALUE_TYPE (value))
375     {
376     /* direct copy for the types we use */
377     case G_TYPE_INT64:
378     case G_TYPE_BOOLEAN:
379     case G_TYPE_DOUBLE:
380     case G_TYPE_STRING:
381       node_value_unset (node);
382       g_value_init (&(node->data.value), G_VALUE_TYPE (value));
383       g_value_copy (value, &(node->data.value));
384       break;
385
386     /* auto-promote ints to long longs */
387     case G_TYPE_INT:
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));
392       break;
393
394     /* auto-promote single precision to double precision */
395     case G_TYPE_FLOAT:
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));
400       break;
401
402     default:
403       g_warning ("Invalid value of type '%s'",
404                  g_type_name (G_VALUE_TYPE (value)));
405       return;
406     }
407
408 }
409
410 /**
411  * json_node_free:
412  * @node: a #JsonNode
413  *
414  * Frees the resources allocated by @node.
415  */
416 void
417 json_node_free (JsonNode *node)
418 {
419   if (G_LIKELY (node))
420     {
421       switch (node->type)
422         {
423         case JSON_NODE_OBJECT:
424           if (node->data.object)
425             json_object_unref (node->data.object);
426           break;
427
428         case JSON_NODE_ARRAY:
429           if (node->data.array)
430             json_array_unref (node->data.array);
431           break;
432
433         case JSON_NODE_VALUE:
434           g_value_unset (&(node->data.value));
435           break;
436
437         case JSON_NODE_NULL:
438           break;
439         }
440
441       g_slice_free (JsonNode, node);
442     }
443 }
444
445 /**
446  * json_node_type_name:
447  * @node: a #JsonNode
448  *
449  * Retrieves the user readable name of the data type contained by @node.
450  *
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
453  */
454 const gchar *
455 json_node_type_name (JsonNode *node)
456 {
457   g_return_val_if_fail (node != NULL, "(null)");
458
459   switch (node->type)
460     {
461     case JSON_NODE_OBJECT:
462     case JSON_NODE_ARRAY:
463     case JSON_NODE_NULL:
464       return json_node_type_get_name (node->type);
465
466     case JSON_NODE_VALUE:
467       return g_type_name (G_VALUE_TYPE (&(node->data.value)));
468     }
469
470   return "unknown";
471 }
472
473 const gchar *
474 json_node_type_get_name (JsonNodeType node_type)
475 {
476   switch (node_type)
477     {
478     case JSON_NODE_OBJECT:
479       return "JsonObject";
480
481     case JSON_NODE_ARRAY:
482       return "JsonArray";
483
484     case JSON_NODE_NULL:
485       return "NULL";
486
487     case JSON_NODE_VALUE:
488       return "Value";
489
490     default:
491       g_assert_not_reached ();
492       break;
493     }
494
495   return "unknown";
496 }
497
498 /**
499  * json_node_set_parent:
500  * @node: a #JsonNode
501  * @parent: (transfer none): the parent #JsonNode of @node
502  *
503  * Sets the parent #JsonNode of @node
504  *
505  * Since: 0.8
506  */
507 void
508 json_node_set_parent (JsonNode *node,
509                       JsonNode *parent)
510 {
511   g_return_if_fail (node != NULL);
512
513   node->parent = parent;
514 }
515
516 /**
517  * json_node_get_parent:
518  * @node: a #JsonNode
519  *
520  * Retrieves the parent #JsonNode of @node.
521  *
522  * Return value: (transfer none): the parent node, or %NULL if @node is
523  *   the root node
524  */
525 JsonNode *
526 json_node_get_parent (JsonNode *node)
527 {
528   g_return_val_if_fail (node != NULL, NULL);
529
530   return node->parent;
531 }
532
533 /**
534  * json_node_set_string:
535  * @node: a #JsonNode of type %JSON_NODE_VALUE
536  * @value: a string value
537  *
538  * Sets @value as the string content of the @node, replacing any existing
539  * content.
540  */
541 void
542 json_node_set_string (JsonNode    *node,
543                       const gchar *value)
544 {
545   g_return_if_fail (node != NULL);
546   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
547
548   if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_STRING)
549     g_value_set_string (&(node->data.value), value);
550   else
551     {
552       GValue copy = { 0, };
553
554       g_value_init (&copy, G_TYPE_STRING);
555       g_value_set_string (&copy, value);
556
557       json_node_set_value (node, &copy);
558
559       g_value_unset (&copy);
560     }
561 }
562
563 /**
564  * json_node_get_string:
565  * @node: a #JsonNode of type %JSON_NODE_VALUE
566  *
567  * Gets the string value stored inside a #JsonNode
568  *
569  * Return value: a string value.
570  */
571 const gchar *
572 json_node_get_string (JsonNode *node)
573 {
574   g_return_val_if_fail (node != NULL, NULL);
575
576   if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
577     return NULL;
578
579   if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_STRING)
580     return g_value_get_string (&(node->data.value));
581
582   return NULL;
583 }
584
585 /**
586  * json_node_dup_string:
587  * @node: a #JsonNode of type %JSON_NODE_VALUE
588  *
589  * Gets a copy of the string value stored inside a #JsonNode
590  *
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
593  */
594 gchar *
595 json_node_dup_string (JsonNode *node)
596 {
597   g_return_val_if_fail (node != NULL, NULL);
598
599   if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
600     return NULL;
601
602   if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_STRING)
603     return g_value_dup_string (&(node->data.value));
604
605   return NULL;
606 }
607
608 /**
609  * json_node_set_int:
610  * @node: a #JsonNode of type %JSON_NODE_VALUE
611  * @value: an integer value
612  *
613  * Sets @value as the integer content of the @node, replacing any existing
614  * content.
615  */
616 void
617 json_node_set_int (JsonNode *node,
618                    gint64    value)
619 {
620   g_return_if_fail (node != NULL);
621   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
622
623   if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_INT64)
624     g_value_set_int64 (&(node->data.value), value);
625   else
626     {
627       GValue copy = { 0, };
628
629       g_value_init (&copy, G_TYPE_INT64);
630       g_value_set_int64 (&copy, value);
631
632       json_node_set_value (node, &copy);
633
634       g_value_unset (&copy);
635     }
636 }
637
638 /**
639  * json_node_get_int:
640  * @node: a #JsonNode of type %JSON_NODE_VALUE
641  *
642  * Gets the integer value stored inside a #JsonNode
643  *
644  * Return value: an integer value.
645  */
646 gint64
647 json_node_get_int (JsonNode *node)
648 {
649   g_return_val_if_fail (node != NULL, 0);
650
651   if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
652     return 0;
653
654   if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_INT64)
655     return g_value_get_int64 (&(node->data.value));
656
657   return 0;
658 }
659
660 /**
661  * json_node_set_double:
662  * @node: a #JsonNode of type %JSON_NODE_VALUE
663  * @value: a double value
664  *
665  * Sets @value as the double content of the @node, replacing any existing
666  * content.
667  */
668 void
669 json_node_set_double (JsonNode *node,
670                       gdouble   value)
671 {
672   g_return_if_fail (node != NULL);
673   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
674
675   if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_DOUBLE)
676     g_value_set_double (&(node->data.value), value);
677   else
678     {
679       GValue copy = { 0, };
680
681       g_value_init (&copy, G_TYPE_DOUBLE);
682       g_value_set_double (&copy, value);
683
684       json_node_set_value (node, &copy);
685
686       g_value_unset (&copy);
687     }
688 }
689
690 /**
691  * json_node_get_double:
692  * @node: a #JsonNode of type %JSON_NODE_VALUE
693  *
694  * Gets the double value stored inside a #JsonNode
695  *
696  * Return value: a double value.
697  */
698 gdouble
699 json_node_get_double (JsonNode *node)
700 {
701   g_return_val_if_fail (node != NULL, 0.0);
702
703   if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
704     return 0;
705
706   if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_DOUBLE)
707     return g_value_get_double (&(node->data.value));
708
709   return 0.0;
710 }
711
712 /**
713  * json_node_set_boolean:
714  * @node: a #JsonNode of type %JSON_NODE_VALUE
715  * @value: a boolean value
716  *
717  * Sets @value as the boolean content of the @node, replacing any existing
718  * content.
719  */
720 void
721 json_node_set_boolean (JsonNode *node,
722                        gboolean  value)
723 {
724   g_return_if_fail (node != NULL);
725   g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
726
727   if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_BOOLEAN)
728     g_value_set_boolean (&(node->data.value), value);
729   else
730     {
731       GValue copy = { 0, };
732
733       g_value_init (&copy, G_TYPE_BOOLEAN);
734       g_value_set_boolean (&copy, value);
735
736       json_node_set_value (node, &copy);
737
738       g_value_unset (&copy);
739     }
740 }
741
742 /**
743  * json_node_get_boolean:
744  * @node: a #JsonNode of type %JSON_NODE_VALUE
745  *
746  * Gets the boolean value stored inside a #JsonNode
747  *
748  * Return value: a boolean value.
749  */
750 gboolean
751 json_node_get_boolean (JsonNode *node)
752 {
753   g_return_val_if_fail (node != NULL, FALSE);
754
755   if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
756     return FALSE;
757
758   if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_BOOLEAN)
759     return g_value_get_boolean (&(node->data.value));
760
761   return FALSE;
762 }
763
764 /**
765  * json_node_get_node_type:
766  * @node: a #JsonNode
767  *
768  * Retrieves the #JsonNodeType of @node
769  *
770  * Return value: the type of the node
771  *
772  * Since: 0.8
773  */
774 JsonNodeType
775 json_node_get_node_type (JsonNode *node)
776 {
777   g_return_val_if_fail (node != NULL, JSON_NODE_NULL);
778
779   return node->type;
780 }
781
782 /**
783  * json_node_is_null:
784  * @node: a #JsonNode
785  *
786  * Checks whether @node is a %JSON_NODE_NULL
787  *
788  * <note>A null node is not the same as a %NULL #JsonNode</note>
789  *
790  * Return value: %TRUE if the node is null
791  *
792  * Since: 0.8
793  */
794 gboolean
795 json_node_is_null (JsonNode *node)
796 {
797   g_return_val_if_fail (node != NULL, TRUE);
798
799   return node->type == JSON_NODE_NULL;
800 }