]>
Commit | Line | Data |
---|---|---|
556826b5 OM |
1 | /* |
2 | * Copyright (c) 2009-2016 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 | #ifndef JANSSON_H | |
9 | #define JANSSON_H | |
10 | ||
11 | #include <stdio.h> | |
12 | #include <stdint.h> | |
13 | #include <stdlib.h> /* for size_t */ | |
14 | #include <stdarg.h> | |
15 | ||
16 | #include "jansson_config.h" | |
17 | ||
18 | #ifdef __cplusplus | |
19 | extern "C" { | |
20 | #endif | |
21 | ||
22 | /* version */ | |
23 | ||
24 | #define JANSSON_MAJOR_VERSION 2 | |
25 | #define JANSSON_MINOR_VERSION 11 | |
26 | #define JANSSON_MICRO_VERSION 0 | |
27 | ||
28 | /* Micro version is omitted if it's 0 */ | |
29 | #define JANSSON_VERSION "2.11" | |
30 | ||
31 | /* Version as a 3-byte hex number, e.g. 0x010201 == 1.2.1. Use this | |
32 | for numeric comparisons, e.g. #if JANSSON_VERSION_HEX >= ... */ | |
33 | #define JANSSON_VERSION_HEX ((JANSSON_MAJOR_VERSION << 16) | \ | |
34 | (JANSSON_MINOR_VERSION << 8) | \ | |
35 | (JANSSON_MICRO_VERSION << 0)) | |
36 | ||
37 | /* If __atomic or __sync builtins are available the library is thread | |
38 | * safe for all read-only functions plus reference counting. */ | |
39 | #if JSON_HAVE_ATOMIC_BUILTINS || JSON_HAVE_SYNC_BUILTINS | |
40 | #define JANSSON_THREAD_SAFE_REFCOUNT 1 | |
41 | #endif | |
42 | ||
43 | /* types */ | |
44 | ||
45 | typedef enum { | |
46 | JSON_OBJECT, | |
47 | JSON_ARRAY, | |
48 | JSON_STRING, | |
49 | JSON_INTEGER, | |
50 | JSON_REAL, | |
51 | JSON_TRUE, | |
52 | JSON_FALSE, | |
53 | JSON_NULL | |
54 | } json_type; | |
55 | ||
56 | typedef struct json_t { | |
57 | json_type type; | |
58 | volatile size_t refcount; | |
59 | } json_t; | |
60 | ||
61 | #ifndef JANSSON_USING_CMAKE /* disabled if using cmake */ | |
62 | #if JSON_INTEGER_IS_LONG_LONG | |
63 | #ifdef _WIN32 | |
64 | #define JSON_INTEGER_FORMAT "I64d" | |
65 | #else | |
66 | #define JSON_INTEGER_FORMAT "lld" | |
67 | #endif | |
68 | typedef long long json_int_t; | |
69 | #else | |
70 | #define JSON_INTEGER_FORMAT "ld" | |
71 | typedef long json_int_t; | |
72 | #endif /* JSON_INTEGER_IS_LONG_LONG */ | |
73 | #endif | |
74 | ||
75 | #define json_typeof(json) ((json)->type) | |
76 | #define json_is_object(json) ((json) && json_typeof(json) == JSON_OBJECT) | |
77 | #define json_is_array(json) ((json) && json_typeof(json) == JSON_ARRAY) | |
78 | #define json_is_string(json) ((json) && json_typeof(json) == JSON_STRING) | |
79 | #define json_is_integer(json) ((json) && json_typeof(json) == JSON_INTEGER) | |
80 | #define json_is_real(json) ((json) && json_typeof(json) == JSON_REAL) | |
81 | #define json_is_number(json) (json_is_integer(json) || json_is_real(json)) | |
82 | #define json_is_true(json) ((json) && json_typeof(json) == JSON_TRUE) | |
83 | #define json_is_false(json) ((json) && json_typeof(json) == JSON_FALSE) | |
84 | #define json_boolean_value json_is_true | |
85 | #define json_is_boolean(json) (json_is_true(json) || json_is_false(json)) | |
86 | #define json_is_null(json) ((json) && json_typeof(json) == JSON_NULL) | |
87 | ||
88 | /* construction, destruction, reference counting */ | |
89 | ||
90 | json_t *json_object(void); | |
91 | json_t *json_array(void); | |
92 | json_t *json_string(const char *value); | |
93 | json_t *json_stringn(const char *value, size_t len); | |
94 | json_t *json_string_nocheck(const char *value); | |
95 | json_t *json_stringn_nocheck(const char *value, size_t len); | |
96 | json_t *json_integer(json_int_t value); | |
97 | json_t *json_real(double value); | |
98 | json_t *json_true(void); | |
99 | json_t *json_false(void); | |
100 | #define json_boolean(val) ((val) ? json_true() : json_false()) | |
101 | json_t *json_null(void); | |
102 | ||
103 | /* do not call JSON_INTERNAL_INCREF or JSON_INTERNAL_DECREF directly */ | |
104 | #if JSON_HAVE_ATOMIC_BUILTINS | |
105 | #define JSON_INTERNAL_INCREF(json) __atomic_add_fetch(&json->refcount, 1, __ATOMIC_ACQUIRE) | |
106 | #define JSON_INTERNAL_DECREF(json) __atomic_sub_fetch(&json->refcount, 1, __ATOMIC_RELEASE) | |
107 | #elif JSON_HAVE_SYNC_BUILTINS | |
108 | #define JSON_INTERNAL_INCREF(json) __sync_add_and_fetch(&json->refcount, 1) | |
109 | #define JSON_INTERNAL_DECREF(json) __sync_sub_and_fetch(&json->refcount, 1) | |
110 | #else | |
111 | #define JSON_INTERNAL_INCREF(json) (++json->refcount) | |
112 | #define JSON_INTERNAL_DECREF(json) (--json->refcount) | |
113 | #endif | |
114 | ||
115 | static JSON_INLINE | |
116 | json_t *json_incref(json_t *json) | |
117 | { | |
118 | if(json && json->refcount != (size_t)-1) | |
119 | JSON_INTERNAL_INCREF(json); | |
120 | return json; | |
121 | } | |
122 | ||
123 | /* do not call json_delete directly */ | |
124 | void json_delete(json_t *json); | |
125 | ||
126 | static JSON_INLINE | |
127 | void json_decref(json_t *json) | |
128 | { | |
129 | if(json && json->refcount != (size_t)-1 && JSON_INTERNAL_DECREF(json) == 0) | |
130 | json_delete(json); | |
131 | } | |
132 | ||
133 | #if defined(__GNUC__) || defined(__clang__) | |
134 | static JSON_INLINE | |
135 | void json_decrefp(json_t **json) | |
136 | { | |
137 | if(json) { | |
138 | json_decref(*json); | |
139 | *json = NULL; | |
140 | } | |
141 | } | |
142 | ||
143 | #define json_auto_t json_t __attribute__((cleanup(json_decrefp))) | |
144 | #endif | |
145 | ||
146 | ||
147 | /* error reporting */ | |
148 | ||
149 | #define JSON_ERROR_TEXT_LENGTH 160 | |
150 | #define JSON_ERROR_SOURCE_LENGTH 80 | |
151 | ||
152 | typedef struct json_error_t { | |
153 | int line; | |
154 | int column; | |
155 | int position; | |
156 | char source[JSON_ERROR_SOURCE_LENGTH]; | |
157 | char text[JSON_ERROR_TEXT_LENGTH]; | |
158 | } json_error_t; | |
159 | ||
160 | enum json_error_code { | |
161 | json_error_unknown, | |
162 | json_error_out_of_memory, | |
163 | json_error_stack_overflow, | |
164 | json_error_cannot_open_file, | |
165 | json_error_invalid_argument, | |
166 | json_error_invalid_utf8, | |
167 | json_error_premature_end_of_input, | |
168 | json_error_end_of_input_expected, | |
169 | json_error_invalid_syntax, | |
170 | json_error_invalid_format, | |
171 | json_error_wrong_type, | |
172 | json_error_null_character, | |
173 | json_error_null_value, | |
174 | json_error_null_byte_in_key, | |
175 | json_error_duplicate_key, | |
176 | json_error_numeric_overflow, | |
177 | json_error_item_not_found, | |
178 | json_error_index_out_of_range | |
179 | }; | |
180 | ||
181 | static JSON_INLINE enum json_error_code json_error_code(const json_error_t *e) { | |
182 | return (enum json_error_code)e->text[JSON_ERROR_TEXT_LENGTH - 1]; | |
183 | } | |
184 | ||
185 | /* getters, setters, manipulation */ | |
186 | ||
187 | void json_object_seed(size_t seed); | |
188 | size_t json_object_size(const json_t *object); | |
189 | json_t *json_object_get(const json_t *object, const char *key); | |
190 | int json_object_set_new(json_t *object, const char *key, json_t *value); | |
191 | int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value); | |
192 | int json_object_del(json_t *object, const char *key); | |
193 | int json_object_clear(json_t *object); | |
194 | int json_object_update(json_t *object, json_t *other); | |
195 | int json_object_update_existing(json_t *object, json_t *other); | |
196 | int json_object_update_missing(json_t *object, json_t *other); | |
197 | void *json_object_iter(json_t *object); | |
198 | void *json_object_iter_at(json_t *object, const char *key); | |
199 | void *json_object_key_to_iter(const char *key); | |
200 | void *json_object_iter_next(json_t *object, void *iter); | |
201 | const char *json_object_iter_key(void *iter); | |
202 | json_t *json_object_iter_value(void *iter); | |
203 | int json_object_iter_set_new(json_t *object, void *iter, json_t *value); | |
204 | ||
205 | #define json_object_foreach(object, key, value) \ | |
206 | for(key = json_object_iter_key(json_object_iter(object)); \ | |
207 | key && (value = json_object_iter_value(json_object_key_to_iter(key))); \ | |
208 | key = json_object_iter_key(json_object_iter_next(object, json_object_key_to_iter(key)))) | |
209 | ||
210 | #define json_object_foreach_safe(object, n, key, value) \ | |
211 | for(key = json_object_iter_key(json_object_iter(object)), \ | |
212 | n = json_object_iter_next(object, json_object_key_to_iter(key)); \ | |
213 | key && (value = json_object_iter_value(json_object_key_to_iter(key))); \ | |
214 | key = json_object_iter_key(n), \ | |
215 | n = json_object_iter_next(object, json_object_key_to_iter(key))) | |
216 | ||
217 | #define json_array_foreach(array, index, value) \ | |
218 | for(index = 0; \ | |
219 | index < json_array_size(array) && (value = json_array_get(array, index)); \ | |
220 | index++) | |
221 | ||
222 | static JSON_INLINE | |
223 | int json_object_set(json_t *object, const char *key, json_t *value) | |
224 | { | |
225 | return json_object_set_new(object, key, json_incref(value)); | |
226 | } | |
227 | ||
228 | static JSON_INLINE | |
229 | int json_object_set_nocheck(json_t *object, const char *key, json_t *value) | |
230 | { | |
231 | return json_object_set_new_nocheck(object, key, json_incref(value)); | |
232 | } | |
233 | ||
234 | static JSON_INLINE | |
235 | int json_object_iter_set(json_t *object, void *iter, json_t *value) | |
236 | { | |
237 | return json_object_iter_set_new(object, iter, json_incref(value)); | |
238 | } | |
239 | ||
240 | size_t json_array_size(const json_t *array); | |
241 | json_t *json_array_get(const json_t *array, size_t index); | |
242 | int json_array_set_new(json_t *array, size_t index, json_t *value); | |
243 | int json_array_append_new(json_t *array, json_t *value); | |
244 | int json_array_insert_new(json_t *array, size_t index, json_t *value); | |
245 | int json_array_remove(json_t *array, size_t index); | |
246 | int json_array_clear(json_t *array); | |
247 | int json_array_extend(json_t *array, json_t *other); | |
248 | ||
249 | static JSON_INLINE | |
250 | int json_array_set(json_t *array, size_t ind, json_t *value) | |
251 | { | |
252 | return json_array_set_new(array, ind, json_incref(value)); | |
253 | } | |
254 | ||
255 | static JSON_INLINE | |
256 | int json_array_append(json_t *array, json_t *value) | |
257 | { | |
258 | return json_array_append_new(array, json_incref(value)); | |
259 | } | |
260 | ||
261 | static JSON_INLINE | |
262 | int json_array_insert(json_t *array, size_t ind, json_t *value) | |
263 | { | |
264 | return json_array_insert_new(array, ind, json_incref(value)); | |
265 | } | |
266 | ||
267 | const char *json_string_value(const json_t *string); | |
268 | size_t json_string_length(const json_t *string); | |
269 | json_int_t json_integer_value(const json_t *integer); | |
270 | double json_real_value(const json_t *real); | |
271 | double json_number_value(const json_t *json); | |
272 | ||
273 | int json_string_set(json_t *string, const char *value); | |
274 | int json_string_setn(json_t *string, const char *value, size_t len); | |
275 | int json_string_set_nocheck(json_t *string, const char *value); | |
276 | int json_string_setn_nocheck(json_t *string, const char *value, size_t len); | |
277 | int json_integer_set(json_t *integer, json_int_t value); | |
278 | int json_real_set(json_t *real, double value); | |
279 | ||
280 | /* pack, unpack */ | |
281 | ||
282 | json_t *json_pack(const char *fmt, ...); | |
283 | json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...); | |
284 | json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap); | |
285 | ||
286 | #define JSON_VALIDATE_ONLY 0x1 | |
287 | #define JSON_STRICT 0x2 | |
288 | ||
289 | int json_unpack(json_t *root, const char *fmt, ...); | |
290 | int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...); | |
291 | int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap); | |
292 | ||
293 | /* sprintf */ | |
294 | ||
295 | json_t *json_sprintf(const char *fmt, ...); | |
296 | json_t *json_vsprintf(const char *fmt, va_list ap); | |
297 | ||
298 | ||
299 | /* equality */ | |
300 | ||
301 | int json_equal(const json_t *value1, const json_t *value2); | |
302 | ||
303 | ||
304 | /* copying */ | |
305 | ||
306 | json_t *json_copy(json_t *value); | |
307 | json_t *json_deep_copy(const json_t *value); | |
308 | ||
309 | ||
310 | /* decoding */ | |
311 | ||
312 | #define JSON_REJECT_DUPLICATES 0x1 | |
313 | #define JSON_DISABLE_EOF_CHECK 0x2 | |
314 | #define JSON_DECODE_ANY 0x4 | |
315 | #define JSON_DECODE_INT_AS_REAL 0x8 | |
316 | #define JSON_ALLOW_NUL 0x10 | |
317 | ||
318 | typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data); | |
319 | ||
320 | json_t *json_loads(const char *input, size_t flags, json_error_t *error); | |
321 | json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error); | |
322 | json_t *json_loadf(FILE *input, size_t flags, json_error_t *error); | |
323 | json_t *json_loadfd(int input, size_t flags, json_error_t *error); | |
324 | json_t *json_load_file(const char *path, size_t flags, json_error_t *error); | |
325 | json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error); | |
326 | ||
327 | ||
328 | /* encoding */ | |
329 | ||
330 | #define JSON_MAX_INDENT 0x1F | |
331 | #define JSON_INDENT(n) ((n) & JSON_MAX_INDENT) | |
332 | #define JSON_COMPACT 0x20 | |
333 | #define JSON_ENSURE_ASCII 0x40 | |
334 | #define JSON_SORT_KEYS 0x80 | |
335 | #define JSON_PRESERVE_ORDER 0x100 | |
336 | #define JSON_ENCODE_ANY 0x200 | |
337 | #define JSON_ESCAPE_SLASH 0x400 | |
338 | #define JSON_REAL_PRECISION(n) (((n) & 0x1F) << 11) | |
339 | #define JSON_EMBED 0x10000 | |
340 | ||
341 | typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data); | |
342 | ||
343 | char *json_dumps(const json_t *json, size_t flags); | |
344 | size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags); | |
345 | int json_dumpf(const json_t *json, FILE *output, size_t flags); | |
346 | int json_dumpfd(const json_t *json, int output, size_t flags); | |
347 | int json_dump_file(const json_t *json, const char *path, size_t flags); | |
348 | int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags); | |
349 | ||
350 | /* custom memory allocation */ | |
351 | ||
352 | typedef void *(*json_malloc_t)(size_t); | |
353 | typedef void (*json_free_t)(void *); | |
354 | ||
355 | void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn); | |
356 | void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn); | |
357 | ||
358 | #ifdef __cplusplus | |
359 | } | |
360 | #endif | |
361 | ||
362 | #endif |