json.h (1635B)
1 2 #ifndef CHIBIPUB_JSON 3 #define CHIBIPUB_JSON 4 5 #include "cursor.h" 6 #include "errors.h" 7 #include <stddef.h> 8 9 struct json_pusher { 10 struct errors errs; 11 struct cursor cur; 12 }; 13 14 struct json_strtok { 15 unsigned char *text; 16 int size; 17 int is_key; 18 int needs_escape; 19 }; 20 21 struct json_handlers { 22 void *data; 23 int (*string)(struct json_handlers *data, struct json_strtok *str); 24 int (*boolean)(struct json_handlers *data, int val); 25 int (*null)(struct json_handlers *data); 26 int (*number)(struct json_handlers *data, unsigned int number); 27 int (*token)(struct json_handlers *data, char type, unsigned int **len); 28 }; 29 30 struct json_parser { 31 struct cursor cur; 32 struct errors errs; 33 struct json_handlers handlers; 34 }; 35 36 int handle_string(struct json_handlers *h, struct json_strtok *str); 37 int handle_bool(struct json_handlers *h, int val); 38 int handle_null(struct json_handlers *h); 39 int handle_number(struct json_handlers *h, unsigned int number); 40 int handle_token(struct json_handlers *h, char type, unsigned int **len); 41 42 void init_json_parser(struct json_parser *p, unsigned char *buf, size_t buf_size, struct json_handlers *h); 43 void init_json_strtok(struct json_strtok *str); 44 void init_json_pusher_with(struct json_pusher *p, struct cursor *out); 45 void init_json_pusher(struct json_pusher *p, unsigned char *buf, size_t size); 46 void init_json_handlers(struct json_handlers *h); 47 void make_compact_handlers(struct json_handlers *h, struct json_pusher *out); 48 int parse_json(struct json_parser *); 49 int push_escaped(struct cursor *cur, unsigned char *text, int size); 50 void copy_json_handlers(struct json_handlers *src, struct json_handlers *dst); 51 52 #endif