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.
12 #include <stdlib.h> /* for size_t */
15 #include <jansson_config.h>
23 #define JANSSON_MAJOR_VERSION 2
24 #define JANSSON_MINOR_VERSION 3
25 #define JANSSON_MICRO_VERSION 99
27 /* Micro version is omitted if it's 0 */
28 #define JANSSON_VERSION "2.4-dev"
30 /* Version as a 3-byte hex number, e.g. 0x010201 == 1.2.1. Use this
31 for numeric comparisons, e.g. #if JANSSON_VERSION_HEX >= ... */
32 #define JANSSON_VERSION_HEX ((JANSSON_MAJOR_VERSION << 16) | \
33 (JANSSON_MINOR_VERSION << 8) | \
34 (JANSSON_MICRO_VERSION << 0))
55 #if JSON_INTEGER_IS_LONG_LONG
57 #define JSON_INTEGER_FORMAT "I64d"
59 #define JSON_INTEGER_FORMAT "lld"
61 typedef long long json_int_t;
63 #define JSON_INTEGER_FORMAT "ld"
64 typedef long json_int_t;
65 #endif /* JSON_INTEGER_IS_LONG_LONG */
67 #define json_typeof(json) ((json)->type)
68 #define json_is_object(json) (json && json_typeof(json) == JSON_OBJECT)
69 #define json_is_array(json) (json && json_typeof(json) == JSON_ARRAY)
70 #define json_is_string(json) (json && json_typeof(json) == JSON_STRING)
71 #define json_is_integer(json) (json && json_typeof(json) == JSON_INTEGER)
72 #define json_is_real(json) (json && json_typeof(json) == JSON_REAL)
73 #define json_is_number(json) (json_is_integer(json) || json_is_real(json))
74 #define json_is_true(json) (json && json_typeof(json) == JSON_TRUE)
75 #define json_is_false(json) (json && json_typeof(json) == JSON_FALSE)
76 #define json_is_boolean(json) (json_is_true(json) || json_is_false(json))
77 #define json_is_null(json) (json && json_typeof(json) == JSON_NULL)
79 /* construction, destruction, reference counting */
81 json_t *json_object(void);
82 json_t *json_array(void);
83 json_t *json_string(const char *value);
84 json_t *json_string_nocheck(const char *value);
85 json_t *json_integer(json_int_t value);
86 json_t *json_real(double value);
87 json_t *json_true(void);
88 json_t *json_false(void);
89 #define json_boolean(val) ((val) ? json_true() : json_false())
90 json_t *json_null(void);
93 json_t *json_incref(json_t *json)
95 if(json && json->refcount != (size_t)-1)
100 /* do not call json_delete directly */
101 void json_delete(json_t *json);
104 void json_decref(json_t *json)
106 if(json && json->refcount != (size_t)-1 && --json->refcount == 0)
111 /* error reporting */
113 #define JSON_ERROR_TEXT_LENGTH 160
114 #define JSON_ERROR_SOURCE_LENGTH 80
120 char source[JSON_ERROR_SOURCE_LENGTH];
121 char text[JSON_ERROR_TEXT_LENGTH];
125 /* getters, setters, manipulation */
127 size_t json_object_size(const json_t *object);
128 json_t *json_object_get(const json_t *object, const char *key);
129 int json_object_set_new(json_t *object, const char *key, json_t *value);
130 int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
131 int json_object_del(json_t *object, const char *key);
132 int json_object_clear(json_t *object);
133 int json_object_update(json_t *object, json_t *other);
134 int json_object_update_existing(json_t *object, json_t *other);
135 int json_object_update_missing(json_t *object, json_t *other);
136 void *json_object_iter(json_t *object);
137 void *json_object_iter_at(json_t *object, const char *key);
138 void *json_object_key_to_iter(const char *key);
139 void *json_object_iter_next(json_t *object, void *iter);
140 const char *json_object_iter_key(void *iter);
141 json_t *json_object_iter_value(void *iter);
142 int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
144 #define json_object_foreach(object, key, value) \
145 for(key = json_object_iter_key(json_object_iter(object)); \
146 key && (value = json_object_iter_value(json_object_key_to_iter(key))); \
147 key = json_object_iter_key(json_object_iter_next(object, json_object_key_to_iter(key))))
150 int json_object_set(json_t *object, const char *key, json_t *value)
152 return json_object_set_new(object, key, json_incref(value));
156 int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
158 return json_object_set_new_nocheck(object, key, json_incref(value));
162 int json_object_iter_set(json_t *object, void *iter, json_t *value)
164 return json_object_iter_set_new(object, iter, json_incref(value));
167 size_t json_array_size(const json_t *array);
168 json_t *json_array_get(const json_t *array, size_t index);
169 int json_array_set_new(json_t *array, size_t index, json_t *value);
170 int json_array_append_new(json_t *array, json_t *value);
171 int json_array_insert_new(json_t *array, size_t index, json_t *value);
172 int json_array_remove(json_t *array, size_t index);
173 int json_array_clear(json_t *array);
174 int json_array_extend(json_t *array, json_t *other);
177 int json_array_set(json_t *array, size_t index, json_t *value)
179 return json_array_set_new(array, index, json_incref(value));
183 int json_array_append(json_t *array, json_t *value)
185 return json_array_append_new(array, json_incref(value));
189 int json_array_insert(json_t *array, size_t index, json_t *value)
191 return json_array_insert_new(array, index, json_incref(value));
194 const char *json_string_value(const json_t *string);
195 json_int_t json_integer_value(const json_t *integer);
196 double json_real_value(const json_t *real);
197 double json_number_value(const json_t *json);
199 int json_string_set(json_t *string, const char *value);
200 int json_string_set_nocheck(json_t *string, const char *value);
201 int json_integer_set(json_t *integer, json_int_t value);
202 int json_real_set(json_t *real, double value);
207 json_t *json_pack(const char *fmt, ...);
208 json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...);
209 json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap);
211 #define JSON_VALIDATE_ONLY 0x1
212 #define JSON_STRICT 0x2
214 int json_unpack(json_t *root, const char *fmt, ...);
215 int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...);
216 int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);
221 int json_equal(json_t *value1, json_t *value2);
226 json_t *json_copy(json_t *value);
227 json_t *json_deep_copy(json_t *value);
232 #define JSON_REJECT_DUPLICATES 0x1
233 #define JSON_DISABLE_EOF_CHECK 0x2
234 #define JSON_DECODE_ANY 0x4
236 typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data);
238 json_t *json_loads(const char *input, size_t flags, json_error_t *error);
239 json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error);
240 json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
241 json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
242 json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error);
247 #define JSON_INDENT(n) (n & 0x1F)
248 #define JSON_COMPACT 0x20
249 #define JSON_ENSURE_ASCII 0x40
250 #define JSON_SORT_KEYS 0x80
251 #define JSON_PRESERVE_ORDER 0x100
252 #define JSON_ENCODE_ANY 0x200
253 #define JSON_ESCAPE_SLASH 0x400
255 typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
257 char *json_dumps(const json_t *json, size_t flags);
258 int json_dumpf(const json_t *json, FILE *output, size_t flags);
259 int json_dump_file(const json_t *json, const char *path, size_t flags);
260 int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags);
262 /* custom memory allocation */
264 typedef void *(*json_malloc_t)(size_t);
265 typedef void (*json_free_t)(void *);
267 void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn);