Refactor decoder input stream
[jansson.git] / src / jansson.h
1 /*
2  * Copyright (c) 2009-2011 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
18 extern "C" {
19 #endif
20
21 /* version */
22
23 #define JANSSON_MAJOR_VERSION  1
24 #define JANSSON_MINOR_VERSION  3
25 #define JANSSON_MICRO_VERSION  0
26
27 /* Micro version is omitted if it's 0 */
28 #define JANSSON_VERSION  "1.3"
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
39 typedef 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
50 typedef struct {
51     json_type type;
52     size_t refcount;
53 } json_t;
54
55 #if JSON_INTEGER_IS_LONG_LONG
56 #define JSON_INTEGER_FORMAT "lld"
57 typedef long long json_int_t;
58 #else
59 #define JSON_INTEGER_FORMAT "ld"
60 typedef long json_int_t;
61 #endif /* JSON_INTEGER_IS_LONG_LONG */
62
63 #define json_typeof(json)      ((json)->type)
64 #define json_is_object(json)   (json && json_typeof(json) == JSON_OBJECT)
65 #define json_is_array(json)    (json && json_typeof(json) == JSON_ARRAY)
66 #define json_is_string(json)   (json && json_typeof(json) == JSON_STRING)
67 #define json_is_integer(json)  (json && json_typeof(json) == JSON_INTEGER)
68 #define json_is_real(json)     (json && json_typeof(json) == JSON_REAL)
69 #define json_is_number(json)   (json_is_integer(json) || json_is_real(json))
70 #define json_is_true(json)     (json && json_typeof(json) == JSON_TRUE)
71 #define json_is_false(json)    (json && json_typeof(json) == JSON_FALSE)
72 #define json_is_boolean(json)  (json_is_true(json) || json_is_false(json))
73 #define json_is_null(json)     (json && json_typeof(json) == JSON_NULL)
74
75 /* construction, destruction, reference counting */
76
77 json_t *json_object(void);
78 json_t *json_array(void);
79 json_t *json_string(const char *value);
80 json_t *json_string_nocheck(const char *value);
81 json_t *json_integer(json_int_t value);
82 json_t *json_real(double value);
83 json_t *json_true(void);
84 json_t *json_false(void);
85 json_t *json_null(void);
86
87 static JSON_INLINE
88 json_t *json_incref(json_t *json)
89 {
90     if(json && json->refcount != (size_t)-1)
91         ++json->refcount;
92     return json;
93 }
94
95 /* do not call json_delete directly */
96 void json_delete(json_t *json);
97
98 static JSON_INLINE
99 void json_decref(json_t *json)
100 {
101     if(json && json->refcount != (size_t)-1 && --json->refcount == 0)
102         json_delete(json);
103 }
104
105
106 /* error reporting */
107
108 #define JSON_ERROR_TEXT_LENGTH    160
109 #define JSON_ERROR_SOURCE_LENGTH   80
110
111 typedef struct {
112     int line;
113     int column;
114     int position;
115     char source[JSON_ERROR_SOURCE_LENGTH];
116     char text[JSON_ERROR_TEXT_LENGTH];
117 } json_error_t;
118
119
120 /* getters, setters, manipulation */
121
122 size_t json_object_size(const json_t *object);
123 json_t *json_object_get(const json_t *object, const char *key);
124 int json_object_set_new(json_t *object, const char *key, json_t *value);
125 int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
126 int json_object_del(json_t *object, const char *key);
127 int json_object_clear(json_t *object);
128 int json_object_update(json_t *object, json_t *other);
129 void *json_object_iter(json_t *object);
130 void *json_object_iter_at(json_t *object, const char *key);
131 void *json_object_iter_next(json_t *object, void *iter);
132 const char *json_object_iter_key(void *iter);
133 json_t *json_object_iter_value(void *iter);
134 int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
135
136 static JSON_INLINE
137 int json_object_set(json_t *object, const char *key, json_t *value)
138 {
139     return json_object_set_new(object, key, json_incref(value));
140 }
141
142 static JSON_INLINE
143 int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
144 {
145     return json_object_set_new_nocheck(object, key, json_incref(value));
146 }
147
148 static JSON_INLINE
149 int json_object_iter_set(json_t *object, void *iter, json_t *value)
150 {
151     return json_object_iter_set_new(object, iter, json_incref(value));
152 }
153
154 size_t json_array_size(const json_t *array);
155 json_t *json_array_get(const json_t *array, size_t index);
156 int json_array_set_new(json_t *array, size_t index, json_t *value);
157 int json_array_append_new(json_t *array, json_t *value);
158 int json_array_insert_new(json_t *array, size_t index, json_t *value);
159 int json_array_remove(json_t *array, size_t index);
160 int json_array_clear(json_t *array);
161 int json_array_extend(json_t *array, json_t *other);
162
163 static JSON_INLINE
164 int json_array_set(json_t *array, size_t index, json_t *value)
165 {
166     return json_array_set_new(array, index, json_incref(value));
167 }
168
169 static JSON_INLINE
170 int json_array_append(json_t *array, json_t *value)
171 {
172     return json_array_append_new(array, json_incref(value));
173 }
174
175 static JSON_INLINE
176 int json_array_insert(json_t *array, size_t index, json_t *value)
177 {
178     return json_array_insert_new(array, index, json_incref(value));
179 }
180
181 const char *json_string_value(const json_t *string);
182 json_int_t json_integer_value(const json_t *integer);
183 double json_real_value(const json_t *real);
184 double json_number_value(const json_t *json);
185
186 int json_string_set(json_t *string, const char *value);
187 int json_string_set_nocheck(json_t *string, const char *value);
188 int json_integer_set(json_t *integer, json_int_t value);
189 int json_real_set(json_t *real, double value);
190
191
192 /* pack, unpack */
193
194 json_t *json_pack(const char *fmt, ...);
195 json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...);
196 json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap);
197
198 #define JSON_VALIDATE_ONLY  0x1
199 #define JSON_STRICT         0x2
200
201 int json_unpack(json_t *root, const char *fmt, ...);
202 int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...);
203 int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);
204
205
206 /* equality */
207
208 int json_equal(json_t *value1, json_t *value2);
209
210
211 /* copying */
212
213 json_t *json_copy(json_t *value);
214 json_t *json_deep_copy(json_t *value);
215
216
217 /* loading, printing */
218
219 json_t *json_loads(const char *input, size_t flags, json_error_t *error);
220 json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
221 json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
222
223 #define JSON_INDENT(n)      (n & 0x1F)
224 #define JSON_COMPACT        0x20
225 #define JSON_ENSURE_ASCII   0x40
226 #define JSON_SORT_KEYS      0x80
227 #define JSON_PRESERVE_ORDER 0x100
228
229 char *json_dumps(const json_t *json, size_t flags);
230 int json_dumpf(const json_t *json, FILE *output, size_t flags);
231 int json_dump_file(const json_t *json, const char *path, size_t flags);
232
233
234 /* custom memory allocation */
235
236 typedef void *(*json_malloc_t)(size_t);
237 typedef void (*json_free_t)(void *);
238
239 void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn);
240
241 #ifdef __cplusplus
242 }
243 #endif
244
245 #endif