9 JSON_OBJ, /* { ... } */
10 JSON_ARR /* [ ... ] */
15 /* objects are collections of items { "foo": 1, "bar": 2 } */
17 struct json_item *items;
18 int num_items, max_items;
21 /* arrays are collections of values [ "foo", "bar", 3.14, false, "xyzzy" ] */
23 struct json_value *val;
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 */
37 /* items are key-value pairs "foo": "xyzzy" */
40 struct json_value val;
42 struct json_item *next;
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);
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);
57 int json_item(struct json_item *item, const char *name);
58 void json_destroy_item(struct json_item *item);
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 */
67 void json_destroy_value(struct json_value *jv);
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
72 int json_obj_append(struct json_obj *obj, const struct json_item *item);
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
77 int json_arr_append(struct json_arr *arr, const struct json_value *val);
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);
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);
85 /* typed lookup functions return their default argument if the requested value
86 * is not found, or if it has the wrong type.
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);
96 int json_parse(struct json_obj *root, const char *text);
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);