aboutsummaryrefslogtreecommitdiff
path: root/src/json.h
blob: 8f9836815b894852ba3c01bede5679b61f296f70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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);

Return to:

Send suggestions and report system problems to the System administrator.