forked from json-c/json-c
-
Notifications
You must be signed in to change notification settings - Fork 3
/
json_object.c
1854 lines (1638 loc) · 48.6 KB
/
json_object.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
* Michael Clark <[email protected]>
* Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See COPYING for details.
*
*/
#include "config.h"
#include "strerror_override.h"
#include <assert.h>
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arraylist.h"
#include "debug.h"
#include "json_inttypes.h"
#include "json_object.h"
#include "json_object_private.h"
#include "json_util.h"
#include "linkhash.h"
#include "math_compat.h"
#include "printbuf.h"
#include "snprintf_compat.h"
#include "strdup_compat.h"
/* Avoid ctype.h and locale overhead */
#define is_plain_digit(c) ((c) >= '0' && (c) <= '9')
#if SIZEOF_LONG_LONG != SIZEOF_INT64_T
#error The long long type is not 64-bits
#endif
#ifndef SSIZE_T_MAX
#if SIZEOF_SSIZE_T == SIZEOF_INT
#define SSIZE_T_MAX INT_MAX
#elif SIZEOF_SSIZE_T == SIZEOF_LONG
#define SSIZE_T_MAX LONG_MAX
#elif SIZEOF_SSIZE_T == SIZEOF_LONG_LONG
#define SSIZE_T_MAX LLONG_MAX
#else
#error Unable to determine size of ssize_t
#endif
#endif
const char *json_number_chars = "0123456789.+-eE"; /* Unused, but part of public API, drop for 1.0 */
const char *json_hex_chars = "0123456789abcdefABCDEF";
static void json_object_generic_delete(struct json_object *jso);
#if defined(_MSC_VER) && (_MSC_VER <= 1800)
/* VS2013 doesn't know about "inline" */
#define inline __inline
#elif defined(AIX_CC)
#define inline
#endif
/* define colors */
#define ANSI_COLOR_RESET "\033[0m"
#define ANSI_COLOR_FG_GREEN "\033[0;32m"
#define ANSI_COLOR_FG_BLUE "\033[0;34m"
#define ANSI_COLOR_FG_MAGENTA "\033[0;35m"
/*
* Helper functions to more safely cast to a particular type of json_object
*/
static inline struct json_object_object *JC_OBJECT(struct json_object *jso)
{
return (void *)jso;
}
static inline const struct json_object_object *JC_OBJECT_C(const struct json_object *jso)
{
return (const void *)jso;
}
static inline struct json_object_array *JC_ARRAY(struct json_object *jso)
{
return (void *)jso;
}
static inline const struct json_object_array *JC_ARRAY_C(const struct json_object *jso)
{
return (const void *)jso;
}
static inline struct json_object_boolean *JC_BOOL(struct json_object *jso)
{
return (void *)jso;
}
static inline const struct json_object_boolean *JC_BOOL_C(const struct json_object *jso)
{
return (const void *)jso;
}
static inline struct json_object_double *JC_DOUBLE(struct json_object *jso)
{
return (void *)jso;
}
static inline const struct json_object_double *JC_DOUBLE_C(const struct json_object *jso)
{
return (const void *)jso;
}
static inline struct json_object_int *JC_INT(struct json_object *jso)
{
return (void *)jso;
}
static inline const struct json_object_int *JC_INT_C(const struct json_object *jso)
{
return (const void *)jso;
}
static inline struct json_object_string *JC_STRING(struct json_object *jso)
{
return (void *)jso;
}
static inline const struct json_object_string *JC_STRING_C(const struct json_object *jso)
{
return (const void *)jso;
}
#define JC_CONCAT(a, b) a##b
#define JC_CONCAT3(a, b, c) a##b##c
#define JSON_OBJECT_NEW(jtype) \
(struct JC_CONCAT(json_object_, jtype) *)json_object_new( \
JC_CONCAT(json_type_, jtype), sizeof(struct JC_CONCAT(json_object_, jtype)), \
&JC_CONCAT3(json_object_, jtype, _to_json_string))
static inline struct json_object *json_object_new(enum json_type o_type, size_t alloc_size,
json_object_to_json_string_fn *to_json_string);
static void json_object_object_delete(struct json_object *jso_base);
static void json_object_string_delete(struct json_object *jso);
static void json_object_array_delete(struct json_object *jso);
static json_object_to_json_string_fn json_object_object_to_json_string;
static json_object_to_json_string_fn json_object_boolean_to_json_string;
static json_object_to_json_string_fn json_object_double_to_json_string_default;
static json_object_to_json_string_fn json_object_int_to_json_string;
static json_object_to_json_string_fn json_object_string_to_json_string;
static json_object_to_json_string_fn json_object_array_to_json_string;
static json_object_to_json_string_fn _json_object_userdata_to_json_string;
#ifndef JSON_NORETURN
#if defined(_MSC_VER)
#define JSON_NORETURN __declspec(noreturn)
#elif defined(__OS400__)
#define JSON_NORETURN
#else
/* 'cold' attribute is for optimization, telling the computer this code
* path is unlikely.
*/
#define JSON_NORETURN __attribute__((noreturn, cold))
#endif
#endif
/**
* Abort and optionally print a message on standard error.
* This should be used rather than assert() for unconditional abortion
* (in particular for code paths which are never supposed to be run).
* */
JSON_NORETURN static void json_abort(const char *message);
/* helper for accessing the optimized string data component in json_object
*/
static inline char *get_string_component_mutable(struct json_object *jso)
{
if (JC_STRING_C(jso)->len < 0)
{
/* Due to json_object_set_string(), we might have a pointer */
return JC_STRING(jso)->c_string.pdata;
}
return JC_STRING(jso)->c_string.idata;
}
static inline const char *get_string_component(const struct json_object *jso)
{
return get_string_component_mutable((void *)(uintptr_t)(const void *)jso);
}
/* string escaping */
static int json_escape_str(struct printbuf *pb, const char *str, size_t len, int flags)
{
size_t pos = 0, start_offset = 0;
unsigned char c;
while (len)
{
--len;
c = str[pos];
switch (c)
{
case '\b':
case '\n':
case '\r':
case '\t':
case '\f':
case '"':
case '\\':
case '/':
if ((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
{
pos++;
break;
}
if (pos > start_offset)
printbuf_memappend(pb, str + start_offset, pos - start_offset);
if (c == '\b')
printbuf_memappend(pb, "\\b", 2);
else if (c == '\n')
printbuf_memappend(pb, "\\n", 2);
else if (c == '\r')
printbuf_memappend(pb, "\\r", 2);
else if (c == '\t')
printbuf_memappend(pb, "\\t", 2);
else if (c == '\f')
printbuf_memappend(pb, "\\f", 2);
else if (c == '"')
printbuf_memappend(pb, "\\\"", 2);
else if (c == '\\')
printbuf_memappend(pb, "\\\\", 2);
else if (c == '/')
printbuf_memappend(pb, "\\/", 2);
start_offset = ++pos;
break;
default:
if (c < ' ')
{
char sbuf[7];
if (pos > start_offset)
printbuf_memappend(pb, str + start_offset,
pos - start_offset);
snprintf(sbuf, sizeof(sbuf), "\\u00%c%c", json_hex_chars[c >> 4],
json_hex_chars[c & 0xf]);
printbuf_memappend_fast(pb, sbuf, (int)sizeof(sbuf) - 1);
start_offset = ++pos;
}
else
pos++;
}
}
if (pos > start_offset)
printbuf_memappend(pb, str + start_offset, pos - start_offset);
return 0;
}
/* reference counting */
struct json_object *json_object_get(struct json_object *jso)
{
if (!jso)
return jso;
// Don't overflow the refcounter.
assert(jso->_ref_count < UINT32_MAX);
#if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
__sync_add_and_fetch(&jso->_ref_count, 1);
#else
++jso->_ref_count;
#endif
return jso;
}
int json_object_put(struct json_object *jso)
{
if (!jso)
return 0;
/* Avoid invalid free and crash explicitly instead of (silently)
* segfaulting.
*/
assert(jso->_ref_count > 0);
#if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
/* Note: this only allow the refcount to remain correct
* when multiple threads are adjusting it. It is still an error
* for a thread to decrement the refcount if it doesn't "own" it,
* as that can result in the thread that loses the race to 0
* operating on an already-freed object.
*/
if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0)
return 0;
#else
if (--jso->_ref_count > 0)
return 0;
#endif
if (jso->_user_delete)
jso->_user_delete(jso, jso->_userdata);
switch (jso->o_type)
{
case json_type_object: json_object_object_delete(jso); break;
case json_type_array: json_object_array_delete(jso); break;
case json_type_string: json_object_string_delete(jso); break;
default: json_object_generic_delete(jso); break;
}
return 1;
}
/* generic object construction and destruction parts */
static void json_object_generic_delete(struct json_object *jso)
{
printbuf_free(jso->_pb);
free(jso);
}
static inline struct json_object *json_object_new(enum json_type o_type, size_t alloc_size,
json_object_to_json_string_fn *to_json_string)
{
struct json_object *jso;
jso = (struct json_object *)malloc(alloc_size);
if (!jso)
return NULL;
jso->o_type = o_type;
jso->_ref_count = 1;
jso->_to_json_string = to_json_string;
jso->_pb = NULL;
jso->_user_delete = NULL;
jso->_userdata = NULL;
//jso->... // Type-specific fields must be set by caller
return jso;
}
/* type checking functions */
int json_object_is_type(const struct json_object *jso, enum json_type type)
{
if (!jso)
return (type == json_type_null);
return (jso->o_type == type);
}
enum json_type json_object_get_type(const struct json_object *jso)
{
if (!jso)
return json_type_null;
return jso->o_type;
}
void *json_object_get_userdata(json_object *jso)
{
return jso ? jso->_userdata : NULL;
}
void json_object_set_userdata(json_object *jso, void *userdata, json_object_delete_fn *user_delete)
{
// Can't return failure, so abort if we can't perform the operation.
assert(jso != NULL);
// First, clean up any previously existing user info
if (jso->_user_delete)
jso->_user_delete(jso, jso->_userdata);
jso->_userdata = userdata;
jso->_user_delete = user_delete;
}
/* set a custom conversion to string */
void json_object_set_serializer(json_object *jso, json_object_to_json_string_fn *to_string_func,
void *userdata, json_object_delete_fn *user_delete)
{
json_object_set_userdata(jso, userdata, user_delete);
if (to_string_func == NULL)
{
// Reset to the standard serialization function
switch (jso->o_type)
{
case json_type_null: jso->_to_json_string = NULL; break;
case json_type_boolean:
jso->_to_json_string = &json_object_boolean_to_json_string;
break;
case json_type_double:
jso->_to_json_string = &json_object_double_to_json_string_default;
break;
case json_type_int: jso->_to_json_string = &json_object_int_to_json_string; break;
case json_type_object:
jso->_to_json_string = &json_object_object_to_json_string;
break;
case json_type_array:
jso->_to_json_string = &json_object_array_to_json_string;
break;
case json_type_string:
jso->_to_json_string = &json_object_string_to_json_string;
break;
}
return;
}
jso->_to_json_string = to_string_func;
}
/* extended conversion to string */
const char *json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
{
const char *r = NULL;
size_t s = 0;
if (!jso)
{
s = 4;
r = "null";
}
else if ((jso->_pb) || (jso->_pb = printbuf_new()))
{
printbuf_reset(jso->_pb);
if (jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
{
s = (size_t)jso->_pb->bpos;
r = jso->_pb->buf;
}
}
if (length)
*length = s;
return r;
}
const char *json_object_to_json_string_ext(struct json_object *jso, int flags)
{
return json_object_to_json_string_length(jso, flags, NULL);
}
/* backwards-compatible conversion to string */
const char *json_object_to_json_string(struct json_object *jso)
{
return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
}
static void indent(struct printbuf *pb, int level, int flags)
{
if (flags & JSON_C_TO_STRING_PRETTY)
{
if (flags & JSON_C_TO_STRING_PRETTY_TAB)
{
printbuf_memset(pb, -1, '\t', level);
}
else
{
printbuf_memset(pb, -1, ' ', level * 2);
}
}
}
/* json_object_object */
static int json_object_object_to_json_string(struct json_object *jso, struct printbuf *pb,
int level, int flags)
{
int had_children = 0;
struct json_object_iter iter;
printbuf_strappend(pb, "{" /*}*/);
json_object_object_foreachC(jso, iter)
{
if (had_children)
{
printbuf_strappend(pb, ",");
}
if (flags & JSON_C_TO_STRING_PRETTY)
printbuf_strappend(pb, "\n");
had_children = 1;
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
printbuf_strappend(pb, " ");
indent(pb, level + 1, flags);
if (flags & JSON_C_TO_STRING_COLOR)
printbuf_strappend(pb, ANSI_COLOR_FG_BLUE);
printbuf_strappend(pb, "\"");
json_escape_str(pb, iter.key, strlen(iter.key), flags);
printbuf_strappend(pb, "\"");
if (flags & JSON_C_TO_STRING_COLOR)
printbuf_strappend(pb, ANSI_COLOR_RESET);
if (flags & JSON_C_TO_STRING_SPACED)
printbuf_strappend(pb, ": ");
else
printbuf_strappend(pb, ":");
if (iter.val == NULL) {
if (flags & JSON_C_TO_STRING_COLOR)
printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA);
printbuf_strappend(pb, "null");
if (flags & JSON_C_TO_STRING_COLOR)
printbuf_strappend(pb, ANSI_COLOR_RESET);
} else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0)
return -1;
}
if ((flags & JSON_C_TO_STRING_PRETTY) && had_children)
{
printbuf_strappend(pb, "\n");
indent(pb, level, flags);
}
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
return printbuf_strappend(pb, /*{*/ " }");
else
return printbuf_strappend(pb, /*{*/ "}");
}
static void json_object_lh_entry_free(struct lh_entry *ent)
{
if (!lh_entry_k_is_constant(ent))
free(lh_entry_k(ent));
json_object_put((struct json_object *)lh_entry_v(ent));
}
static void json_object_object_delete(struct json_object *jso_base)
{
lh_table_free(JC_OBJECT(jso_base)->c_object);
json_object_generic_delete(jso_base);
}
struct json_object *json_object_new_object(void)
{
struct json_object_object *jso = JSON_OBJECT_NEW(object);
if (!jso)
return NULL;
jso->c_object =
lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES, &json_object_lh_entry_free);
if (!jso->c_object)
{
json_object_generic_delete(&jso->base);
errno = ENOMEM;
return NULL;
}
return &jso->base;
}
struct lh_table *json_object_get_object(const struct json_object *jso)
{
if (!jso)
return NULL;
switch (jso->o_type)
{
case json_type_object: return JC_OBJECT_C(jso)->c_object;
default: return NULL;
}
}
int json_object_object_add_ex(struct json_object *jso, const char *const key,
struct json_object *const val, const unsigned opts)
{
struct json_object *existing_value = NULL;
struct lh_entry *existing_entry;
unsigned long hash;
assert(json_object_get_type(jso) == json_type_object);
// We lookup the entry and replace the value, rather than just deleting
// and re-adding it, so the existing key remains valid.
hash = lh_get_hash(JC_OBJECT(jso)->c_object, (const void *)key);
existing_entry =
(opts & JSON_C_OBJECT_ADD_KEY_IS_NEW)
? NULL
: lh_table_lookup_entry_w_hash(JC_OBJECT(jso)->c_object, (const void *)key, hash);
// The caller must avoid creating loops in the object tree, but do a
// quick check anyway to make sure we're not creating a trivial loop.
if (jso == val)
return -1;
if (!existing_entry)
{
const void *const k =
(opts & JSON_C_OBJECT_ADD_CONSTANT_KEY) ? (const void *)key : strdup(key);
if (k == NULL)
return -1;
return lh_table_insert_w_hash(JC_OBJECT(jso)->c_object, k, val, hash, opts);
}
existing_value = (json_object *)lh_entry_v(existing_entry);
if (existing_value)
json_object_put(existing_value);
lh_entry_set_val(existing_entry, val);
return 0;
}
int json_object_object_add(struct json_object *jso, const char *key, struct json_object *val)
{
return json_object_object_add_ex(jso, key, val, 0);
}
int json_object_object_length(const struct json_object *jso)
{
assert(json_object_get_type(jso) == json_type_object);
return lh_table_length(JC_OBJECT_C(jso)->c_object);
}
size_t json_c_object_sizeof(void)
{
return sizeof(struct json_object);
}
struct json_object *json_object_object_get(const struct json_object *jso, const char *key)
{
struct json_object *result = NULL;
json_object_object_get_ex(jso, key, &result);
return result;
}
json_bool json_object_object_get_ex(const struct json_object *jso, const char *key,
struct json_object **value)
{
if (value != NULL)
*value = NULL;
if (NULL == jso)
return 0;
switch (jso->o_type)
{
case json_type_object:
return lh_table_lookup_ex(JC_OBJECT_C(jso)->c_object, (const void *)key,
(void **)value);
default:
if (value != NULL)
*value = NULL;
return 0;
}
}
void json_object_object_del(struct json_object *jso, const char *key)
{
assert(json_object_get_type(jso) == json_type_object);
lh_table_delete(JC_OBJECT(jso)->c_object, key);
}
/* json_object_boolean */
static int json_object_boolean_to_json_string(struct json_object *jso, struct printbuf *pb,
int level, int flags)
{
int ret;
if (flags & JSON_C_TO_STRING_COLOR)
printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA);
if (JC_BOOL(jso)->c_boolean)
ret = printbuf_strappend(pb, "true");
else
ret = printbuf_strappend(pb, "false");
if (ret > -1 && flags & JSON_C_TO_STRING_COLOR)
return printbuf_strappend(pb, ANSI_COLOR_RESET);
return ret;
}
struct json_object *json_object_new_boolean(json_bool b)
{
struct json_object_boolean *jso = JSON_OBJECT_NEW(boolean);
if (!jso)
return NULL;
jso->c_boolean = b;
return &jso->base;
}
json_bool json_object_get_boolean(const struct json_object *jso)
{
if (!jso)
return 0;
switch (jso->o_type)
{
case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
case json_type_int:
switch (JC_INT_C(jso)->cint_type)
{
case json_object_int_type_int64: return (JC_INT_C(jso)->cint.c_int64 != 0);
case json_object_int_type_uint64: return (JC_INT_C(jso)->cint.c_uint64 != 0);
default: json_abort("invalid cint_type");
}
case json_type_double: return (JC_DOUBLE_C(jso)->c_double != 0);
case json_type_string: return (JC_STRING_C(jso)->len != 0);
default: return 0;
}
}
int json_object_set_boolean(struct json_object *jso, json_bool new_value)
{
if (!jso || jso->o_type != json_type_boolean)
return 0;
JC_BOOL(jso)->c_boolean = new_value;
return 1;
}
/* json_object_int */
static int json_object_int_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
int flags)
{
/* room for 19 digits, the sign char, and a null term */
char sbuf[21];
if (JC_INT(jso)->cint_type == json_object_int_type_int64)
snprintf(sbuf, sizeof(sbuf), "%" PRId64, JC_INT(jso)->cint.c_int64);
else
snprintf(sbuf, sizeof(sbuf), "%" PRIu64, JC_INT(jso)->cint.c_uint64);
return printbuf_memappend(pb, sbuf, strlen(sbuf));
}
struct json_object *json_object_new_int(int32_t i)
{
return json_object_new_int64(i);
}
int32_t json_object_get_int(const struct json_object *jso)
{
int64_t cint64 = 0;
double cdouble;
enum json_type o_type;
if (!jso)
return 0;
o_type = jso->o_type;
if (o_type == json_type_int)
{
const struct json_object_int *jsoint = JC_INT_C(jso);
if (jsoint->cint_type == json_object_int_type_int64)
{
cint64 = jsoint->cint.c_int64;
}
else
{
if (jsoint->cint.c_uint64 >= INT64_MAX)
cint64 = INT64_MAX;
else
cint64 = (int64_t)jsoint->cint.c_uint64;
}
}
else if (o_type == json_type_string)
{
/*
* Parse strings into 64-bit numbers, then use the
* 64-to-32-bit number handling below.
*/
if (json_parse_int64(get_string_component(jso), &cint64) != 0)
return 0; /* whoops, it didn't work. */
o_type = json_type_int;
}
switch (o_type)
{
case json_type_int:
/* Make sure we return the correct values for out of range numbers. */
if (cint64 <= INT32_MIN)
return INT32_MIN;
if (cint64 >= INT32_MAX)
return INT32_MAX;
return (int32_t)cint64;
case json_type_double:
cdouble = JC_DOUBLE_C(jso)->c_double;
if (cdouble <= INT32_MIN)
return INT32_MIN;
if (cdouble >= INT32_MAX)
return INT32_MAX;
return (int32_t)cdouble;
case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
default: return 0;
}
}
int json_object_set_int(struct json_object *jso, int new_value)
{
return json_object_set_int64(jso, (int64_t)new_value);
}
struct json_object *json_object_new_int64(int64_t i)
{
struct json_object_int *jso = JSON_OBJECT_NEW(int);
if (!jso)
return NULL;
jso->cint.c_int64 = i;
jso->cint_type = json_object_int_type_int64;
return &jso->base;
}
struct json_object *json_object_new_uint64(uint64_t i)
{
struct json_object_int *jso = JSON_OBJECT_NEW(int);
if (!jso)
return NULL;
jso->cint.c_uint64 = i;
jso->cint_type = json_object_int_type_uint64;
return &jso->base;
}
int64_t json_object_get_int64(const struct json_object *jso)
{
int64_t cint;
if (!jso)
return 0;
switch (jso->o_type)
{
case json_type_int:
{
const struct json_object_int *jsoint = JC_INT_C(jso);
switch (jsoint->cint_type)
{
case json_object_int_type_int64: return jsoint->cint.c_int64;
case json_object_int_type_uint64:
if (jsoint->cint.c_uint64 >= INT64_MAX)
return INT64_MAX;
return (int64_t)jsoint->cint.c_uint64;
default: json_abort("invalid cint_type");
}
}
case json_type_double:
// INT64_MAX can't be exactly represented as a double
// so cast to tell the compiler it's ok to round up.
if (JC_DOUBLE_C(jso)->c_double >= (double)INT64_MAX)
return INT64_MAX;
if (JC_DOUBLE_C(jso)->c_double <= INT64_MIN)
return INT64_MIN;
return (int64_t)JC_DOUBLE_C(jso)->c_double;
case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
case json_type_string:
if (json_parse_int64(get_string_component(jso), &cint) == 0)
return cint;
/* FALLTHRU */
default: return 0;
}
}
uint64_t json_object_get_uint64(const struct json_object *jso)
{
uint64_t cuint;
if (!jso)
return 0;
switch (jso->o_type)
{
case json_type_int:
{
const struct json_object_int *jsoint = JC_INT_C(jso);
switch (jsoint->cint_type)
{
case json_object_int_type_int64:
if (jsoint->cint.c_int64 < 0)
return 0;
return (uint64_t)jsoint->cint.c_int64;
case json_object_int_type_uint64: return jsoint->cint.c_uint64;
default: json_abort("invalid cint_type");
}
}
case json_type_double:
// UINT64_MAX can't be exactly represented as a double
// so cast to tell the compiler it's ok to round up.
if (JC_DOUBLE_C(jso)->c_double >= (double)UINT64_MAX)
return UINT64_MAX;
if (JC_DOUBLE_C(jso)->c_double < 0)
return 0;
return (uint64_t)JC_DOUBLE_C(jso)->c_double;
case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
case json_type_string:
if (json_parse_uint64(get_string_component(jso), &cuint) == 0)
return cuint;
/* FALLTHRU */
default: return 0;
}
}
int json_object_set_int64(struct json_object *jso, int64_t new_value)
{
if (!jso || jso->o_type != json_type_int)
return 0;
JC_INT(jso)->cint.c_int64 = new_value;
JC_INT(jso)->cint_type = json_object_int_type_int64;
return 1;
}
int json_object_set_uint64(struct json_object *jso, uint64_t new_value)
{
if (!jso || jso->o_type != json_type_int)
return 0;
JC_INT(jso)->cint.c_uint64 = new_value;
JC_INT(jso)->cint_type = json_object_int_type_uint64;
return 1;
}
int json_object_int_inc(struct json_object *jso, int64_t val)
{
struct json_object_int *jsoint;
if (!jso || jso->o_type != json_type_int)
return 0;
jsoint = JC_INT(jso);
switch (jsoint->cint_type)
{
case json_object_int_type_int64:
if (val > 0 && jsoint->cint.c_int64 > INT64_MAX - val)
{
jsoint->cint.c_uint64 = (uint64_t)jsoint->cint.c_int64 + (uint64_t)val;
jsoint->cint_type = json_object_int_type_uint64;
}
else if (val < 0 && jsoint->cint.c_int64 < INT64_MIN - val)
{
jsoint->cint.c_int64 = INT64_MIN;
}
else
{
jsoint->cint.c_int64 += val;
}
return 1;
case json_object_int_type_uint64:
if (val > 0 && jsoint->cint.c_uint64 > UINT64_MAX - (uint64_t)val)
{
jsoint->cint.c_uint64 = UINT64_MAX;
}
else if (val < 0 && jsoint->cint.c_uint64 < (uint64_t)(-val))
{
jsoint->cint.c_int64 = (int64_t)jsoint->cint.c_uint64 + val;
jsoint->cint_type = json_object_int_type_int64;
}
else if (val < 0 && jsoint->cint.c_uint64 >= (uint64_t)(-val))
{
jsoint->cint.c_uint64 -= (uint64_t)(-val);
}
else
{
jsoint->cint.c_uint64 += val;
}
return 1;
default: json_abort("invalid cint_type");
}
}
/* json_object_double */
#if defined(HAVE___THREAD)
// i.e. __thread or __declspec(thread)
static SPEC___THREAD char *tls_serialization_float_format = NULL;
#endif
static char *global_serialization_float_format = NULL;
int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
{
if (global_or_thread == JSON_C_OPTION_GLOBAL)
{
#if defined(HAVE___THREAD)
if (tls_serialization_float_format)
{
free(tls_serialization_float_format);
tls_serialization_float_format = NULL;
}
#endif
if (global_serialization_float_format)
free(global_serialization_float_format);
if (double_format)
{
char *p = strdup(double_format);
if (p == NULL)
{
_json_c_set_last_err("json_c_set_serialization_double_format: "
"out of memory\n");
return -1;
}
global_serialization_float_format = p;
}
else
{
global_serialization_float_format = NULL;
}
}
else if (global_or_thread == JSON_C_OPTION_THREAD)
{
#if defined(HAVE___THREAD)
if (tls_serialization_float_format)
{
free(tls_serialization_float_format);
tls_serialization_float_format = NULL;
}
if (double_format)
{
char *p = strdup(double_format);
if (p == NULL)
{
_json_c_set_last_err("json_c_set_serialization_double_format: "
"out of memory\n");
return -1;
}
tls_serialization_float_format = p;
}
else
{
tls_serialization_float_format = NULL;
}