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