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