Merge branch '1.1'
[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_nocheck(json_t *json, const char *key, json_t *value)
124 {
125     return json_object_set_new_nocheck(json, key, json_incref(value));
126 }
127
128 int json_object_set_new(json_t *json, const char *key, json_t *value)
129 {
130     if(!key || !utf8_check_string(key, -1))
131     {
132         json_decref(value);
133         return -1;
134     }
135
136     return json_object_set_new_nocheck(json, key, value);
137 }
138
139 int json_object_del(json_t *json, const char *key)
140 {
141     json_object_t *object;
142
143     if(!json_is_object(json))
144         return -1;
145
146     object = json_to_object(json);
147     return hashtable_del(&object->hashtable, key);
148 }
149
150 int json_object_clear(json_t *json)
151 {
152     json_object_t *object;
153
154     if(!json_is_object(json))
155         return -1;
156
157     object = json_to_object(json);
158     hashtable_clear(&object->hashtable);
159
160     return 0;
161 }
162
163 int json_object_update(json_t *object, json_t *other)
164 {
165     void *iter;
166
167     if(!json_is_object(object) || !json_is_object(other))
168         return -1;
169
170     iter = json_object_iter(other);
171     while(iter) {
172         const char *key;
173         json_t *value;
174
175         key = json_object_iter_key(iter);
176         value = json_object_iter_value(iter);
177
178         if(json_object_set(object, key, value))
179             return -1;
180
181         iter = json_object_iter_next(other, iter);
182     }
183
184     return 0;
185 }
186
187 void *json_object_iter(json_t *json)
188 {
189     json_object_t *object;
190
191     if(!json_is_object(json))
192         return NULL;
193
194     object = json_to_object(json);
195     return hashtable_iter(&object->hashtable);
196 }
197
198 void *json_object_iter_next(json_t *json, void *iter)
199 {
200     json_object_t *object;
201
202     if(!json_is_object(json) || iter == NULL)
203         return NULL;
204
205     object = json_to_object(json);
206     return hashtable_iter_next(&object->hashtable, iter);
207 }
208
209 const char *json_object_iter_key(void *iter)
210 {
211     if(!iter)
212         return NULL;
213
214     return (const char *)hashtable_iter_key(iter);
215 }
216
217 json_t *json_object_iter_value(void *iter)
218 {
219     if(!iter)
220         return NULL;
221
222     return (json_t *)hashtable_iter_value(iter);
223 }
224
225
226 /*** array ***/
227
228 json_t *json_array(void)
229 {
230     json_array_t *array = malloc(sizeof(json_array_t));
231     if(!array)
232         return NULL;
233     json_init(&array->json, JSON_ARRAY);
234
235     array->entries = 0;
236     array->size = 8;
237
238     array->table = malloc(array->size * sizeof(json_t *));
239     if(!array->table) {
240         free(array);
241         return NULL;
242     }
243
244     array->visited = 0;
245
246     return &array->json;
247 }
248
249 static void json_delete_array(json_array_t *array)
250 {
251     unsigned int i;
252
253     for(i = 0; i < array->entries; i++)
254         json_decref(array->table[i]);
255
256     free(array->table);
257     free(array);
258 }
259
260 unsigned int json_array_size(const json_t *json)
261 {
262     if(!json_is_array(json))
263         return 0;
264
265     return json_to_array(json)->entries;
266 }
267
268 json_t *json_array_get(const json_t *json, unsigned int index)
269 {
270     json_array_t *array;
271     if(!json_is_array(json))
272         return NULL;
273     array = json_to_array(json);
274
275     if(index >= array->entries)
276         return NULL;
277
278     return array->table[index];
279 }
280
281 int json_array_set_new(json_t *json, unsigned int index, json_t *value)
282 {
283     json_array_t *array;
284
285     if(!value)
286         return -1;
287
288     if(!json_is_array(json) || json == value)
289     {
290         json_decref(value);
291         return -1;
292     }
293     array = json_to_array(json);
294
295     if(index >= array->entries)
296     {
297         json_decref(value);
298         return -1;
299     }
300
301     json_decref(array->table[index]);
302     array->table[index] = value;
303
304     return 0;
305 }
306
307 static void array_move(json_array_t *array, unsigned int dest,
308                        unsigned int src, unsigned int count)
309 {
310     memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
311 }
312
313 static void array_copy(json_t **dest, unsigned int dpos,
314                        json_t **src, unsigned int spos,
315                        unsigned int count)
316 {
317     memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
318 }
319
320 static json_t **json_array_grow(json_array_t *array,
321                                 unsigned int amount,
322                                 int copy)
323 {
324     unsigned int new_size;
325     json_t **old_table, **new_table;
326
327     if(array->entries + amount <= array->size)
328         return array->table;
329
330     old_table = array->table;
331
332     new_size = max(array->size + amount, array->size * 2);
333     new_table = malloc(new_size * sizeof(json_t *));
334     if(!new_table)
335         return NULL;
336
337     array->size = new_size;
338     array->table = new_table;
339
340     if(copy) {
341         array_copy(array->table, 0, old_table, 0, array->entries);
342         free(old_table);
343         return array->table;
344     }
345
346     return old_table;
347 }
348
349 int json_array_append_new(json_t *json, json_t *value)
350 {
351     json_array_t *array;
352
353     if(!value)
354         return -1;
355
356     if(!json_is_array(json) || json == value)
357     {
358         json_decref(value);
359         return -1;
360     }
361     array = json_to_array(json);
362
363     if(!json_array_grow(array, 1, 1)) {
364         json_decref(value);
365         return -1;
366     }
367
368     array->table[array->entries] = value;
369     array->entries++;
370
371     return 0;
372 }
373
374 int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
375 {
376     json_array_t *array;
377     json_t **old_table;
378
379     if(!value)
380         return -1;
381
382     if(!json_is_array(json) || json == value) {
383         json_decref(value);
384         return -1;
385     }
386     array = json_to_array(json);
387
388     if(index > array->entries) {
389         json_decref(value);
390         return -1;
391     }
392
393     old_table = json_array_grow(array, 1, 0);
394     if(!old_table) {
395         json_decref(value);
396         return -1;
397     }
398
399     if(old_table != array->table) {
400         array_copy(array->table, 0, old_table, 0, index);
401         array_copy(array->table, index + 1, old_table, index,
402                    array->entries - index);
403         free(old_table);
404     }
405     else
406         array_move(array, index + 1, index, array->entries - index);
407
408     array->table[index] = value;
409     array->entries++;
410
411     return 0;
412 }
413
414 int json_array_remove(json_t *json, unsigned int index)
415 {
416     json_array_t *array;
417
418     if(!json_is_array(json))
419         return -1;
420     array = json_to_array(json);
421
422     if(index >= array->entries)
423         return -1;
424
425     json_decref(array->table[index]);
426
427     array_move(array, index, index + 1, array->entries - index);
428     array->entries--;
429
430     return 0;
431 }
432
433 int json_array_clear(json_t *json)
434 {
435     json_array_t *array;
436     unsigned int i;
437
438     if(!json_is_array(json))
439         return -1;
440     array = json_to_array(json);
441
442     for(i = 0; i < array->entries; i++)
443         json_decref(array->table[i]);
444
445     array->entries = 0;
446     return 0;
447 }
448
449 int json_array_extend(json_t *json, json_t *other_json)
450 {
451     json_array_t *array, *other;
452     unsigned int i;
453
454     if(!json_is_array(json) || !json_is_array(other_json))
455         return -1;
456     array = json_to_array(json);
457     other = json_to_array(other_json);
458
459     if(!json_array_grow(array, other->entries, 1))
460         return -1;
461
462     for(i = 0; i < other->entries; i++)
463         json_incref(other->table[i]);
464
465     array_copy(array->table, array->entries, other->table, 0, other->entries);
466
467     array->entries += other->entries;
468     return 0;
469 }
470
471
472 /*** string ***/
473
474 json_t *json_string_nocheck(const char *value)
475 {
476     json_string_t *string;
477
478     if(!value)
479         return NULL;
480
481     string = malloc(sizeof(json_string_t));
482     if(!string)
483         return NULL;
484     json_init(&string->json, JSON_STRING);
485
486     string->value = strdup(value);
487     if(!string->value) {
488         free(string);
489         return NULL;
490     }
491
492     return &string->json;
493 }
494
495 json_t *json_string(const char *value)
496 {
497     if(!value || !utf8_check_string(value, -1))
498         return NULL;
499
500     return json_string_nocheck(value);
501 }
502
503 const char *json_string_value(const json_t *json)
504 {
505     if(!json_is_string(json))
506         return NULL;
507
508     return json_to_string(json)->value;
509 }
510
511 int json_string_set(json_t *json, const char *value)
512 {
513     char *dup;
514     json_string_t *string;
515
516     if(!json_is_string(json) || !value || !utf8_check_string(value, -1))
517         return -1;
518
519     dup = strdup(value);
520     if(!dup)
521         return -1;
522
523     string = json_to_string(json);
524     free(string->value);
525     string->value = dup;
526
527     return 0;
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 }