]> mj.ucw.cz Git - libucw.git/blob - ucw-json/json.h
5340a30e1ea57f15f4048b4f6ef94e08efc81c5f
[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   bool in_eof;
34   struct json_node *next_token;
35   struct json_node *trivial_token;
36   int next_char;
37 };
38
39 struct json_context *json_new(void);
40 void json_delete(struct json_context *js);
41 void json_reset(struct json_context *js);
42
43 enum json_node_type {
44   JSON_INVALID,
45   JSON_NULL,
46   JSON_BOOLEAN,
47   JSON_NUMBER,
48   JSON_STRING,
49   JSON_ARRAY,
50   JSON_OBJECT,
51   // These are not real nodes, but raw tokens
52   JSON_BEGIN_ARRAY,
53   JSON_END_ARRAY,
54   JSON_BEGIN_OBJECT,
55   JSON_END_OBJECT,
56   JSON_NAME_SEP,
57   JSON_VALUE_SEP,
58   JSON_EOF,
59 };
60
61 struct json_node {
62   enum json_node_type type;
63   union {
64     bool boolean;
65     double number;
66     const char *string;
67     struct json_node **elements;        // Growing array
68     struct json_pair *pairs;            // Growing array
69   };
70 };
71
72 struct json_pair {
73   const char *key;
74   struct json_node *value;
75   // FIXME: Hash table
76 };
77
78 struct json_node *json_new_node(struct json_context *js, enum json_node_type type);
79
80 static inline struct json_node *json_new_null(struct json_context *js)
81 {
82   return json_new_node(js, JSON_NULL);
83 }
84
85 static inline struct json_node *json_new_bool(struct json_context *js, bool value)
86 {
87   struct json_node *n = json_new_node(js, JSON_BOOLEAN);
88   n->boolean = value;
89   return n;
90 }
91
92 static inline struct json_node *json_new_number(struct json_context *js, double value)
93 {
94   struct json_node *n = json_new_node(js, JSON_NUMBER);
95   n->number = value;
96   return n;
97 }
98
99 static inline struct json_node *json_new_string_ref(struct json_context *js, const char *value)
100 {
101   struct json_node *n = json_new_node(js, JSON_STRING);
102   n->string = value;
103   return n;
104 }
105
106 static inline struct json_node *json_new_string(struct json_context *js, const char *value)
107 {
108   return json_new_string_ref(js, mp_strdup(js->pool, value));
109 }
110
111 struct json_node *json_new_array(struct json_context *js);
112 void json_array_append(struct json_node *array, struct json_node *elt);
113
114 struct json_node *json_new_object(struct json_context *js);
115 // FIXME: key must not be freed
116 void json_object_set(struct json_node *n, const char *key, struct json_node *value);
117 struct json_node *json_object_get(struct json_node *n, const char *key);
118
119 void json_set_input(struct json_context *js, struct fastbuf *in);
120 struct json_node *json_peek_token(struct json_context *js);
121 struct json_node *json_next_token(struct json_context *js);
122
123 struct json_node *json_next_object(struct json_context *js);
124
125 struct json_node *json_parse(struct json_context *js, struct fastbuf *fb);
126
127 #endif