src/cx/json.h

changeset 965
dfdfedbe2c86
parent 954
a1d87e8fff6d
child 966
1aa7ec3e46e7
equal deleted inserted replaced
964:3860f509fcbe 965:dfdfedbe2c86
67 CX_JSON_INTEGER, // TODO: the spec does not know integer types 67 CX_JSON_INTEGER, // TODO: the spec does not know integer types
68 CX_JSON_NUMBER, 68 CX_JSON_NUMBER,
69 CX_JSON_LITERAL 69 CX_JSON_LITERAL
70 }; 70 };
71 71
72 enum cx_json_literal_type { 72 enum cx_json_literal {
73 CX_JSON_NULL, 73 CX_JSON_NULL,
74 CX_JSON_TRUE, 74 CX_JSON_TRUE,
75 CX_JSON_FALSE 75 CX_JSON_FALSE
76 }; 76 };
77 77
86 CX_JSON_READER_LITERAL 86 CX_JSON_READER_LITERAL
87 }; 87 };
88 88
89 typedef enum cx_json_token_type CxJsonTokenType; 89 typedef enum cx_json_token_type CxJsonTokenType;
90 typedef enum cx_json_value_type CxJsonValueType; 90 typedef enum cx_json_value_type CxJsonValueType;
91 typedef enum cx_json_literal_type CxJsonLiteralType;
92 typedef enum cx_json_reader_type CxJsonReaderType; 91 typedef enum cx_json_reader_type CxJsonReaderType;
93 92
94 typedef struct cx_json_s CxJson; 93 typedef struct cx_json_s CxJson;
95 typedef struct cx_json_token_s CxJsonToken; 94 typedef struct cx_json_token_s CxJsonToken;
96 95
97 typedef struct cx_json_value_s CxJsonValue; 96 typedef struct cx_json_value_s CxJsonValue;
98 97
99 typedef struct cx_json_array_s CxJsonArray; 98 typedef struct cx_json_array_s CxJsonArray;
100 typedef struct cx_json_object_s CxJsonObject; 99 typedef struct cx_json_object_s CxJsonObject;
101 typedef struct cx_mutstr_s CxJsonString; 100 typedef struct cx_mutstr_s CxJsonString;
102 typedef struct cx_json_integer_s CxJsonInteger; 101 typedef int64_t CxJsonInteger;
103 typedef struct cx_json_number_s CxJsonNumber; 102 typedef double CxJsonNumber;
104 typedef struct cx_json_literal_s CxJsonLiteral; 103 typedef enum cx_json_literal CxJsonLiteral;
105 104
106 typedef struct cx_json_obj_value_s CxJsonObjValue; 105 typedef struct cx_json_obj_value_s CxJsonObjValue;
107 106
108 struct cx_json_token_s { 107 struct cx_json_token_s {
109 CxJsonTokenType tokentype; 108 CxJsonTokenType tokentype;
159 }; 158 };
160 159
161 struct cx_json_obj_value_s { 160 struct cx_json_obj_value_s {
162 char *name; 161 char *name;
163 CxJsonValue *value; 162 CxJsonValue *value;
164 };
165
166 // TODO: remove single member structs
167
168 struct cx_json_integer_s {
169 int64_t value;
170 };
171
172 struct cx_json_number_s {
173 double value;
174 };
175
176 struct cx_json_literal_s {
177 CxJsonLiteralType literal;
178 }; 163 };
179 164
180 struct cx_json_value_s { 165 struct cx_json_value_s {
181 CxJsonValueType type; 166 CxJsonValueType type;
182 union { 167 union {
198 void cxJsonDestroy(CxJson *json); 183 void cxJsonDestroy(CxJson *json);
199 184
200 __attribute__((__nonnull__)) 185 __attribute__((__nonnull__))
201 void cxJsonFill(CxJson *json, const char *buf, size_t len); 186 void cxJsonFill(CxJson *json, const char *buf, size_t len);
202 187
203 // TODO: discuss if it is intentional that cxJsonNext() will usually parse an entire file in one go
204 __attribute__((__nonnull__)) 188 __attribute__((__nonnull__))
205 int cxJsonNext(CxJson *json, CxJsonValue **value); 189 int cxJsonNext(CxJson *json, CxJsonValue **value);
206 190
207 void cxJsonValueFree(CxJsonValue *value); 191 void cxJsonValueFree(CxJsonValue *value);
208 192
237 return value->type == CX_JSON_LITERAL; 221 return value->type == CX_JSON_LITERAL;
238 } 222 }
239 223
240 __attribute__((__nonnull__)) 224 __attribute__((__nonnull__))
241 static inline bool cxJsonIsBool(CxJsonValue *value) { 225 static inline bool cxJsonIsBool(CxJsonValue *value) {
242 return cxJsonIsLiteral(value) && value->value.literal.literal != CX_JSON_NULL; 226 return cxJsonIsLiteral(value) && value->value.literal != CX_JSON_NULL;
243 } 227 }
244 228
245 __attribute__((__nonnull__)) 229 __attribute__((__nonnull__))
246 static inline bool cxJsonIsTrue(CxJsonValue *value) { 230 static inline bool cxJsonIsTrue(CxJsonValue *value) {
247 return cxJsonIsLiteral(value) && value->value.literal.literal == CX_JSON_TRUE; 231 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_TRUE;
248 } 232 }
249 233
250 __attribute__((__nonnull__)) 234 __attribute__((__nonnull__))
251 static inline bool cxJsonIsFalse(CxJsonValue *value) { 235 static inline bool cxJsonIsFalse(CxJsonValue *value) {
252 return cxJsonIsLiteral(value) && value->value.literal.literal == CX_JSON_FALSE; 236 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_FALSE;
253 } 237 }
254 238
255 __attribute__((__nonnull__)) 239 __attribute__((__nonnull__))
256 static inline bool cxJsonIsNull(CxJsonValue *value) { 240 static inline bool cxJsonIsNull(CxJsonValue *value) {
257 return cxJsonIsLiteral(value) && value->value.literal.literal == CX_JSON_NULL; 241 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_NULL;
258 } 242 }
259 243
260 __attribute__((__nonnull__)) 244 __attribute__((__nonnull__))
261 static inline cxmutstr cxJsonAsString(CxJsonValue *value) { 245 static inline cxmutstr cxJsonAsString(CxJsonValue *value) {
262 // TODO: do we need a separate method to return this directly as cxstring? 246 // TODO: do we need a separate method to return this directly as cxstring?
263 return value->value.string; 247 return value->value.string;
264 } 248 }
265 249
266 __attribute__((__nonnull__)) 250 __attribute__((__nonnull__))
267 static inline double cxJsonAsDouble(CxJsonValue *value) { 251 static inline double cxJsonAsDouble(CxJsonValue *value) {
268 return value->value.number.value; 252 return value->value.number;
269 } 253 }
270 254
271 __attribute__((__nonnull__)) 255 __attribute__((__nonnull__))
272 static inline int64_t cxJsonAsInteger(CxJsonValue *value) { 256 static inline int64_t cxJsonAsInteger(CxJsonValue *value) {
273 return value->value.integer.value; 257 return value->value.integer;
274 } 258 }
275 259
276 __attribute__((__nonnull__)) 260 __attribute__((__nonnull__))
277 static inline bool cxJsonAsBool(CxJsonValue *value) { 261 static inline bool cxJsonAsBool(CxJsonValue *value) {
278 return value->value.literal.literal == CX_JSON_TRUE; 262 return value->value.literal == CX_JSON_TRUE;
279 } 263 }
280 264
281 __attribute__((__nonnull__)) 265 __attribute__((__nonnull__))
282 static inline size_t cxJsonArrSize(CxJsonValue *value) { 266 static inline size_t cxJsonArrSize(CxJsonValue *value) {
283 return value->value.array.size; 267 return value->value.array.size;

mercurial