-
Notifications
You must be signed in to change notification settings - Fork 0
/
static-json-builder.h
372 lines (305 loc) · 12.5 KB
/
static-json-builder.h
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#ifndef STATIC_JSON_BUILDER_H
#define STATIC_JSON_BUILDER_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct json_prop_t;
struct json_object_t;
struct json_array_t;
struct json_value_t;
typedef struct json_prop_t json_prop_t;
typedef struct json_object_t json_object_t;
typedef struct json_array_t json_array_t;
typedef struct json_value_t json_value_t;
typedef json_value_t* Json;
typedef enum json_value_type_t
{
JSON_VALUE_TYPE_NULL = 0,
JSON_VALUE_TYPE_BOOL,
JSON_VALUE_TYPE_INT,
JSON_VALUE_TYPE_FLOAT,
JSON_VALUE_TYPE_STRING,
JSON_VALUE_TYPE_ARRAY,
JSON_VALUE_TYPE_OBJECT,
JSON_VALUE_TYPE_MAX,
} json_value_type_t;
struct json_object_t
{
size_t size;
json_prop_t **props;
};
struct json_array_t
{
size_t size;
json_value_t **entries;
};
struct json_value_t
{
json_value_type_t type;
union
{
bool boolean;
int64_t integer;
double floating;
const char *string;
json_object_t *object;
json_array_t *array;
} as;
};
struct json_prop_t
{
const char *key;
json_value_t *entry;
};
#define JsonNull() ( \
&(json_value_t) { \
.type = JSON_VALUE_TYPE_NULL, \
} \
)
#define JsonBool(b) ( \
&(json_value_t) { \
.type = JSON_VALUE_TYPE_BOOL, \
.as.boolean = (b), \
} \
)
#define JsonInt(i) ( \
&(json_value_t) { \
.type = JSON_VALUE_TYPE_INT, \
.as.integer = (i), \
} \
)
#define JsonFloat(f) ( \
&(json_value_t) { \
.type = JSON_VALUE_TYPE_FLOAT, \
.as.floating = (f), \
} \
)
#define JsonString(s) ( \
&(json_value_t) { \
.type = JSON_VALUE_TYPE_STRING, \
.as.string = (const char *) (s), \
} \
)
#define JsonProp(k,e) ( \
&(json_prop_t) { \
.key = (const char *) (k), \
.entry = (e), \
} \
)
/*
Use __VA_ARGS__ length hack to avoid pedantic error
- warning: ISO C forbids empty initializer braces
*/
#define TYPED_VA_ARGS_LENGTH(T, ...) ( \
sizeof((T[]){ 0, __VA_ARGS__ }) / sizeof(T) - 1 \
)
#define JsonArray(...) ( \
&(json_value_t) { \
.type = JSON_VALUE_TYPE_ARRAY, \
.as.array = &(json_array_t) { \
.size = TYPED_VA_ARGS_LENGTH(json_value_t *, __VA_ARGS__), \
.entries = TYPED_VA_ARGS_LENGTH(json_value_t *, __VA_ARGS__) \
? (json_value_t **) (json_value_t *[]) { NULL, __VA_ARGS__ } + 1 \
: (NULL), \
} \
} \
)
#define JsonObject(...) ( \
&(json_value_t) { \
.type = JSON_VALUE_TYPE_OBJECT, \
.as.object = &(json_object_t) { \
.size = TYPED_VA_ARGS_LENGTH(json_prop_t *, __VA_ARGS__), \
.props = TYPED_VA_ARGS_LENGTH(json_prop_t *, __VA_ARGS__) \
? (json_prop_t **) (json_prop_t *[]) { NULL, __VA_ARGS__ } + 1 \
: (NULL), \
} \
} \
)
/**
* Serializes target json into a string.
*
* @param json The target json to be converted into a string
* @return String representation of the target json or NULL on string allocation error
* @note You need to release the string allocated by this method
*/
static inline char *json_stringify(json_value_t *json);
/**
* Serializes target json into a string and puts the result into a buffer.
*
* @param json The target json to be converted into a string
* @param buffer Buffer where you want to put the string json representation
* @note You can find out how big the allocated buffer should be with `json_stingified_size(...)` method
*/
static inline void json_stringify_into_buffer(json_value_t *json, char *buffer);
/**
* Computes the size of the string representation of the json.
*
* @param json The target json for which you want to compute the size of the string representation
* @return the size of the string representation of the target json
*/
static inline size_t json_stingified_size(json_value_t *json);
typedef void (*json_size_compute_func_t) (json_value_t *json, size_t *size);
typedef void (*json_write_func_t) (json_value_t *json, char **cursor);
static inline void json_size_compute_func_for_null (json_value_t *json, size_t *size);
static inline void json_size_compute_func_for_bool (json_value_t *json, size_t *size);
static inline void json_size_compute_func_for_int (json_value_t *json, size_t *size);
static inline void json_size_compute_func_for_floating (json_value_t *json, size_t *size);
static inline void json_size_compute_func_for_string (json_value_t *json, size_t *size);
static inline void json_size_compute_func_for_array (json_value_t *json, size_t *size);
static inline void json_size_compute_func_for_object (json_value_t *json, size_t *size);
static const json_size_compute_func_t json_size_compute_func_by_type[JSON_VALUE_TYPE_MAX] = {
[JSON_VALUE_TYPE_NULL] = json_size_compute_func_for_null,
[JSON_VALUE_TYPE_BOOL] = json_size_compute_func_for_bool,
[JSON_VALUE_TYPE_INT] = json_size_compute_func_for_int,
[JSON_VALUE_TYPE_FLOAT] = json_size_compute_func_for_floating,
[JSON_VALUE_TYPE_STRING] = json_size_compute_func_for_string,
[JSON_VALUE_TYPE_ARRAY] = json_size_compute_func_for_array,
[JSON_VALUE_TYPE_OBJECT] = json_size_compute_func_for_object,
};
static inline void json_write_func_for_null (json_value_t *json, char **cursor);
static inline void json_write_func_for_bool (json_value_t *json, char **cursor);
static inline void json_write_func_for_int (json_value_t *json, char **cursor);
static inline void json_write_func_for_floating (json_value_t *json, char **cursor);
static inline void json_write_func_for_string (json_value_t *json, char **cursor);
static inline void json_write_func_for_array (json_value_t *json, char **cursor);
static inline void json_write_func_for_object (json_value_t *json, char **cursor);
static const json_write_func_t json_write_func_by_type[JSON_VALUE_TYPE_MAX] = {
[JSON_VALUE_TYPE_NULL] = json_write_func_for_null,
[JSON_VALUE_TYPE_BOOL] = json_write_func_for_bool,
[JSON_VALUE_TYPE_INT] = json_write_func_for_int,
[JSON_VALUE_TYPE_FLOAT] = json_write_func_for_floating,
[JSON_VALUE_TYPE_STRING] = json_write_func_for_string,
[JSON_VALUE_TYPE_ARRAY] = json_write_func_for_array,
[JSON_VALUE_TYPE_OBJECT] = json_write_func_for_object,
};
static inline size_t json_size_compute(json_value_t *json)
{
size_t size = 0;
json_size_compute_func_by_type[json->type](json, &size);
return size;
}
static inline size_t json_write(json_value_t *json, char *buffer)
{
char *cursor = buffer;
json_write_func_by_type[json->type](json, &cursor);
return (size_t) (cursor - buffer);
}
static inline void json_size_compute_func_for_null(json_value_t *json, size_t *size)
{
(void) json;
*size += strlen("null");
}
static inline void json_size_compute_func_for_bool(json_value_t *json, size_t *size)
{
*size += json->as.boolean ? strlen("true") : strlen("false");
}
static inline void json_size_compute_func_for_int(json_value_t *json, size_t *size)
{
*size += snprintf(NULL, 0, "%" PRId64, json->as.integer);
}
static inline void json_size_compute_func_for_floating(json_value_t *json, size_t *size)
{
*size += snprintf(NULL, 0, "%f", json->as.floating);
}
static inline void json_size_compute_func_for_string(json_value_t *json, size_t *size)
{
*size += strlen("\"") + strlen(json->as.string) + strlen("\"");
}
static inline void json_size_compute_func_for_array(json_value_t *json, size_t *size)
{
*size += strlen("[");
for (size_t i = 0; i < json->as.array->size; i++) {
if (i != 0) {
*size += strlen(",");
}
*size += json_size_compute(json->as.array->entries[i]);
}
*size += strlen("]");
}
static inline void json_size_compute_func_for_object(json_value_t *json, size_t *size)
{
*size += strlen("{");
for (size_t i = 0; i < json->as.object->size; i++) {
json_prop_t *property = json->as.object->props[i];
if (i != 0) {
*size += strlen(",");
}
*size += strlen("\"") + strlen(property->key) + strlen("\":");
*size += json_size_compute(property->entry);
}
*size += strlen("}");
}
static inline void json_write_func_for_null(json_value_t *json, char **cursor)
{
*cursor += snprintf(*cursor, json_size_compute(json) + 1, "%s", "null");
}
static inline void json_write_func_for_bool(json_value_t *json, char **cursor)
{
*cursor += snprintf(*cursor, json_size_compute(json) + 1, "%s", json->as.boolean ? "true" : "false");
}
static inline void json_write_func_for_int(json_value_t *json, char **cursor)
{
*cursor += snprintf(*cursor, json_size_compute(json) + 1, "%" PRId64, json->as.integer);
}
static inline void json_write_func_for_floating(json_value_t *json, char **cursor)
{
*cursor += snprintf(*cursor, json_size_compute(json) + 1, "%f", json->as.floating);
}
static inline void json_write_func_for_string(json_value_t *json, char **cursor)
{
*cursor += snprintf(*cursor, json_size_compute(json) + 1, "\"%s\"", json->as.string);
}
static inline void json_write_func_for_array(json_value_t *json, char **cursor)
{
*cursor += snprintf(*cursor, 2, "%c", '[');
for (size_t i = 0; i < json->as.array->size; i++) {
json_value_t *entry = json->as.array->entries[i];
if (i != 0) {
*cursor += snprintf(*cursor, 2, "%c", ',');
}
*cursor += json_write(entry, *cursor);
}
*cursor += snprintf(*cursor, 2, "%c", ']');
}
static inline void json_write_func_for_object(json_value_t *json, char **cursor)
{
*cursor += snprintf(*cursor, 2, "%c", '{');
for (size_t i = 0; i < json->as.object->size; i++) {
json_prop_t *property = json->as.object->props[i];
if (i != 0) {
*cursor += snprintf(*cursor, 2, "%c", ',');
}
*cursor += snprintf(*cursor, strlen(property->key) + 4, "\"%s\":", property->key);
*cursor += json_write(property->entry, *cursor);
}
*cursor += snprintf(*cursor, 2, "%c", '}');
}
static inline char *json_stringify(json_value_t *json)
{
assert(json && "attempt to stringify json but json is a null pointer");
size_t length = json_size_compute(json);
char *buffer = calloc(length + 1, sizeof(char));
if (buffer == NULL) {
return NULL;
}
size_t written = json_write(json, buffer);
assert(length == written && "not all data was written to the buffer");
return buffer;
}
static inline void json_stringify_into_buffer(json_value_t *json, char *buffer)
{
assert(json && "attempt to write json into buffer but json is a null pointer");
assert(buffer && "attempt to write json into buffer but buffer is a null pointer");
json_write(json, buffer);
}
static inline size_t json_stingified_size(json_value_t *json)
{
assert(json && "attempt to get the json string size but json is a null pointer");
return json_size_compute(json) + 1;
}
#endif /* STATIC_JSON_BUILDER_H */