fixed makefile
[public/netxms.git] / include / jansson.h
CommitLineData
d41268ba
VK
1/*
2 * Copyright (c) 2009-2013 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#include <jansson_config.h>
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21/* version */
22
23#define JANSSON_MAJOR_VERSION 2
24#define JANSSON_MINOR_VERSION 6
25#define JANSSON_MICRO_VERSION 0
26
27/* Micro version is omitted if it's 0 */
28#define JANSSON_VERSION "2.6"
29
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))
35
36
37/* types */
38
39typedef enum {
40 JSON_OBJECT,
41 JSON_ARRAY,
42 JSON_STRING,
43 JSON_INTEGER,
44 JSON_REAL,
45 JSON_TRUE,
46 JSON_FALSE,
47 JSON_NULL
48} json_type;
49
50typedef struct json_t {
51 json_type type;
52 size_t refcount;
53} json_t;
54
55#ifndef JANSSON_USING_CMAKE /* disabled if using cmake */
56#if JSON_INTEGER_IS_LONG_LONG
57#ifdef _WIN32
58#define JSON_INTEGER_FORMAT "I64d"
59#else
60#define JSON_INTEGER_FORMAT "lld"
61#endif
62typedef long long json_int_t;
63#else
64#define JSON_INTEGER_FORMAT "ld"
65typedef long json_int_t;
66#endif /* JSON_INTEGER_IS_LONG_LONG */
67#endif
68
69#define json_typeof(json) ((json)->type)
70#define json_is_object(json) (json && json_typeof(json) == JSON_OBJECT)
71#define json_is_array(json) (json && json_typeof(json) == JSON_ARRAY)
72#define json_is_string(json) (json && json_typeof(json) == JSON_STRING)
73#define json_is_integer(json) (json && json_typeof(json) == JSON_INTEGER)
74#define json_is_real(json) (json && json_typeof(json) == JSON_REAL)
75#define json_is_number(json) (json_is_integer(json) || json_is_real(json))
76#define json_is_true(json) (json && json_typeof(json) == JSON_TRUE)
77#define json_is_false(json) (json && json_typeof(json) == JSON_FALSE)
78#define json_is_boolean(json) (json_is_true(json) || json_is_false(json))
79#define json_is_null(json) (json && json_typeof(json) == JSON_NULL)
80
81/* construction, destruction, reference counting */
82
83json_t *json_object(void);
84json_t *json_array(void);
85json_t *json_string(const char *value);
86json_t *json_string_nocheck(const char *value);
87json_t *json_integer(json_int_t value);
88json_t *json_real(double value);
89json_t *json_true(void);
90json_t *json_false(void);
91#define json_boolean(val) ((val) ? json_true() : json_false())
92json_t *json_null(void);
93
94static JSON_INLINE
95json_t *json_incref(json_t *json)
96{
97 if(json && json->refcount != (size_t)-1)
98 ++json->refcount;
99 return json;
100}
101
102/* do not call json_delete directly */
103void json_delete(json_t *json);
104
105static JSON_INLINE
106void json_decref(json_t *json)
107{
108 if(json && json->refcount != (size_t)-1 && --json->refcount == 0)
109 json_delete(json);
110}
111
112
113/* error reporting */
114
115#define JSON_ERROR_TEXT_LENGTH 160
116#define JSON_ERROR_SOURCE_LENGTH 80
117
118typedef struct {
119 int line;
120 int column;
121 int position;
122 char source[JSON_ERROR_SOURCE_LENGTH];
123 char text[JSON_ERROR_TEXT_LENGTH];
124} json_error_t;
125
126
127/* getters, setters, manipulation */
128
129void json_object_seed(size_t seed);
130size_t json_object_size(const json_t *object);
131json_t *json_object_get(const json_t *object, const char *key);
132int json_object_set_new(json_t *object, const char *key, json_t *value);
133int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
134int json_object_del(json_t *object, const char *key);
135int json_object_clear(json_t *object);
136int json_object_update(json_t *object, json_t *other);
137int json_object_update_existing(json_t *object, json_t *other);
138int json_object_update_missing(json_t *object, json_t *other);
139void *json_object_iter(json_t *object);
140void *json_object_iter_at(json_t *object, const char *key);
141void *json_object_key_to_iter(const char *key);
142void *json_object_iter_next(json_t *object, void *iter);
143const char *json_object_iter_key(void *iter);
144json_t *json_object_iter_value(void *iter);
145int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
146
147#define json_object_foreach(object, key, value) \
148 for(key = json_object_iter_key(json_object_iter(object)); \
149 key && (value = json_object_iter_value(json_object_key_to_iter(key))); \
150 key = json_object_iter_key(json_object_iter_next(object, json_object_key_to_iter(key))))
151
152#define json_array_foreach(array, index, value) \
153 for(index = 0; \
154 index < json_array_size(array) && (value = json_array_get(array, index)); \
155 index++)
156
157static JSON_INLINE
158int json_object_set(json_t *object, const char *key, json_t *value)
159{
160 return json_object_set_new(object, key, json_incref(value));
161}
162
163static JSON_INLINE
164int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
165{
166 return json_object_set_new_nocheck(object, key, json_incref(value));
167}
168
169static JSON_INLINE
170int json_object_iter_set(json_t *object, void *iter, json_t *value)
171{
172 return json_object_iter_set_new(object, iter, json_incref(value));
173}
174
175size_t json_array_size(const json_t *array);
176json_t *json_array_get(const json_t *array, size_t index);
177int json_array_set_new(json_t *array, size_t index, json_t *value);
178int json_array_append_new(json_t *array, json_t *value);
179int json_array_insert_new(json_t *array, size_t index, json_t *value);
180int json_array_remove(json_t *array, size_t index);
181int json_array_clear(json_t *array);
182int json_array_extend(json_t *array, json_t *other);
183
184static JSON_INLINE
185int json_array_set(json_t *array, size_t ind, json_t *value)
186{
187 return json_array_set_new(array, ind, json_incref(value));
188}
189
190static JSON_INLINE
191int json_array_append(json_t *array, json_t *value)
192{
193 return json_array_append_new(array, json_incref(value));
194}
195
196static JSON_INLINE
197int json_array_insert(json_t *array, size_t ind, json_t *value)
198{
199 return json_array_insert_new(array, ind, json_incref(value));
200}
201
202const char *json_string_value(const json_t *string);
203json_int_t json_integer_value(const json_t *integer);
204double json_real_value(const json_t *real);
205double json_number_value(const json_t *json);
206
207int json_string_set(json_t *string, const char *value);
208int json_string_set_nocheck(json_t *string, const char *value);
209int json_integer_set(json_t *integer, json_int_t value);
210int json_real_set(json_t *real, double value);
211
212
213/* pack, unpack */
214
215json_t *json_pack(const char *fmt, ...);
216json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...);
217json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap);
218
219#define JSON_VALIDATE_ONLY 0x1
220#define JSON_STRICT 0x2
221
222int json_unpack(json_t *root, const char *fmt, ...);
223int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...);
224int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);
225
226
227/* equality */
228
229int json_equal(json_t *value1, json_t *value2);
230
231
232/* copying */
233
234json_t *json_copy(json_t *value);
235json_t *json_deep_copy(const json_t *value);
236
237
238/* decoding */
239
240#define JSON_REJECT_DUPLICATES 0x1
241#define JSON_DISABLE_EOF_CHECK 0x2
242#define JSON_DECODE_ANY 0x4
243#define JSON_DECODE_INT_AS_REAL 0x8
244
245typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data);
246
247json_t *json_loads(const char *input, size_t flags, json_error_t *error);
248json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error);
249json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
250json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
251json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error);
252
253
254/* encoding */
255
256#define JSON_INDENT(n) (n & 0x1F)
257#define JSON_COMPACT 0x20
258#define JSON_ENSURE_ASCII 0x40
259#define JSON_SORT_KEYS 0x80
260#define JSON_PRESERVE_ORDER 0x100
261#define JSON_ENCODE_ANY 0x200
262#define JSON_ESCAPE_SLASH 0x400
263
264typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
265
266char *json_dumps(const json_t *json, size_t flags);
267int json_dumpf(const json_t *json, FILE *output, size_t flags);
268int json_dump_file(const json_t *json, const char *path, size_t flags);
269int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags);
270
271/* custom memory allocation */
272
273typedef void *(*json_malloc_t)(size_t);
274typedef void (*json_free_t)(void *);
275
276void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn);
277
278#ifdef __cplusplus
279}
280#endif
281
282#endif