src/cx/json.h

changeset 1000
1aecddf7e209
parent 996
333155f234c4
child 1002
1483c47063a8
equal deleted inserted replaced
999:84fc42b04d3b 1000:1aecddf7e209
75 CX_JSON_NULL, 75 CX_JSON_NULL,
76 CX_JSON_TRUE, 76 CX_JSON_TRUE,
77 CX_JSON_FALSE 77 CX_JSON_FALSE
78 }; 78 };
79 79
80 enum cx_json_reader_type {
81 CX_JSON_READER_OBJECT_BEGIN,
82 CX_JSON_READER_OBJECT_END,
83 CX_JSON_READER_ARRAY_BEGIN,
84 CX_JSON_READER_ARRAY_END,
85 CX_JSON_READER_STRING,
86 CX_JSON_READER_INTEGER,
87 CX_JSON_READER_NUMBER,
88 CX_JSON_READER_LITERAL
89 };
90
91 typedef enum cx_json_token_type CxJsonTokenType; 80 typedef enum cx_json_token_type CxJsonTokenType;
92 typedef enum cx_json_value_type CxJsonValueType; 81 typedef enum cx_json_value_type CxJsonValueType;
93 typedef enum cx_json_reader_type CxJsonReaderType;
94 82
95 typedef struct cx_json_s CxJson; 83 typedef struct cx_json_s CxJson;
96 typedef struct cx_json_token_s CxJsonToken; 84 typedef struct cx_json_token_s CxJsonToken;
97 85
98 typedef struct cx_json_value_s CxJsonValue; 86 typedef struct cx_json_value_s CxJsonValue;
104 typedef double CxJsonNumber; 92 typedef double CxJsonNumber;
105 typedef enum cx_json_literal CxJsonLiteral; 93 typedef enum cx_json_literal CxJsonLiteral;
106 94
107 typedef struct cx_json_obj_value_s CxJsonObjValue; 95 typedef struct cx_json_obj_value_s CxJsonObjValue;
108 96
109 struct cx_json_token_s {
110 CxJsonTokenType tokentype;
111 const char *content;
112 size_t length;
113 size_t alloc;
114 };
115
116 struct cx_json_s {
117 const CxAllocator *allocator;
118 const char *buffer;
119 size_t size;
120 size_t pos;
121
122 CxJsonToken uncompleted;
123 int tokenizer_escape;
124
125 int *states;
126 size_t nstates;
127 size_t states_alloc;
128 int states_internal[8];
129
130 CxJsonToken reader_token;
131 CxJsonReaderType reader_type;
132 int value_ready;
133 char *value_name;
134 size_t value_name_len;
135 char *value_str;
136 size_t value_str_len;
137 int64_t value_int;
138 double value_double;
139
140 CxJsonValue **readvalue_stack;
141 unsigned readvalue_nelm;
142 unsigned readvalue_alloc;
143 CxJsonValue *read_value;
144 int readvalue_initialized;
145
146 unsigned reader_array_alloc;
147
148 int error;
149 };
150 97
151 struct cx_json_array_s { 98 struct cx_json_array_s {
152 CX_ARRAY_DECLARE(CxJsonValue*, array); 99 CX_ARRAY_DECLARE(CxJsonValue*, array);
153 }; 100 };
154 101
172 CxJsonNumber number; 119 CxJsonNumber number;
173 CxJsonLiteral literal; 120 CxJsonLiteral literal;
174 } value; 121 } value;
175 }; 122 };
176 123
177 124 struct cx_json_token_s {
178 cx_attr_nonnull_arg(2) 125 CxJsonTokenType tokentype;
179 void cxJsonInit(const CxAllocator *allocator, CxJson *json); 126 const char *content;
127 size_t length;
128 size_t alloc;
129 };
130
131 struct cx_json_s {
132 const CxAllocator *allocator;
133 const char *buffer;
134 size_t size;
135 size_t pos;
136
137 CxJsonToken uncompleted;
138
139 /**
140 * A pointer to an intermediate state of the currently parsed value.
141 *
142 * Never access this value manually.
143 */
144 CxJsonValue *parsed;
145
146 /**
147 * State stack.
148 */
149 CX_ARRAY_DECLARE_SIZED(int, states, unsigned);
150
151 /**
152 * Value buffer stack.
153 */
154 CX_ARRAY_DECLARE_SIZED(CxJsonValue*, vbuf, unsigned);
155
156 /**
157 * Internally reserved memory for the state stack.
158 */
159 int states_internal[8];
160
161 /**
162 * Internally reserved memory for the value buffer stack.
163 */
164 CxJsonValue* vbuf_internal[8];
165
166 int error; // TODO: currently unused
167 bool tokenizer_escape; // TODO: check if it can be replaced with look-behind
168 };
169
170 cx_attr_nonnull_arg(1)
171 void cxJsonInit(CxJson *json, const CxAllocator *allocator);
180 172
181 cx_attr_nonnull 173 cx_attr_nonnull
182 void cxJsonDestroy(CxJson *json); 174 void cxJsonDestroy(CxJson *json);
183 175
184 cx_attr_nonnull 176 cx_attr_nonnull
248 } 240 }
249 #endif 241 #endif
250 242
251 void cxJsonValueFree(CxJsonValue *value); 243 void cxJsonValueFree(CxJsonValue *value);
252 244
253 // TODO: if the CxJsonValue was a returned value, we could reference cxJsonValueFree() as deallocator
254 cx_attr_nonnull 245 cx_attr_nonnull
255 int cxJsonNext(CxJson *json, CxJsonValue **value); 246 int cxJsonNext(CxJson *json, CxJsonValue **value);
256 247
257 cx_attr_nonnull 248 cx_attr_nonnull
258 static inline bool cxJsonIsObject(CxJsonValue *value) { 249 static inline bool cxJsonIsObject(CxJsonValue *value) {

mercurial