4 * (c) 2015 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
10 #ifndef _UCW_JSON_JSON_H
11 #define _UCW_JSON_JSON_H
13 #include <ucw/clists.h>
14 #include <ucw/slists.h>
15 #include <ucw/mempool.h>
16 #include <ucw/fastbuf.h>
18 #ifdef CONFIG_UCW_CLEAN_ABI
28 struct mempool_state init_state;
30 struct fastbuf *in_fb;
34 struct json_node *next_token;
35 struct json_node *trivial_token;
38 struct fastbuf *out_fb;
40 uint format_options; // Public
43 struct json_context *json_new(void);
44 void json_delete(struct json_context *js);
45 void json_reset(struct json_context *js);
55 // These are not real nodes, but raw tokens
66 enum json_node_type type;
71 struct json_node **elements; // Growing array
72 struct json_pair *pairs; // Growing array
78 struct json_node *value;
82 struct json_node *json_new_node(struct json_context *js, enum json_node_type type);
84 static inline struct json_node *json_new_null(struct json_context *js)
86 return json_new_node(js, JSON_NULL);
89 static inline struct json_node *json_new_bool(struct json_context *js, bool value)
91 struct json_node *n = json_new_node(js, JSON_BOOLEAN);
96 static inline struct json_node *json_new_number(struct json_context *js, double value)
98 struct json_node *n = json_new_node(js, JSON_NUMBER);
103 static inline struct json_node *json_new_string_ref(struct json_context *js, const char *value)
105 struct json_node *n = json_new_node(js, JSON_STRING);
110 static inline struct json_node *json_new_string(struct json_context *js, const char *value)
112 return json_new_string_ref(js, mp_strdup(js->pool, value));
115 struct json_node *json_new_array(struct json_context *js);
116 void json_array_append(struct json_node *array, struct json_node *elt);
118 struct json_node *json_new_object(struct json_context *js);
119 // FIXME: key must not be freed
120 void json_object_set(struct json_node *n, const char *key, struct json_node *value);
121 struct json_node *json_object_get(struct json_node *n, const char *key);
123 void json_set_input(struct json_context *js, struct fastbuf *in);
124 struct json_node *json_peek_token(struct json_context *js);
125 struct json_node *json_next_token(struct json_context *js);
127 struct json_node *json_next_value(struct json_context *js);
129 struct json_node *json_parse(struct json_context *js, struct fastbuf *fb);
131 void json_set_output(struct json_context *js, struct fastbuf *fb);
132 void json_write_value(struct json_context *js, struct json_node *n);
133 void json_write(struct json_context *js, struct fastbuf *fb, struct json_node *n);
135 enum json_format_option {
136 JSON_FORMAT_ESCAPE_NONASCII = 1,
137 JSON_FORMAT_INDENT = 2,