Shaohua Li | cc372b1 | 2012-09-17 09:12:18 +0200 | [diff] [blame] | 1 | #ifndef __JSON__H |
| 2 | #define __JSON__H |
| 3 | struct json_object; |
| 4 | struct json_array; |
| 5 | struct json_pair; |
| 6 | |
| 7 | #define JSON_TYPE_STRING 0 |
| 8 | #define JSON_TYPE_INTEGER 1 |
| 9 | #define JSON_TYPE_FLOAT 2 |
| 10 | #define JSON_TYPE_OBJECT 3 |
| 11 | #define JSON_TYPE_ARRAY 4 |
| 12 | #define JSON_PARENT_TYPE_PAIR 0 |
| 13 | #define JSON_PARENT_TYPE_ARRAY 1 |
| 14 | struct json_value { |
| 15 | int type; |
| 16 | union { |
| 17 | long integer_number; |
| 18 | double float_number; |
| 19 | char *string; |
| 20 | struct json_object *object; |
| 21 | struct json_array *array; |
| 22 | }; |
| 23 | int parent_type; |
| 24 | union { |
| 25 | struct json_pair *parent_pair; |
| 26 | struct json_array *parent_array; |
| 27 | }; |
| 28 | }; |
| 29 | |
| 30 | struct json_array { |
| 31 | struct json_value **values; |
| 32 | int value_cnt; |
| 33 | struct json_value *parent; |
| 34 | }; |
| 35 | |
| 36 | struct json_object { |
| 37 | struct json_pair **pairs; |
| 38 | int pair_cnt; |
| 39 | struct json_value *parent; |
| 40 | }; |
| 41 | |
| 42 | struct json_pair { |
| 43 | char *name; |
| 44 | struct json_value *value; |
| 45 | struct json_object *parent; |
| 46 | }; |
| 47 | |
| 48 | struct json_object *json_create_object(void); |
| 49 | struct json_array *json_create_array(void); |
| 50 | |
| 51 | void json_free_object(struct json_object *obj); |
| 52 | |
| 53 | int json_object_add_value_type(struct json_object *obj, const char *name, int type, ...); |
| 54 | #define json_object_add_value_int(obj, name, val) \ |
| 55 | json_object_add_value_type((obj), name, JSON_TYPE_INTEGER, (val)) |
| 56 | #define json_object_add_value_float(obj, name, val) \ |
| 57 | json_object_add_value_type((obj), name, JSON_TYPE_FLOAT, (val)) |
| 58 | #define json_object_add_value_string(obj, name, val) \ |
| 59 | json_object_add_value_type((obj), name, JSON_TYPE_STRING, (val)) |
| 60 | #define json_object_add_value_object(obj, name, val) \ |
| 61 | json_object_add_value_type((obj), name, JSON_TYPE_OBJECT, (val)) |
| 62 | #define json_object_add_value_array(obj, name, val) \ |
| 63 | json_object_add_value_type((obj), name, JSON_TYPE_ARRAY, (val)) |
| 64 | int json_array_add_value_type(struct json_array *array, int type, ...); |
| 65 | #define json_array_add_value_int(obj, val) \ |
| 66 | json_array_add_value_type((obj), JSON_TYPE_INTEGER, (val)) |
| 67 | #define json_array_add_value_float(obj, val) \ |
| 68 | json_array_add_value_type((obj), JSON_TYPE_FLOAT, (val)) |
| 69 | #define json_array_add_value_string(obj, val) \ |
| 70 | json_array_add_value_type((obj), JSON_TYPE_STRING, (val)) |
| 71 | #define json_array_add_value_object(obj, val) \ |
| 72 | json_array_add_value_type((obj), JSON_TYPE_OBJECT, (val)) |
| 73 | #define json_array_add_value_array(obj, val) \ |
| 74 | json_array_add_value_type((obj), JSON_TYPE_ARRAY, (val)) |
| 75 | |
| 76 | void json_print_object(struct json_object *obj); |
| 77 | #endif |