]> mj.ucw.cz Git - libucw.git/blob - ucw-json/json.h
ABI cleanups
[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/clists.h>
14 #include <ucw/slists.h>
15 #include <ucw/mempool.h>
16 #include <ucw/fastbuf.h>
17
18 #ifdef CONFIG_UCW_CLEAN_ABI
19 #define json_array_append ucw_json_array_append
20 #define json_delete ucw_json_delete
21 #define json_new ucw_json_new
22 #define json_new_array ucw_json_new_array
23 #define json_new_node ucw_json_new_node
24 #define json_new_object ucw_json_new_object
25 #define json_next_token ucw_json_next_token
26 #define json_next_value ucw_json_next_value
27 #define json_object_get ucw_json_object_get
28 #define json_object_set ucw_json_object_set
29 #define json_parse ucw_json_parse
30 #define json_peek_token ucw_json_peek_token
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  * === FIXME
40  ***/
41
42 struct json_context {
43   struct mempool *pool;
44   struct mempool_state init_state;
45
46   struct fastbuf *in_fb;
47   uint in_line;
48   uint in_column;
49   bool in_eof;
50   struct json_node *next_token;
51   struct json_node *trivial_token;
52   int next_char;
53
54   struct fastbuf *out_fb;
55   uint out_indent;
56   uint format_options;          // Public
57 };
58
59 struct json_context *json_new(void);
60 void json_delete(struct json_context *js);
61 void json_reset(struct json_context *js);
62
63 enum json_node_type {
64   JSON_INVALID,
65   JSON_NULL,
66   JSON_BOOLEAN,
67   JSON_NUMBER,
68   JSON_STRING,
69   JSON_ARRAY,
70   JSON_OBJECT,
71   // These are not real nodes, but raw tokens
72   JSON_BEGIN_ARRAY,
73   JSON_END_ARRAY,
74   JSON_BEGIN_OBJECT,
75   JSON_END_OBJECT,
76   JSON_NAME_SEP,
77   JSON_VALUE_SEP,
78   JSON_EOF,
79 };
80
81 struct json_node {
82   enum json_node_type type;
83   union {
84     bool boolean;
85     double number;
86     const char *string;
87     struct json_node **elements;        // Growing array
88     struct json_pair *pairs;            // Growing array
89   };
90 };
91
92 struct json_pair {
93   const char *key;
94   struct json_node *value;
95   // FIXME: Hash table
96 };
97
98 struct json_node *json_new_node(struct json_context *js, enum json_node_type type);
99
100 static inline struct json_node *json_new_null(struct json_context *js)
101 {
102   return json_new_node(js, JSON_NULL);
103 }
104
105 static inline struct json_node *json_new_bool(struct json_context *js, bool value)
106 {
107   struct json_node *n = json_new_node(js, JSON_BOOLEAN);
108   n->boolean = value;
109   return n;
110 }
111
112 static inline struct json_node *json_new_number(struct json_context *js, double value)
113 {
114   struct json_node *n = json_new_node(js, JSON_NUMBER);
115   n->number = value;
116   return n;
117 }
118
119 static inline struct json_node *json_new_string_ref(struct json_context *js, const char *value)
120 {
121   struct json_node *n = json_new_node(js, JSON_STRING);
122   n->string = value;
123   return n;
124 }
125
126 static inline struct json_node *json_new_string(struct json_context *js, const char *value)
127 {
128   return json_new_string_ref(js, mp_strdup(js->pool, value));
129 }
130
131 struct json_node *json_new_array(struct json_context *js);
132 void json_array_append(struct json_node *array, struct json_node *elt);
133
134 struct json_node *json_new_object(struct json_context *js);
135 // FIXME: key must not be freed
136 void json_object_set(struct json_node *n, const char *key, struct json_node *value);
137 struct json_node *json_object_get(struct json_node *n, const char *key);
138
139 void json_set_input(struct json_context *js, struct fastbuf *in);
140 struct json_node *json_peek_token(struct json_context *js);
141 struct json_node *json_next_token(struct json_context *js);
142
143 struct json_node *json_next_value(struct json_context *js);
144
145 struct json_node *json_parse(struct json_context *js, struct fastbuf *fb);
146
147 void json_set_output(struct json_context *js, struct fastbuf *fb);
148 void json_write_value(struct json_context *js, struct json_node *n);
149 void json_write(struct json_context *js, struct fastbuf *fb, struct json_node *n);
150
151 enum json_format_option {
152   JSON_FORMAT_ESCAPE_NONASCII = 1,
153   JSON_FORMAT_INDENT = 2,
154 };
155
156 #endif