Enhance error reporting
[jansson.git] / src / jansson.h
1 /*
2  * Copyright (c) 2009, 2010 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 <jansson_config.h>
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 /* types */
20
21 typedef enum {
22     JSON_OBJECT,
23     JSON_ARRAY,
24     JSON_STRING,
25     JSON_INTEGER,
26     JSON_REAL,
27     JSON_TRUE,
28     JSON_FALSE,
29     JSON_NULL
30 } json_type;
31
32 typedef struct {
33     json_type type;
34     size_t refcount;
35 } json_t;
36
37 #if JSON_INTEGER_IS_LONG_LONG
38 #define JSON_INTEGER_FORMAT "lld"
39 typedef long long json_int_t;
40 #else
41 #define JSON_INTEGER_FORMAT "ld"
42 typedef long json_int_t;
43 #endif /* JSON_INTEGER_IS_LONG_LONG */
44
45 #define json_typeof(json)      ((json)->type)
46 #define json_is_object(json)   (json && json_typeof(json) == JSON_OBJECT)
47 #define json_is_array(json)    (json && json_typeof(json) == JSON_ARRAY)
48 #define json_is_string(json)   (json && json_typeof(json) == JSON_STRING)
49 #define json_is_integer(json)  (json && json_typeof(json) == JSON_INTEGER)
50 #define json_is_real(json)     (json && json_typeof(json) == JSON_REAL)
51 #define json_is_number(json)   (json_is_integer(json) || json_is_real(json))
52 #define json_is_true(json)     (json && json_typeof(json) == JSON_TRUE)
53 #define json_is_false(json)    (json && json_typeof(json) == JSON_FALSE)
54 #define json_is_boolean(json)  (json_is_true(json) || json_is_false(json))
55 #define json_is_null(json)     (json && json_typeof(json) == JSON_NULL)
56
57 /* construction, destruction, reference counting */
58
59 json_t *json_object(void);
60 json_t *json_array(void);
61 json_t *json_string(const char *value);
62 json_t *json_string_nocheck(const char *value);
63 json_t *json_integer(json_int_t value);
64 json_t *json_real(double value);
65 json_t *json_true(void);
66 json_t *json_false(void);
67 json_t *json_null(void);
68
69 static JSON_INLINE
70 json_t *json_incref(json_t *json)
71 {
72     if(json && json->refcount != (size_t)-1)
73         ++json->refcount;
74     return json;
75 }
76
77 /* do not call json_delete directly */
78 void json_delete(json_t *json);
79
80 static JSON_INLINE
81 void json_decref(json_t *json)
82 {
83     if(json && json->refcount != (size_t)-1 && --json->refcount == 0)
84         json_delete(json);
85 }
86
87
88 /* error reporting */
89
90 #define JSON_ERROR_TEXT_LENGTH    160
91 #define JSON_ERROR_SOURCE_LENGTH   80
92
93 typedef struct {
94     char text[JSON_ERROR_TEXT_LENGTH];
95     int line;
96     int column;
97     char source[JSON_ERROR_SOURCE_LENGTH];
98 } json_error_t;
99
100
101 /* getters, setters, manipulation */
102
103 size_t json_object_size(const json_t *object);
104 json_t *json_object_get(const json_t *object, const char *key);
105 int json_object_set_new(json_t *object, const char *key, json_t *value);
106 int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
107 int json_object_del(json_t *object, const char *key);
108 int json_object_clear(json_t *object);
109 int json_object_update(json_t *object, json_t *other);
110 void *json_object_iter(json_t *object);
111 void *json_object_iter_at(json_t *object, const char *key);
112 void *json_object_iter_next(json_t *object, void *iter);
113 const char *json_object_iter_key(void *iter);
114 json_t *json_object_iter_value(void *iter);
115 int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
116
117 static JSON_INLINE
118 int json_object_set(json_t *object, const char *key, json_t *value)
119 {
120     return json_object_set_new(object, key, json_incref(value));
121 }
122
123 static JSON_INLINE
124 int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
125 {
126     return json_object_set_new_nocheck(object, key, json_incref(value));
127 }
128
129 static JSON_INLINE
130 int json_object_iter_set(json_t *object, void *iter, json_t *value)
131 {
132     return json_object_iter_set_new(object, iter, json_incref(value));
133 }
134
135 size_t json_array_size(const json_t *array);
136 json_t *json_array_get(const json_t *array, size_t index);
137 int json_array_set_new(json_t *array, size_t index, json_t *value);
138 int json_array_append_new(json_t *array, json_t *value);
139 int json_array_insert_new(json_t *array, size_t index, json_t *value);
140 int json_array_remove(json_t *array, size_t index);
141 int json_array_clear(json_t *array);
142 int json_array_extend(json_t *array, json_t *other);
143
144 static JSON_INLINE
145 int json_array_set(json_t *array, size_t index, json_t *value)
146 {
147     return json_array_set_new(array, index, json_incref(value));
148 }
149
150 static JSON_INLINE
151 int json_array_append(json_t *array, json_t *value)
152 {
153     return json_array_append_new(array, json_incref(value));
154 }
155
156 static JSON_INLINE
157 int json_array_insert(json_t *array, size_t index, json_t *value)
158 {
159     return json_array_insert_new(array, index, json_incref(value));
160 }
161
162 const char *json_string_value(const json_t *string);
163 json_int_t json_integer_value(const json_t *integer);
164 double json_real_value(const json_t *real);
165 double json_number_value(const json_t *json);
166
167 int json_string_set(json_t *string, const char *value);
168 int json_string_set_nocheck(json_t *string, const char *value);
169 int json_integer_set(json_t *integer, json_int_t value);
170 int json_real_set(json_t *real, double value);
171
172 json_t *json_pack(json_error_t *error, const char *fmt, ...);
173 int json_unpack(json_t *root, json_error_t *error, const char *fmt, ...);
174
175 /* equality */
176
177 int json_equal(json_t *value1, json_t *value2);
178
179
180 /* copying */
181
182 json_t *json_copy(json_t *value);
183 json_t *json_deep_copy(json_t *value);
184
185
186 /* loading, printing */
187
188 json_t *json_loads(const char *input, size_t flags, json_error_t *error);
189 json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
190 json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
191
192 #define JSON_INDENT(n)      (n & 0x1F)
193 #define JSON_COMPACT        0x20
194 #define JSON_ENSURE_ASCII   0x40
195 #define JSON_SORT_KEYS      0x80
196 #define JSON_PRESERVE_ORDER 0x100
197
198 char *json_dumps(const json_t *json, size_t flags);
199 int json_dumpf(const json_t *json, FILE *output, size_t flags);
200 int json_dump_file(const json_t *json, const char *path, size_t flags);
201
202 #ifdef __cplusplus
203 }
204 #endif
205
206 #endif