libtrap 1.17.1
jansson.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009-2014 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 <stdlib.h> /* for size_t */
13#include <stdarg.h>
14
15#ifndef JSON_INLINE
16# ifdef __cplusplus
17# define JSON_INLINE inline
18# else
19# define JSON_INLINE inline
20# endif
21#endif
22
23/* If your compiler supports the `long long` type and the strtoll()
24 library function, JSON_INTEGER_IS_LONG_LONG is defined to 1,
25 otherwise to 0. */
26#ifndef JSON_INTEGER_IS_LONG_LONG
27#define JSON_INTEGER_IS_LONG_LONG 1
28#endif
29
30/* If locale.h and localeconv() are available, define to 1,
31 otherwise to 0. */
32#ifndef JSON_HAVE_LOCALECONV
33#define JSON_HAVE_LOCALECONV 1
34#endif
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/* version */
41
42#define JANSSON_MAJOR_VERSION 2
43#define JANSSON_MINOR_VERSION 7
44#define JANSSON_MICRO_VERSION 0
45
46/* Micro version is omitted if it's 0 */
47#define JANSSON_VERSION "2.7"
48
49/* Version as a 3-byte hex number, e.g. 0x010201 == 1.2.1. Use this
50 for numeric comparisons, e.g. #if JANSSON_VERSION_HEX >= ... */
51#define JANSSON_VERSION_HEX ((JANSSON_MAJOR_VERSION << 16) | \
52 (JANSSON_MINOR_VERSION << 8) | \
53 (JANSSON_MICRO_VERSION << 0))
54
55
56/* types */
57
58typedef enum {
68
69typedef struct json_t {
71 size_t refcount;
73
74#ifndef JANSSON_USING_CMAKE /* disabled if using cmake */
75#if JSON_INTEGER_IS_LONG_LONG
76#define JSON_INTEGER_FORMAT "lld"
77typedef long long json_int_t;
78#else
79#define JSON_INTEGER_FORMAT "ld"
80typedef long json_int_t;
81#endif /* JSON_INTEGER_IS_LONG_LONG */
82#endif
83
84#define json_typeof(json) ((json)->type)
85#define json_is_object(json) ((json) && json_typeof(json) == JSON_OBJECT)
86#define json_is_array(json) ((json) && json_typeof(json) == JSON_ARRAY)
87#define json_is_string(json) ((json) && json_typeof(json) == JSON_STRING)
88#define json_is_integer(json) ((json) && json_typeof(json) == JSON_INTEGER)
89#define json_is_real(json) ((json) && json_typeof(json) == JSON_REAL)
90#define json_is_number(json) (json_is_integer(json) || json_is_real(json))
91#define json_is_true(json) ((json) && json_typeof(json) == JSON_TRUE)
92#define json_is_false(json) ((json) && json_typeof(json) == JSON_FALSE)
93#define json_boolean_value json_is_true
94#define json_is_boolean(json) (json_is_true(json) || json_is_false(json))
95#define json_is_null(json) ((json) && json_typeof(json) == JSON_NULL)
96
97/* construction, destruction, reference counting */
98
101json_t *json_string(const char *value);
102json_t *json_stringn(const char *value, size_t len);
103json_t *json_string_nocheck(const char *value);
104json_t *json_stringn_nocheck(const char *value, size_t len);
106json_t *json_real(double value);
109#define json_boolean(val) ((val) ? json_true() : json_false())
111
112static JSON_INLINE
113json_t *json_incref(json_t *json)
114{
115 if(json && json->refcount != (size_t)-1)
116 ++json->refcount;
117 return json;
118}
119
120/* do not call json_delete directly */
121void json_delete(json_t *json);
122
123static JSON_INLINE
124void json_decref(json_t *json)
125{
126 if(json && json->refcount != (size_t)-1 && --json->refcount == 0)
127 json_delete(json);
128}
129
130
131/* error reporting */
132
133#define JSON_ERROR_TEXT_LENGTH 160
134#define JSON_ERROR_SOURCE_LENGTH 80
135
136typedef struct {
137 int line;
143
144
145/* getters, setters, manipulation */
146
147void json_object_seed(size_t seed);
148size_t json_object_size(const json_t *object);
149json_t *json_object_get(const json_t *object, const char *key);
150int json_object_set_new(json_t *object, const char *key, json_t *value);
151int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
152int json_object_del(json_t *object, const char *key);
154int json_object_update(json_t *object, json_t *other);
158void *json_object_iter_at(json_t *object, const char *key);
159void *json_object_key_to_iter(const char *key);
160void *json_object_iter_next(json_t *object, void *iter);
161const char *json_object_iter_key(void *iter);
163int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
164
165#define json_object_foreach(object, key, value) \
166 for(key = json_object_iter_key(json_object_iter(object)); \
167 key && (value = json_object_iter_value(json_object_key_to_iter(key))); \
168 key = json_object_iter_key(json_object_iter_next(object, json_object_key_to_iter(key))))
169
170#define json_array_foreach(array, index, value) \
171 for(index = 0; \
172 index < json_array_size(array) && (value = json_array_get(array, index)); \
173 index++)
174
175static JSON_INLINE
176int json_object_set(json_t *object, const char *key, json_t *value)
177{
178 return json_object_set_new(object, key, json_incref(value));
179}
180
181static JSON_INLINE
182int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
183{
184 return json_object_set_new_nocheck(object, key, json_incref(value));
185}
186
187static JSON_INLINE
188int json_object_iter_set(json_t *object, void *iter, json_t *value)
189{
190 return json_object_iter_set_new(object, iter, json_incref(value));
191}
192
193size_t json_array_size(const json_t *array);
194json_t *json_array_get(const json_t *array, size_t index);
195int json_array_set_new(json_t *array, size_t index, json_t *value);
197int json_array_insert_new(json_t *array, size_t index, json_t *value);
198int json_array_remove(json_t *array, size_t index);
200int json_array_extend(json_t *array, json_t *other);
201
202static JSON_INLINE
203int json_array_set(json_t *array, size_t ind, json_t *value)
204{
205 return json_array_set_new(array, ind, json_incref(value));
206}
207
208static JSON_INLINE
209int json_array_append(json_t *array, json_t *value)
210{
211 return json_array_append_new(array, json_incref(value));
212}
213
214static JSON_INLINE
215int json_array_insert(json_t *array, size_t ind, json_t *value)
216{
217 return json_array_insert_new(array, ind, json_incref(value));
218}
219
220const char *json_string_value(const json_t *string);
221size_t json_string_length(const json_t *string);
223double json_real_value(const json_t *real);
224double json_number_value(const json_t *json);
225
226int json_string_set(json_t *string, const char *value);
227int json_string_setn(json_t *string, const char *value, size_t len);
228int json_string_set_nocheck(json_t *string, const char *value);
229int json_string_setn_nocheck(json_t *string, const char *value, size_t len);
230int json_integer_set(json_t *integer, json_int_t value);
231int json_real_set(json_t *real, double value);
232
233/* pack, unpack */
234
235json_t *json_pack(const char *fmt, ...);
236json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...);
237json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap);
238
239#define JSON_VALIDATE_ONLY 0x1
240#define JSON_STRICT 0x2
241
242int json_unpack(json_t *root, const char *fmt, ...);
243int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...);
244int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);
245
246
247/* equality */
248
249int json_equal(json_t *value1, json_t *value2);
250
251
252/* copying */
253
256
257
258/* decoding */
259
260#define JSON_REJECT_DUPLICATES 0x1
261#define JSON_DISABLE_EOF_CHECK 0x2
262#define JSON_DECODE_ANY 0x4
263#define JSON_DECODE_INT_AS_REAL 0x8
264#define JSON_ALLOW_NUL 0x10
265
266typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data);
267
268json_t *json_loads(const char *input, size_t flags, json_error_t *error);
269json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error);
270json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
271json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
272json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error);
273
274
275/* encoding */
276
277#define JSON_MAX_INDENT 0x1F
278#define JSON_INDENT(n) ((n) & JSON_MAX_INDENT)
279#define JSON_COMPACT 0x20
280#define JSON_ENSURE_ASCII 0x40
281#define JSON_SORT_KEYS 0x80
282#define JSON_PRESERVE_ORDER 0x100
283#define JSON_ENCODE_ANY 0x200
284#define JSON_ESCAPE_SLASH 0x400
285#define JSON_REAL_PRECISION(n) (((n) & 0x1F) << 11)
286
287typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
288
289char *json_dumps(const json_t *json, size_t flags);
290int json_dumpf(const json_t *json, FILE *output, size_t flags);
291int json_dump_file(const json_t *json, const char *path, size_t flags);
292int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags);
293
294/* custom memory allocation */
295
296typedef void *(*json_malloc_t)(size_t);
297typedef void (*json_free_t)(void *);
298
300
301#ifdef __cplusplus
302}
303#endif
304
305#endif
json_t * json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error)
size_t(* json_load_callback_t)(void *buffer, size_t buflen, void *data)
Definition: jansson.h:266
void * json_object_iter(json_t *object)
json_t * json_object_get(const json_t *object, const char *key)
json_t * json_array(void)
#define JSON_INLINE
Definition: jansson.h:19
int json_array_insert_new(json_t *array, size_t index, json_t *value)
int json_string_set_nocheck(json_t *string, const char *value)
void(* json_free_t)(void *)
Definition: jansson.h:297
#define JSON_ERROR_SOURCE_LENGTH
Definition: jansson.h:134
int json_equal(json_t *value1, json_t *value2)
int json_array_extend(json_t *array, json_t *other)
void * json_object_iter_at(json_t *object, const char *key)
int json_array_set_new(json_t *array, size_t index, json_t *value)
int json_array_remove(json_t *array, size_t index)
int json_string_setn(json_t *string, const char *value, size_t len)
int json_dumpf(const json_t *json, FILE *output, size_t flags)
#define JSON_ERROR_TEXT_LENGTH
Definition: jansson.h:133
int(* json_dump_callback_t)(const char *buffer, size_t size, void *data)
Definition: jansson.h:287
int json_array_append_new(json_t *array, json_t *value)
json_t * json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap)
json_t * json_pack(const char *fmt,...)
size_t json_string_length(const json_t *string)
json_t * json_loadf(FILE *input, size_t flags, json_error_t *error)
const char * json_string_value(const json_t *string)
int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags)
json_t * json_deep_copy(const json_t *value)
size_t json_array_size(const json_t *array)
json_t * json_string_nocheck(const char *value)
void * json_object_iter_next(json_t *object, void *iter)
int json_integer_set(json_t *integer, json_int_t value)
int json_object_iter_set_new(json_t *object, void *iter, json_t *value)
json_t * json_loads(const char *input, size_t flags, json_error_t *error)
void *(* json_malloc_t)(size_t)
Definition: jansson.h:296
int json_unpack(json_t *root, const char *fmt,...)
char * json_dumps(const json_t *json, size_t flags)
int json_object_update_missing(json_t *object, json_t *other)
int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value)
int json_object_del(json_t *object, const char *key)
json_t * json_string(const char *value)
void json_object_seed(size_t seed)
int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt,...)
json_t * json_true(void)
void * json_object_key_to_iter(const char *key)
void json_delete(json_t *json)
int json_string_set(json_t *string, const char *value)
json_t * json_pack_ex(json_error_t *error, size_t flags, const char *fmt,...)
json_t * json_null(void)
int json_object_update(json_t *object, json_t *other)
json_t * json_load_file(const char *path, size_t flags, json_error_t *error)
long long json_int_t
Definition: jansson.h:77
json_t * json_array_get(const json_t *array, size_t index)
int json_object_update_existing(json_t *object, json_t *other)
json_type
Definition: jansson.h:58
@ JSON_STRING
Definition: jansson.h:61
@ JSON_NULL
Definition: jansson.h:66
@ JSON_TRUE
Definition: jansson.h:64
@ JSON_OBJECT
Definition: jansson.h:59
@ JSON_INTEGER
Definition: jansson.h:62
@ JSON_ARRAY
Definition: jansson.h:60
@ JSON_FALSE
Definition: jansson.h:65
@ JSON_REAL
Definition: jansson.h:63
json_t * json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error)
int json_object_clear(json_t *object)
json_t * json_real(double value)
int json_dump_file(const json_t *json, const char *path, size_t flags)
json_t * json_false(void)
double json_real_value(const json_t *real)
json_t * json_stringn(const char *value, size_t len)
size_t json_object_size(const json_t *object)
int json_array_clear(json_t *array)
int json_real_set(json_t *real, double value)
json_t * json_stringn_nocheck(const char *value, size_t len)
json_t * json_object(void)
json_t * json_object_iter_value(void *iter)
json_t * json_integer(json_int_t value)
json_t * json_copy(json_t *value)
json_int_t json_integer_value(const json_t *integer)
double json_number_value(const json_t *json)
void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn)
int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap)
int json_object_set_new(json_t *object, const char *key, json_t *value)
int json_string_setn_nocheck(json_t *string, const char *value, size_t len)
const char * json_object_iter_key(void *iter)
int column
Definition: jansson.h:138
int position
Definition: jansson.h:139
Definition: jansson.h:69
json_type type
Definition: jansson.h:70
size_t refcount
Definition: jansson.h:71