0ab82325cc66ebd94e8d741442d38073b11223cf
[jansson.git] / src / value.c
1 /*
2  * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
3  *
4  * Jansson is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7
8 #define _GNU_SOURCE
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include <jansson.h>
13 #include "hashtable.h"
14 #include "jansson_private.h"
15 #include "utf.h"
16 #include "util.h"
17
18
19 static inline void json_init(json_t *json, json_type type)
20 {
21     json->type = type;
22     json->refcount = 1;
23 }
24
25
26 /*** object ***/
27
28 static unsigned int hash_string(const void *key)
29 {
30     const char *str = (const char *)key;
31     unsigned int hash = 5381;
32     unsigned int c;
33
34     while((c = (unsigned int)*str))
35     {
36         hash = ((hash << 5) + hash) + c;
37         str++;
38     }
39
40     return hash;
41 }
42
43 static int string_equal(const void *key1, const void *key2)
44 {
45     return strcmp((const char *)key1, (const char *)key2) == 0;
46 }
47
48 static void value_decref(void *value)
49 {
50     json_decref((json_t *)value);
51 }
52
53 json_t *json_object(void)
54 {
55     json_object_t *object = malloc(sizeof(json_object_t));
56     if(!object)
57         return NULL;
58     json_init(&object->json, JSON_OBJECT);
59
60     if(hashtable_init(&object->hashtable, hash_string, string_equal,
61                       free, value_decref))
62     {
63         free(object);
64         return NULL;
65     }
66
67     object->visited = 0;
68
69     return &object->json;
70 }
71
72 static void json_delete_object(json_object_t *object)
73 {
74     hashtable_close(&object->hashtable);
75     free(object);
76 }
77
78 unsigned int json_object_size(const json_t *json)
79 {
80     json_object_t *object;
81
82     if(!json_is_object(json))
83         return -1;
84
85     object = json_to_object(json);
86     return object->hashtable.size;
87 }
88
89 json_t *json_object_get(const json_t *json, const char *key)
90 {
91     json_object_t *object;
92
93     if(!json_is_object(json))
94         return NULL;
95
96     object = json_to_object(json);
97     return hashtable_get(&object->hashtable, key);
98 }
99
100 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
101 {
102     json_object_t *object;
103
104     if(!key || !value)
105         return -1;
106
107     if(!json_is_object(json) || json == value)
108     {
109         json_decref(value);
110         return -1;
111     }
112     object = json_to_object(json);
113
114     if(hashtable_set(&object->hashtable, strdup(key), value))
115     {
116         json_decref(value);
117         return -1;
118     }
119
120     return 0;
121 }
122
123 int json_object_set_new(json_t *json, const char *key, json_t *value)
124 {
125     if(!key || !utf8_check_string(key, -1))
126     {
127         json_decref(value);
128         return -1;
129     }
130
131     return json_object_set_new_nocheck(json, key, value);
132 }
133
134 int json_object_del(json_t *json, const char *key)
135 {
136     json_object_t *object;
137
138     if(!json_is_object(json))
139         return -1;
140
141     object = json_to_object(json);
142     return hashtable_del(&object->hashtable, key);
143 }
144
145 int json_object_clear(json_t *json)
146 {
147     json_object_t *object;
148
149     if(!json_is_object(json))
150         return -1;
151
152     object = json_to_object(json);
153     hashtable_clear(&object->hashtable);
154
155     return 0;
156 }
157
158 int json_object_update(json_t *object, json_t *other)
159 {
160     void *iter;
161
162     if(!json_is_object(object) || !json_is_object(other))
163         return -1;
164
165     iter = json_object_iter(other);
166     while(iter) {
167         const char *key;
168         json_t *value;
169
170         key = json_object_iter_key(iter);
171         value = json_object_iter_value(iter);
172
173         if(json_object_set(object, key, value))
174             return -1;
175
176         iter = json_object_iter_next(other, iter);
177     }
178
179     return 0;
180 }
181
182 void *json_object_iter(json_t *json)
183 {
184     json_object_t *object;
185
186     if(!json_is_object(json))
187         return NULL;
188
189     object = json_to_object(json);
190     return hashtable_iter(&object->hashtable);
191 }
192
193 void *json_object_iter_next(json_t *json, void *iter)
194 {
195     json_object_t *object;
196
197     if(!json_is_object(json) || iter == NULL)
198         return NULL;
199
200     object = json_to_object(json);
201     return hashtable_iter_next(&object->hashtable, iter);
202 }
203
204 const char *json_object_iter_key(void *iter)
205 {
206     if(!iter)
207         return NULL;
208
209     return (const char *)hashtable_iter_key(iter);
210 }
211
212 json_t *json_object_iter_value(void *iter)
213 {
214     if(!iter)
215         return NULL;
216
217     return (json_t *)hashtable_iter_value(iter);
218 }
219
220
221 /*** array ***/
222
223 json_t *json_array(void)
224 {
225     json_array_t *array = malloc(sizeof(json_array_t));
226     if(!array)
227         return NULL;
228     json_init(&array->json, JSON_ARRAY);
229
230     array->entries = 0;
231     array->size = 8;
232
233     array->table = malloc(array->size * sizeof(json_t *));
234     if(!array->table) {
235         free(array);
236         return NULL;
237     }
238
239     array->visited = 0;
240
241     return &array->json;
242 }
243
244 static void json_delete_array(json_array_t *array)
245 {
246     unsigned int i;
247
248     for(i = 0; i < array->entries; i++)
249         json_decref(array->table[i]);
250
251     free(array->table);
252     free(array);
253 }
254
255 unsigned int json_array_size(const json_t *json)
256 {
257     if(!json_is_array(json))
258         return 0;
259
260     return json_to_array(json)->entries;
261 }
262
263 json_t *json_array_get(const json_t *json, unsigned int index)
264 {
265     json_array_t *array;
266     if(!json_is_array(json))
267         return NULL;
268     array = json_to_array(json);
269
270     if(index >= array->entries)
271         return NULL;
272
273     return array->table[index];
274 }
275
276 int json_array_set_new(json_t *json, unsigned int index, json_t *value)
277 {
278     json_array_t *array;
279
280     if(!value)
281         return -1;
282
283     if(!json_is_array(json) || json == value)
284     {
285         json_decref(value);
286         return -1;
287     }
288     array = json_to_array(json);
289
290     if(index >= array->entries)
291     {
292         json_decref(value);
293         return -1;
294     }
295
296     json_decref(array->table[index]);
297     array->table[index] = value;
298
299     return 0;
300 }
301
302 static void array_move(json_array_t *array, unsigned int dest,
303                        unsigned int src, unsigned int count)
304 {
305     memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
306 }
307
308 static void array_copy(json_t **dest, unsigned int dpos,
309                        json_t **src, unsigned int spos,
310                        unsigned int count)
311 {
312     memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
313 }
314
315 static json_t **json_array_grow(json_array_t *array,
316                                 unsigned int amount,
317                                 int copy)
318 {
319     unsigned int new_size;
320     json_t **old_table, **new_table;
321
322     if(array->entries + amount <= array->size)
323         return array->table;
324
325     old_table = array->table;
326
327     new_size = max(array->size + amount, array->size * 2);
328     new_table = malloc(new_size * sizeof(json_t *));
329     if(!new_table)
330         return NULL;
331
332     array->size = new_size;
333     array->table = new_table;
334
335     if(copy) {
336         array_copy(array->table, 0, old_table, 0, array->entries);
337         free(old_table);
338         return array->table;
339     }
340
341     return old_table;
342 }
343
344 int json_array_append_new(json_t *json, json_t *value)
345 {
346     json_array_t *array;
347
348     if(!value)
349         return -1;
350
351     if(!json_is_array(json) || json == value)
352     {
353         json_decref(value);
354         return -1;
355     }
356     array = json_to_array(json);
357
358     if(!json_array_grow(array, 1, 1)) {
359         json_decref(value);
360         return -1;
361     }
362
363     array->table[array->entries] = value;
364     array->entries++;
365
366     return 0;
367 }
368
369 int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
370 {
371     json_array_t *array;
372     json_t **old_table;
373
374     if(!value)
375         return -1;
376
377     if(!json_is_array(json) || json == value) {
378         json_decref(value);
379         return -1;
380     }
381     array = json_to_array(json);
382
383     if(index > array->entries) {
384         json_decref(value);
385         return -1;
386     }
387
388     old_table = json_array_grow(array, 1, 0);
389     if(!old_table) {
390         json_decref(value);
391         return -1;
392     }
393
394     if(old_table != array->table) {
395         array_copy(array->table, 0, old_table, 0, index);
396         array_copy(array->table, index + 1, old_table, index,
397                    array->entries - index);
398         free(old_table);
399     }
400     else
401         array_move(array, index + 1, index, array->entries - index);
402
403     array->table[index] = value;
404     array->entries++;
405
406     return 0;
407 }
408
409 int json_array_remove(json_t *json, unsigned int index)
410 {
411     json_array_t *array;
412
413     if(!json_is_array(json))
414         return -1;
415     array = json_to_array(json);
416
417     if(index >= array->entries)
418         return -1;
419
420     json_decref(array->table[index]);
421
422     array_move(array, index, index + 1, array->entries - index);
423     array->entries--;
424
425     return 0;
426 }
427
428 int json_array_clear(json_t *json)
429 {
430     json_array_t *array;
431     unsigned int i;
432
433     if(!json_is_array(json))
434         return -1;
435     array = json_to_array(json);
436
437     for(i = 0; i < array->entries; i++)
438         json_decref(array->table[i]);
439
440     array->entries = 0;
441     return 0;
442 }
443
444 int json_array_extend(json_t *json, json_t *other_json)
445 {
446     json_array_t *array, *other;
447     unsigned int i;
448
449     if(!json_is_array(json) || !json_is_array(other_json))
450         return -1;
451     array = json_to_array(json);
452     other = json_to_array(other_json);
453
454     if(!json_array_grow(array, other->entries, 1))
455         return -1;
456
457     for(i = 0; i < other->entries; i++)
458         json_incref(other->table[i]);
459
460     array_copy(array->table, array->entries, other->table, 0, other->entries);
461
462     array->entries += other->entries;
463     return 0;
464 }
465
466
467 /*** string ***/
468
469 json_t *json_string_nocheck(const char *value)
470 {
471     json_string_t *string;
472
473     if(!value)
474         return NULL;
475
476     string = malloc(sizeof(json_string_t));
477     if(!string)
478         return NULL;
479     json_init(&string->json, JSON_STRING);
480
481     string->value = strdup(value);
482     if(!string->value) {
483         free(string);
484         return NULL;
485     }
486
487     return &string->json;
488 }
489
490 json_t *json_string(const char *value)
491 {
492     if(!value || !utf8_check_string(value, -1))
493         return NULL;
494
495     return json_string_nocheck(value);
496 }
497
498 const char *json_string_value(const json_t *json)
499 {
500     if(!json_is_string(json))
501         return NULL;
502
503     return json_to_string(json)->value;
504 }
505
506 int json_string_set_nocheck(json_t *json, const char *value)
507 {
508     char *dup;
509     json_string_t *string;
510
511     dup = strdup(value);
512     if(!dup)
513         return -1;
514
515     string = json_to_string(json);
516     free(string->value);
517     string->value = dup;
518
519     return 0;
520 }
521
522 int json_string_set(json_t *json, const char *value)
523 {
524     if(!value || !utf8_check_string(value, -1))
525         return -1;
526
527     return json_string_set_nocheck(json, value);
528 }
529
530 static void json_delete_string(json_string_t *string)
531 {
532     free(string->value);
533     free(string);
534 }
535
536
537 /*** integer ***/
538
539 json_t *json_integer(int value)
540 {
541     json_integer_t *integer = malloc(sizeof(json_integer_t));
542     if(!integer)
543         return NULL;
544     json_init(&integer->json, JSON_INTEGER);
545
546     integer->value = value;
547     return &integer->json;
548 }
549
550 int json_integer_value(const json_t *json)
551 {
552     if(!json_is_integer(json))
553         return 0;
554
555     return json_to_integer(json)->value;
556 }
557
558 int json_integer_set(json_t *json, int value)
559 {
560     if(!json_is_integer(json))
561         return -1;
562
563     json_to_integer(json)->value = value;
564
565     return 0;
566 }
567
568 static void json_delete_integer(json_integer_t *integer)
569 {
570     free(integer);
571 }
572
573
574 /*** real ***/
575
576 json_t *json_real(double value)
577 {
578     json_real_t *real = malloc(sizeof(json_real_t));
579     if(!real)
580         return NULL;
581     json_init(&real->json, JSON_REAL);
582
583     real->value = value;
584     return &real->json;
585 }
586
587 double json_real_value(const json_t *json)
588 {
589     if(!json_is_real(json))
590         return 0;
591
592     return json_to_real(json)->value;
593 }
594
595 int json_real_set(json_t *json, double value)
596 {
597     if(!json_is_real(json))
598         return 0;
599
600     json_to_real(json)->value = value;
601
602     return 0;
603 }
604
605 static void json_delete_real(json_real_t *real)
606 {
607     free(real);
608 }
609
610
611 /*** number ***/
612
613 double json_number_value(const json_t *json)
614 {
615     if(json_is_integer(json))
616         return json_integer_value(json);
617     else if(json_is_real(json))
618         return json_real_value(json);
619     else
620         return 0.0;
621 }
622
623
624 /*** simple values ***/
625
626 json_t *json_true(void)
627 {
628     static json_t the_true = {
629         .type = JSON_TRUE,
630         .refcount = (unsigned int)1
631     };
632     return &the_true;
633 }
634
635
636 json_t *json_false(void)
637 {
638     static json_t the_false = {
639         .type = JSON_FALSE,
640         .refcount = (unsigned int)1
641     };
642     return &the_false;
643 }
644
645
646 json_t *json_null(void)
647 {
648     static json_t the_null = {
649         .type = JSON_NULL,
650         .refcount = (unsigned int)1
651     };
652     return &the_null;
653 }
654
655
656 /*** deletion ***/
657
658 void json_delete(json_t *json)
659 {
660     if(json_is_object(json))
661         json_delete_object(json_to_object(json));
662
663     else if(json_is_array(json))
664         json_delete_array(json_to_array(json));
665
666     else if(json_is_string(json))
667         json_delete_string(json_to_string(json));
668
669     else if(json_is_integer(json))
670         json_delete_integer(json_to_integer(json));
671
672     else if(json_is_real(json))
673         json_delete_real(json_to_real(json));
674
675     /* json_delete is not called for true, false or null */
676 }