latest goat3d changes
[deeprace] / libs / goat3d / src / json.h
1 #ifndef JSON_H_
2 #define JSON_H_
3
4 enum {
5         JSON_NULL,
6         JSON_STR,       /* "foo" */
7         JSON_NUM,       /* 3.141 */
8         JSON_BOOL,      /* true */
9         JSON_OBJ,       /* { ... } */
10         JSON_ARR        /* [ ... ] */
11 };
12
13 struct json_value;
14
15 /* objects are collections of items { "foo": 1, "bar": 2 } */
16 struct json_obj {
17         struct json_item *items;
18         int num_items, max_items;
19 };
20
21 /* arrays are collections of values [ "foo", "bar", 3.14, false, "xyzzy" ] */
22 struct json_arr {
23         struct json_value *val;
24         int size, maxsize;
25 };
26
27 struct json_value {
28         int type;
29
30         char *str;                              /* JSON_STR */
31         double num;                             /* JSON_NUM */
32         int boolean;                    /* JSON_BOOL */
33         struct json_obj obj;    /* JSON_OBJ */
34         struct json_arr arr;    /* JSON_ARR */
35 };
36
37 /* items are key-value pairs "foo": "xyzzy" */
38 struct json_item {
39         char *name;
40         struct json_value val;
41
42         struct json_item *next;
43 };
44
45
46 struct json_obj *json_alloc_obj(void);          /* malloc + json_init_obj */
47 void json_free_obj(struct json_obj *obj);       /* json_destroy_obj + free */
48 void json_init_obj(struct json_obj *obj);
49 void json_destroy_obj(struct json_obj *obj);
50
51 struct json_arr *json_alloc_arr(void);          /* malloc + json_init_arr */
52 void json_free_arr(struct json_arr *arr);       /* json_destroy_arr + free */
53 void json_init_arr(struct json_arr *arr);
54 void json_destroy_arr(struct json_arr *arr);
55
56 /* initialize item */
57 int json_item(struct json_item *item, const char *name);
58 void json_destroy_item(struct json_item *item);
59
60 /* pointer values for str, obj, arr can be null */
61 int json_value_str(struct json_value *jv, const char *str);
62 void json_value_num(struct json_value *jv, double num);
63 void json_value_bool(struct json_value *jv, int bval);
64 void json_value_obj(struct json_value *jv, struct json_obj *obj);       /* shallow copy obj */
65 void json_value_arr(struct json_value *jv, struct json_arr *arr);       /* shallow copy arr */
66
67 void json_destroy_value(struct json_value *jv);
68
69 /* item can be null, in which case only space is allocated
70  * if not null, contents are shallow-copied (moved), do not destroy or use
71  */
72 int json_obj_append(struct json_obj *obj, const struct json_item *item);
73
74 /* val can be null in which case only space is allocated
75  * if not null, contents are shallow-copied (moved), do not destroy or use
76  */
77 int json_arr_append(struct json_arr *arr, const struct json_value *val);
78
79 /* find named item in an object (direct descendant name only, not a path) */
80 struct json_item *json_find_item(const struct json_obj *obj, const char *name);
81
82 /* json_lookup returns 0 if the requested value is not found */
83 struct json_value *json_lookup(struct json_obj *root, const char *path);
84
85 /* typed lookup functions return their default argument if the requested value
86  * is not found, or if it has the wrong type.
87  */
88 const char *json_lookup_str(struct json_obj *obj, const char *path, const char *def);
89 double json_lookup_num(struct json_obj *obj, const char *path, double def);
90 int json_lookup_bool(struct json_obj *obj, const char *path, int def);
91 /* these next two are probably not very useful */
92 struct json_obj *json_lookup_obj(struct json_obj *obj, const char *path, struct json_obj *def);
93 struct json_arr *json_lookup_arr(struct json_obj *obj, const char *path, struct json_arr *def);
94
95
96 int json_parse(struct json_obj *root, const char *text);
97
98 /* mostly useful for debugging */
99 void json_print(FILE *fp, struct json_obj *root);
100 void json_print_obj(FILE *fp, struct json_obj *obj, int ind);
101 void json_print_arr(FILE *fp, struct json_arr *arr, int ind);
102 void json_print_item(FILE *fp, struct json_item *item, int ind);
103 void json_print_value(FILE *fp, struct json_value *val, int ind);
104
105 #endif  /* JSON_H_ */