-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_json.c
More file actions
136 lines (113 loc) · 2.2 KB
/
test_json.c
File metadata and controls
136 lines (113 loc) · 2.2 KB
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
#include <stdio.h>
#include "json.h"
#include "n_buf.h"
static const char *hello_world(void)
{
return "Hello world!";
}
static int my_rand(void)
{
return 9;
}
static unsigned long addr(void *data)
{
return (unsigned long) data;
}
static void print(const char *s, struct json *j)
{
char data[1];
struct n_buf nb;
n_buf_init(&nb, 4096);
json_format(&nb, j, data);
printf("%s: %s\n", s, nb.nb_buf);
n_buf_destroy(&nb);
}
int main(int argc, char *argv[])
{
int on = 1, off = 0;
int i, v = 703, V = 704;
struct json v0[] = {
J_(string, F, &hello_world),
};
struct json v1[] = {
J_(int, V, 1044),
};
struct json v2[] = {
J_ARRAY,
J_END,
};
struct json v3[] = {
J_OBJECT,
J_(string, V, "key"), J_(string, V, "value"),
J_END,
};
struct json v4[102];
v4[0] = J_ARRAY;
for (i = 1; i < sizeof(v4) / sizeof(v4[0]) - 1; i++)
v4[i] = J_(int, V, i);
v4[i] = J_END;
struct json v5[] = {
J_ARRAY,
J_OBJECT,
J_PAIR("big_float", double, V, 1E43),
J_PAIR("big_long", long, V, -1L << 43),
J_PAIR("my_rand", int, F, &my_rand),
J_PAIR("data_addr", ulong, D, &addr),
J_END,
J_(value, V, v0),
J_ARRAY,
J_ARRAY,
J_(double, V, 0.789),
J_END,
J_END,
J_(bool, V, on),
J_(bool, P, &on),
J_(value, V, v1),
J_(int, V, v),
J_(int, V, V),
J_(int, P, &v),
J_TRUE,
J_FALSE,
J_NULL,
J_END,
};
struct json v6[] = {
J_OBJECT,
J_PAIR("key0", int, V, 0),
J_PAIR("chars", string, V, "\b\f\n\r\t\"\'\\"),
J_END,
};
struct json v7[] = {
J_(raw, V, "{ \"key1\": 1, \"key2\": 2 }"),
};
struct json v8[] = {
J_OBJECT,
J_PAIR("on", bool, P, &on),
J_PAIR("off", bool, P, &off),
J_END,
};
struct json n0, n1, n2, *p0, *p1, *p2;
n0 = J_NULL;
p0 = &n0;
n1 = J_(value, P, &p0);
p1 = &n1;
n2 = J_(value, P, &p1);
p2 = &n2;
struct json v9[] = {
J_(value, P, &p2),
};
#define print(v) print(#v, v)
print(v0);
print(v1);
print(v2);
print(v3);
print(v4);
print(v5);
print(v6);
print(v7);
print(v8);
off = 1, on = 0;
print(v8);
print(v9);
return 0;
}