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