]> mj.ucw.cz Git - libucw.git/blob - ucw-json/json.h
JSON: Remove unused includes
[libucw.git] / ucw-json / json.h
1 /*
2  *      UCW JSON Library
3  *
4  *      (c) 2015 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _UCW_JSON_JSON_H
11 #define _UCW_JSON_JSON_H
12
13 #include <ucw/mempool.h>
14 #include <ucw/fastbuf.h>
15
16 #ifdef CONFIG_UCW_CLEAN_ABI
17 #define json_array_append ucw_json_array_append
18 #define json_delete ucw_json_delete
19 #define json_new ucw_json_new
20 #define json_new_array ucw_json_new_array
21 #define json_new_node ucw_json_new_node
22 #define json_new_object ucw_json_new_object
23 #define json_next_token ucw_json_next_token
24 #define json_next_value ucw_json_next_value
25 #define json_object_get ucw_json_object_get
26 #define json_object_set ucw_json_object_set
27 #define json_parse ucw_json_parse
28 #define json_peek_token ucw_json_peek_token
29 #define json_pop ucw_json_pop
30 #define json_push ucw_json_push
31 #define json_reset ucw_json_reset
32 #define json_set_input ucw_json_set_input
33 #define json_set_output ucw_json_set_output
34 #define json_write ucw_json_write
35 #define json_write_value ucw_json_write_value
36 #endif
37
38 /***
39  * === JSON library context
40  *
41  * The context structure remembers the whole state of the JSON
42  * library. All JSON values are allocated from a memory pool associated
43  * with the context. By default, their lifetime is the same as that
44  * of the context.
45  *
46  * Alternatively, you can mark the current state of the context
47  * with json_push() and return to the marked state later using
48  * json_pop(). All JSON values created between these two operations
49  * are released afterwards. See json_push() for details.
50  ***/
51
52 /**
53  * The context is represented a pointer to this structure.
54  * The fields marked with [*] are publicly accessible, the rest is private.
55  **/
56 struct json_context {
57   // Memory management
58   struct mempool *pool;
59   struct mempool_state init_state;
60
61   // Parser context
62   struct fastbuf *in_fb;
63   uint in_line;                         // [*] Current line number
64   uint in_column;                       // [*] Current column number
65   bool in_eof;                          //     End of file was encountered
66   struct json_node *next_token;
67   struct json_node *trivial_token;
68   int next_char;
69
70   // Formatter context
71   struct fastbuf *out_fb;
72   uint out_indent;
73   uint format_options;                  // [*] Formatting options (a combination of JSON_FORMAT_xxx)
74 };
75
76 /** Creates a new JSON context. **/
77 struct json_context *json_new(void);
78
79 /** Deletes a JSON context, deallocating all memory associated with it. **/
80 void json_delete(struct json_context *js);
81
82 /**
83  * Recycles a JSON context. All state is reset, allocated objects are freed.
84  * This is equivalent to mp_delete() followed by mp_new(), but it is faster
85  * and the address of the context is preserved.
86  **/
87 void json_reset(struct json_context *js);
88
89 /**
90  * Push the current state of the context onto state stack.
91  *
92  * Between json_push() and the associated json_pop(), only newly
93  * created JSON values can be modified. Older values can be only
94  * inspected, never modified. In particular, new values cannot be
95  * inserted to old arrays nor objects.
96  *
97  * If you are using json_peek_token(), the saved tokens cannot
98  * be carried over push/pop boundary.
99  **/
100 void json_push(struct json_context *js);
101
102 /**
103  * Pop state of the context off state stack. All JSON values created
104  * since the state was saved by json_push() are released.
105  **/
106 void json_pop(struct json_context *js);
107
108 /***
109  * === JSON values
110  *
111  * Each JSON value is represented by <<struct json_node,struct json_node>>,
112  * which is either an elementary value (null, boolean, number, string),
113  * or a container (array, object) pointing to other values.
114  *
115  * A value can belong to multiple containers simultaneously, so in general,
116  * the relationships between values need not form a tree, but a directed
117  * acyclic graph.
118  *
119  * You are allowed to read contents of nodes directly, but construction
120  * and modification of nodes must be always performed using the appropriate
121  * library functions.
122  ***/
123
124 /** Node types **/
125 enum json_node_type {
126   JSON_INVALID,
127   JSON_NULL,
128   JSON_BOOLEAN,
129   JSON_NUMBER,
130   JSON_STRING,
131   JSON_ARRAY,
132   JSON_OBJECT,
133   // These are not real nodes, but raw tokens.
134   // They are not present in the tree of values, but you may see them
135   // if you call json_next_token() and friends.
136   JSON_BEGIN_ARRAY,
137   JSON_END_ARRAY,
138   JSON_BEGIN_OBJECT,
139   JSON_END_OBJECT,
140   JSON_NAME_SEP,
141   JSON_VALUE_SEP,
142   JSON_EOF,
143 };
144
145 /** Each value is represented by a single node. **/
146 struct json_node {
147   enum json_node_type type;
148   union {                               // Data specific to individual value types
149     bool boolean;
150     double number;
151     const char *string;
152     struct json_node **elements;        // Arrays: Growing array of values
153     struct json_pair *pairs;            // Objects: Growing array of pairs
154   };
155 };
156
157 /** Attributes of objects are stored as (key, value) pairs of this format. **/
158 struct json_pair {
159   const char *key;
160   struct json_node *value;
161   // FIXME: Hash table
162 };
163
164 // Used internally
165 struct json_node *json_new_node(struct json_context *js, enum json_node_type type);
166
167 /** Creates a new null value. **/
168 static inline struct json_node *json_new_null(struct json_context *js UNUSED)
169 {
170   static const struct json_node static_null = { .type = JSON_NULL };
171   return (struct json_node *) &static_null;
172 }
173
174 /** Creates a new boolean value. **/
175 static inline struct json_node *json_new_bool(struct json_context *js UNUSED, bool value)
176 {
177   static const struct json_node static_bool[2] = {
178     [0] = { .type = JSON_BOOLEAN, .boolean = 0 },
179     [1] = { .type = JSON_BOOLEAN, .boolean = 1 },
180   };
181   return (struct json_node *) &static_bool[value];
182 }
183
184 /** Creates a new numeric value. **/
185 static inline struct json_node *json_new_number(struct json_context *js, double value)
186 {
187   struct json_node *n = json_new_node(js, JSON_NUMBER);
188   n->number = value;
189   return n;
190 }
191
192 /** Creates a new string value. The @value is kept only as a reference. **/
193 static inline struct json_node *json_new_string_ref(struct json_context *js, const char *value)
194 {
195   struct json_node *n = json_new_node(js, JSON_STRING);
196   n->string = value;
197   return n;
198 }
199
200 /** Creates a new string value, making a private copy of @value. **/
201 static inline struct json_node *json_new_string(struct json_context *js, const char *value)
202 {
203   return json_new_string_ref(js, mp_strdup(js->pool, value));
204 }
205
206 /** Creates a new array value with no elements. **/
207 struct json_node *json_new_array(struct json_context *js);
208
209 /** Appends a new element to the given array. **/
210 void json_array_append(struct json_node *array, struct json_node *elt);
211
212 /** Creates a new object value with no attributes. **/
213 struct json_node *json_new_object(struct json_context *js);
214
215 /**
216  * Adds a new (@key, @value) pair to the given object. If @key is already
217  * present, the pair is replaced. If @value is NULL, no new pair is created
218  * and a pre-existing pair is deleted.
219  *
220  * The @key is referenced by the object, you must not free it during
221  * the lifetime of the JSON context.
222  *
223  * FIXME: Add json_copy_key().
224  **/
225 void json_object_set(struct json_node *n, const char *key, struct json_node *value);
226
227 /** Returns the value associated with @key, or NULL if no such value exists. **/
228 struct json_node *json_object_get(struct json_node *n, const char *key);
229
230 /***
231  * === Parser
232  *
233  * The simplest way to parse a complete JSON file is to call json_parse(),
234  * which returns a value tree representing the contents of the file.
235  *
236  * Alternatively, you can read the input token by token: call json_set_input()
237  * and then repeat json_next_token(). If you are parsing huge JSON files,
238  * you probably want to do json_push() first, then scan and process some
239  * tokens, and then json_pop().
240  ***/
241
242 /** Parses a JSON file from the given fastbuf stream. **/
243 struct json_node *json_parse(struct json_context *js, struct fastbuf *fb);
244
245 /** Selects the given fastbuf stream as parser input. **/
246 void json_set_input(struct json_context *js, struct fastbuf *in);
247
248 /** Reads the next token from the input. **/
249 struct json_node *json_next_token(struct json_context *js);
250
251 /** Reads the next token, but keeps it in the input. **/
252 struct json_node *json_peek_token(struct json_context *js);
253
254 /** Reads the next JSON value, including nested values. **/
255 struct json_node *json_next_value(struct json_context *js);
256
257 /***
258  * === Writer
259  *
260  * JSON files can be produced by simply calling json_write().
261  *
262  * If you want to generate the output on the fly (for example if it is huge),
263  * call json_set_output() and then iterate json_write_value().
264  *
265  * By default, we produce a single-line compact representation,
266  * but you can choose differently by setting the appropriate
267  * `format_options` in the `json_context`.
268  ***/
269
270 /** Writes a JSON file to the given fastbuf stream, containing the JSON value @n. **/
271 void json_write(struct json_context *js, struct fastbuf *fb, struct json_node *n);
272
273 /** Selects the given fastbuf stream as output. **/
274 void json_set_output(struct json_context *js, struct fastbuf *fb);
275
276 /** Writes a single JSON value to the output stream. **/
277 void json_write_value(struct json_context *js, struct json_node *n);
278
279 /** Formatting options. The `format_options` field in the context is a bitwise OR of these flags. **/
280 enum json_format_option {
281   JSON_FORMAT_ESCAPE_NONASCII = 1,      // Produce pure ASCII output by escaping all Unicode characters in strings
282   JSON_FORMAT_INDENT = 2,               // Produce pretty indented output
283 };
284
285 #endif