summaryrefslogtreecommitdiff
path: root/json.h
blob: 09c2f18715c61911dfda6dacc860ef44475a9b3b (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef __JSON__H
#define __JSON__H

#include "lib/output_buffer.h"

#define JSON_TYPE_STRING 0
#define JSON_TYPE_INTEGER 1
#define JSON_TYPE_FLOAT 2
#define JSON_TYPE_OBJECT 3
#define JSON_TYPE_ARRAY 4
#define JSON_PARENT_TYPE_PAIR 0
#define JSON_PARENT_TYPE_ARRAY 1
struct json_value {
	int type;
	union {
		long long integer_number;
		double float_number;
		char *string;
		struct json_object *object;
		struct json_array *array;
	};
	int parent_type;
	union {
		struct json_pair *parent_pair;
		struct json_array *parent_array;
	};
};

struct json_array {
	struct json_value **values;
	int value_cnt;
	struct json_value *parent;
};

struct json_object {
	struct json_pair **pairs;
	int pair_cnt;
	struct json_value *parent;
};

struct json_pair {
	char *name;
	struct json_value *value;
	struct json_object *parent;
};

struct json_object *json_create_object(void);
struct json_array *json_create_array(void);

void json_free_object(struct json_object *obj);

int json_object_add_value_type(struct json_object *obj, const char *name,
			       const struct json_value *val);

static inline int json_object_add_value_int(struct json_object *obj,
					    const char *name, long long val)
{
	struct json_value arg = {
		.type = JSON_TYPE_INTEGER,
		.integer_number = val,
	};

	return json_object_add_value_type(obj, name, &arg);
}

static inline int json_object_add_value_float(struct json_object *obj,
					      const char *name, double val)
{
	struct json_value arg = {
		.type = JSON_TYPE_FLOAT,
		.float_number = val,
	};

	return json_object_add_value_type(obj, name, &arg);
}

static inline int json_object_add_value_string(struct json_object *obj,
					       const char *name,
					       const char *val)
{
	struct json_value arg = {
		.type = JSON_TYPE_STRING,
		.string = (char *)val,
	};

	return json_object_add_value_type(obj, name, &arg);
}

static inline int json_object_add_value_object(struct json_object *obj,
					       const char *name,
					       struct json_object *val)
{
	struct json_value arg = {
		.type = JSON_TYPE_OBJECT,
		.object = val,
	};

	return json_object_add_value_type(obj, name, &arg);
}

static inline int json_object_add_value_array(struct json_object *obj,
					      const char *name,
					      struct json_array *val)
{
	struct json_value arg = {
		.type = JSON_TYPE_ARRAY,
		.array = val,
	};

	return json_object_add_value_type(obj, name, &arg);
}

int json_array_add_value_type(struct json_array *array,
			      const struct json_value *val);

static inline int json_array_add_value_int(struct json_array *obj,
					   long long val)
{
	struct json_value arg = {
		.type = JSON_TYPE_INTEGER,
		.integer_number = val,
	};

	return json_array_add_value_type(obj, &arg);
}

static inline int json_array_add_value_float(struct json_array *obj,
					     double val)
{
	struct json_value arg = {
		.type = JSON_TYPE_FLOAT,
		.float_number = val,
	};

	return json_array_add_value_type(obj, &arg);
}

static inline int json_array_add_value_string(struct json_array *obj,
					      const char *val)
{
	struct json_value arg = {
		.type = JSON_TYPE_STRING,
		.string = (char *)val,
	};

	return json_array_add_value_type(obj, &arg);
}

static inline int json_array_add_value_object(struct json_array *obj,
					      struct json_object *val)
{
	struct json_value arg = {
		.type = JSON_TYPE_OBJECT,
		.object = val,
	};

	return json_array_add_value_type(obj, &arg);
}

static inline int json_array_add_value_array(struct json_array *obj,
					     struct json_array *val)
{
	struct json_value arg = {
		.type = JSON_TYPE_ARRAY,
		.array = val,
	};

	return json_array_add_value_type(obj, &arg);
}

#define json_array_last_value_object(obj) \
	(obj->values[obj->value_cnt - 1]->object)

void json_print_object(struct json_object *obj, struct buf_output *out);
#endif