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