diff options
Diffstat (limited to 'src/json.h')
-rw-r--r-- | src/json.h | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/json.h b/src/json.h new file mode 100644 index 0000000..8f98368 --- /dev/null +++ b/src/json.h @@ -0,0 +1,86 @@ +#include <stdlib.h> + +enum json_value_type { + json_null, + json_bool, + json_number, + json_string, + json_array, + json_object +}; + +struct json_value; + +struct json_array { + size_t oc; + struct json_value **ov; + size_t on; +}; + +struct json_object { + struct json_pair *head; + struct json_pair *tail; + size_t count; +}; + +struct json_value { + enum json_value_type type; + union { + int b; /* json_bool */ + double n; /* json_number */ + char *s; /* json_string */ + struct json_array *a; /* json_array */ + struct json_object *o; /* json_object */ + } v; +}; + +struct json_pair { + struct json_pair *next; + char *k; + struct json_value *v; +}; + +struct json_format +{ + size_t indent; + int precision; + void (*write) (void *, char const *, size_t); + void *data; +}; + +int json_value_format(struct json_value *obj, struct json_format *fmt, + size_t level); +void json_value_free(struct json_value *); + +struct json_value *json_new_null(void); +struct json_value *json_new_bool(int b); +struct json_value *json_new_number(double n); +struct json_value *json_new_string(char const *str); + +struct json_value *json_new_object(void); +int json_object_set(struct json_value *obj, char const *name, + struct json_value *val); +int json_object_get(struct json_value *obj, char const *name, + struct json_value **retval); + +struct json_value *json_new_array(void); +static inline size_t json_array_length(struct json_value *j) { + return j->v.a->oc; +} +int json_array_insert(struct json_value *j, size_t idx, struct json_value *v); +int json_array_append(struct json_value *j, struct json_value *v); +int json_array_set(struct json_value *j, size_t idx, struct json_value *v); +int json_array_get(struct json_value *j, size_t idx, + struct json_value **retval); + + +enum { + JSON_E_NOERR = 0, + JSON_E_NOMEM = -1, + JSON_E_BADTOK = -2, + JSON_E_BADDELIM = -3, + JSON_E_BADSTRING = -4 +}; + +int json_parse_string(char const *input, struct json_value **retval, + char **endp); |