2
* Copyright (c) 2009-2012 Petri Lehtinen <petri@digip.org>
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.
16
#include "hashtable.h"
17
#include "jansson_private.h"
20
/* Work around nonstandard isnan() and isinf() implementations */
22
static JSON_INLINE int jansson_isnan(double x) { return x != x; }
23
# define isnan jansson_isnan
26
static JSON_INLINE int jansson_isinf(double x) { return !isnan(x) && isnan(x - x); }
27
# define isinf jansson_isinf
30
static JSON_INLINE void json_init(json_t *json, json_type type)
39
json_t *json_object(void)
41
json_object_t *object = jsonp_malloc(sizeof(json_object_t));
44
json_init(&object->json, JSON_OBJECT);
46
if(hashtable_init(&object->hashtable))
58
static void json_delete_object(json_object_t *object)
60
hashtable_close(&object->hashtable);
64
size_t json_object_size(const json_t *json)
66
json_object_t *object;
68
if(!json_is_object(json))
71
object = json_to_object(json);
72
return object->hashtable.size;
75
json_t *json_object_get(const json_t *json, const char *key)
77
json_object_t *object;
79
if(!json_is_object(json))
82
object = json_to_object(json);
83
return hashtable_get(&object->hashtable, key);
86
int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
88
json_object_t *object;
93
if(!key || !json_is_object(json) || json == value)
98
object = json_to_object(json);
100
if(hashtable_set(&object->hashtable, key, object->serial++, value))
109
int json_object_set_new(json_t *json, const char *key, json_t *value)
111
if(!key || !utf8_check_string(key, -1))
117
return json_object_set_new_nocheck(json, key, value);
120
int json_object_del(json_t *json, const char *key)
122
json_object_t *object;
124
if(!json_is_object(json))
127
object = json_to_object(json);
128
return hashtable_del(&object->hashtable, key);
131
int json_object_clear(json_t *json)
133
json_object_t *object;
135
if(!json_is_object(json))
138
object = json_to_object(json);
140
hashtable_clear(&object->hashtable);
146
int json_object_update(json_t *object, json_t *other)
151
if(!json_is_object(object) || !json_is_object(other))
154
json_object_foreach(other, key, value) {
155
if(json_object_set_nocheck(object, key, value))
162
int json_object_update_existing(json_t *object, json_t *other)
167
if(!json_is_object(object) || !json_is_object(other))
170
json_object_foreach(other, key, value) {
171
if(json_object_get(object, key))
172
json_object_set_nocheck(object, key, value);
178
int json_object_update_missing(json_t *object, json_t *other)
183
if(!json_is_object(object) || !json_is_object(other))
186
json_object_foreach(other, key, value) {
187
if(!json_object_get(object, key))
188
json_object_set_nocheck(object, key, value);
194
void *json_object_iter(json_t *json)
196
json_object_t *object;
198
if(!json_is_object(json))
201
object = json_to_object(json);
202
return hashtable_iter(&object->hashtable);
205
void *json_object_iter_at(json_t *json, const char *key)
207
json_object_t *object;
209
if(!key || !json_is_object(json))
212
object = json_to_object(json);
213
return hashtable_iter_at(&object->hashtable, key);
216
void *json_object_iter_next(json_t *json, void *iter)
218
json_object_t *object;
220
if(!json_is_object(json) || iter == NULL)
223
object = json_to_object(json);
224
return hashtable_iter_next(&object->hashtable, iter);
227
const char *json_object_iter_key(void *iter)
232
return hashtable_iter_key(iter);
235
json_t *json_object_iter_value(void *iter)
240
return (json_t *)hashtable_iter_value(iter);
243
int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
245
if(!json_is_object(json) || !iter || !value)
248
hashtable_iter_set(iter, value);
252
void *json_object_key_to_iter(const char *key)
257
return hashtable_key_to_iter(key);
260
static int json_object_equal(json_t *object1, json_t *object2)
263
json_t *value1, *value2;
265
if(json_object_size(object1) != json_object_size(object2))
268
json_object_foreach(object1, key, value1) {
269
value2 = json_object_get(object2, key);
271
if(!json_equal(value1, value2))
278
static json_t *json_object_copy(json_t *object)
285
result = json_object();
289
json_object_foreach(object, key, value)
290
json_object_set_nocheck(result, key, value);
295
static json_t *json_object_deep_copy(json_t *object)
302
result = json_object();
306
json_object_foreach(object, key, value)
307
json_object_set_new_nocheck(result, key, json_deep_copy(value));
315
json_t *json_array(void)
317
json_array_t *array = jsonp_malloc(sizeof(json_array_t));
320
json_init(&array->json, JSON_ARRAY);
325
array->table = jsonp_malloc(array->size * sizeof(json_t *));
336
static void json_delete_array(json_array_t *array)
340
for(i = 0; i < array->entries; i++)
341
json_decref(array->table[i]);
343
jsonp_free(array->table);
347
size_t json_array_size(const json_t *json)
349
if(!json_is_array(json))
352
return json_to_array(json)->entries;
355
json_t *json_array_get(const json_t *json, size_t index)
358
if(!json_is_array(json))
360
array = json_to_array(json);
362
if(index >= array->entries)
365
return array->table[index];
368
int json_array_set_new(json_t *json, size_t index, json_t *value)
375
if(!json_is_array(json) || json == value)
380
array = json_to_array(json);
382
if(index >= array->entries)
388
json_decref(array->table[index]);
389
array->table[index] = value;
394
static void array_move(json_array_t *array, size_t dest,
395
size_t src, size_t count)
397
memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
400
static void array_copy(json_t **dest, size_t dpos,
401
json_t **src, size_t spos,
404
memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
407
static json_t **json_array_grow(json_array_t *array,
412
json_t **old_table, **new_table;
414
if(array->entries + amount <= array->size)
417
old_table = array->table;
419
new_size = max(array->size + amount, array->size * 2);
420
new_table = jsonp_malloc(new_size * sizeof(json_t *));
424
array->size = new_size;
425
array->table = new_table;
428
array_copy(array->table, 0, old_table, 0, array->entries);
429
jsonp_free(old_table);
436
int json_array_append_new(json_t *json, json_t *value)
443
if(!json_is_array(json) || json == value)
448
array = json_to_array(json);
450
if(!json_array_grow(array, 1, 1)) {
455
array->table[array->entries] = value;
461
int json_array_insert_new(json_t *json, size_t index, json_t *value)
469
if(!json_is_array(json) || json == value) {
473
array = json_to_array(json);
475
if(index > array->entries) {
480
old_table = json_array_grow(array, 1, 0);
486
if(old_table != array->table) {
487
array_copy(array->table, 0, old_table, 0, index);
488
array_copy(array->table, index + 1, old_table, index,
489
array->entries - index);
490
jsonp_free(old_table);
493
array_move(array, index + 1, index, array->entries - index);
495
array->table[index] = value;
501
int json_array_remove(json_t *json, size_t index)
505
if(!json_is_array(json))
507
array = json_to_array(json);
509
if(index >= array->entries)
512
json_decref(array->table[index]);
514
array_move(array, index, index + 1, array->entries - index);
520
int json_array_clear(json_t *json)
525
if(!json_is_array(json))
527
array = json_to_array(json);
529
for(i = 0; i < array->entries; i++)
530
json_decref(array->table[i]);
536
int json_array_extend(json_t *json, json_t *other_json)
538
json_array_t *array, *other;
541
if(!json_is_array(json) || !json_is_array(other_json))
543
array = json_to_array(json);
544
other = json_to_array(other_json);
546
if(!json_array_grow(array, other->entries, 1))
549
for(i = 0; i < other->entries; i++)
550
json_incref(other->table[i]);
552
array_copy(array->table, array->entries, other->table, 0, other->entries);
554
array->entries += other->entries;
558
static int json_array_equal(json_t *array1, json_t *array2)
562
size = json_array_size(array1);
563
if(size != json_array_size(array2))
566
for(i = 0; i < size; i++)
568
json_t *value1, *value2;
570
value1 = json_array_get(array1, i);
571
value2 = json_array_get(array2, i);
573
if(!json_equal(value1, value2))
580
static json_t *json_array_copy(json_t *array)
585
result = json_array();
589
for(i = 0; i < json_array_size(array); i++)
590
json_array_append(result, json_array_get(array, i));
595
static json_t *json_array_deep_copy(json_t *array)
600
result = json_array();
604
for(i = 0; i < json_array_size(array); i++)
605
json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
612
json_t *json_string_nocheck(const char *value)
614
json_string_t *string;
619
string = jsonp_malloc(sizeof(json_string_t));
622
json_init(&string->json, JSON_STRING);
624
string->value = jsonp_strdup(value);
630
return &string->json;
633
json_t *json_string(const char *value)
635
if(!value || !utf8_check_string(value, -1))
638
return json_string_nocheck(value);
641
const char *json_string_value(const json_t *json)
643
if(!json_is_string(json))
646
return json_to_string(json)->value;
649
int json_string_set_nocheck(json_t *json, const char *value)
652
json_string_t *string;
654
if(!json_is_string(json) || !value)
657
dup = jsonp_strdup(value);
661
string = json_to_string(json);
662
jsonp_free(string->value);
668
int json_string_set(json_t *json, const char *value)
670
if(!value || !utf8_check_string(value, -1))
673
return json_string_set_nocheck(json, value);
676
static void json_delete_string(json_string_t *string)
678
jsonp_free(string->value);
682
static int json_string_equal(json_t *string1, json_t *string2)
684
return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
687
static json_t *json_string_copy(json_t *string)
689
return json_string_nocheck(json_string_value(string));
695
json_t *json_integer(json_int_t value)
697
json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
700
json_init(&integer->json, JSON_INTEGER);
702
integer->value = value;
703
return &integer->json;
706
json_int_t json_integer_value(const json_t *json)
708
if(!json_is_integer(json))
711
return json_to_integer(json)->value;
714
int json_integer_set(json_t *json, json_int_t value)
716
if(!json_is_integer(json))
719
json_to_integer(json)->value = value;
724
static void json_delete_integer(json_integer_t *integer)
729
static int json_integer_equal(json_t *integer1, json_t *integer2)
731
return json_integer_value(integer1) == json_integer_value(integer2);
734
static json_t *json_integer_copy(json_t *integer)
736
return json_integer(json_integer_value(integer));
742
json_t *json_real(double value)
746
if(isnan(value) || isinf(value))
749
real = jsonp_malloc(sizeof(json_real_t));
752
json_init(&real->json, JSON_REAL);
758
double json_real_value(const json_t *json)
760
if(!json_is_real(json))
763
return json_to_real(json)->value;
766
int json_real_set(json_t *json, double value)
768
if(!json_is_real(json) || isnan(value) || isinf(value))
771
json_to_real(json)->value = value;
776
static void json_delete_real(json_real_t *real)
781
static int json_real_equal(json_t *real1, json_t *real2)
783
return json_real_value(real1) == json_real_value(real2);
786
static json_t *json_real_copy(json_t *real)
788
return json_real(json_real_value(real));
794
double json_number_value(const json_t *json)
796
if(json_is_integer(json))
797
return (double)json_integer_value(json);
798
else if(json_is_real(json))
799
return json_real_value(json);
805
/*** simple values ***/
807
json_t *json_true(void)
809
static json_t the_true = {JSON_TRUE, (size_t)-1};
814
json_t *json_false(void)
816
static json_t the_false = {JSON_FALSE, (size_t)-1};
821
json_t *json_null(void)
823
static json_t the_null = {JSON_NULL, (size_t)-1};
830
void json_delete(json_t *json)
832
if(json_is_object(json))
833
json_delete_object(json_to_object(json));
835
else if(json_is_array(json))
836
json_delete_array(json_to_array(json));
838
else if(json_is_string(json))
839
json_delete_string(json_to_string(json));
841
else if(json_is_integer(json))
842
json_delete_integer(json_to_integer(json));
844
else if(json_is_real(json))
845
json_delete_real(json_to_real(json));
847
/* json_delete is not called for true, false or null */
853
int json_equal(json_t *json1, json_t *json2)
858
if(json_typeof(json1) != json_typeof(json2))
861
/* this covers true, false and null as they are singletons */
865
if(json_is_object(json1))
866
return json_object_equal(json1, json2);
868
if(json_is_array(json1))
869
return json_array_equal(json1, json2);
871
if(json_is_string(json1))
872
return json_string_equal(json1, json2);
874
if(json_is_integer(json1))
875
return json_integer_equal(json1, json2);
877
if(json_is_real(json1))
878
return json_real_equal(json1, json2);
886
json_t *json_copy(json_t *json)
891
if(json_is_object(json))
892
return json_object_copy(json);
894
if(json_is_array(json))
895
return json_array_copy(json);
897
if(json_is_string(json))
898
return json_string_copy(json);
900
if(json_is_integer(json))
901
return json_integer_copy(json);
903
if(json_is_real(json))
904
return json_real_copy(json);
906
if(json_is_true(json) || json_is_false(json) || json_is_null(json))
912
json_t *json_deep_copy(json_t *json)
917
if(json_is_object(json))
918
return json_object_deep_copy(json);
920
if(json_is_array(json))
921
return json_array_deep_copy(json);
923
/* for the rest of the types, deep copying doesn't differ from
926
if(json_is_string(json))
927
return json_string_copy(json);
929
if(json_is_integer(json))
930
return json_integer_copy(json);
932
if(json_is_real(json))
933
return json_real_copy(json);
935
if(json_is_true(json) || json_is_false(json) || json_is_null(json))