1 | // Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved. |
2 | // Use of this source code is governed by an MIT license |
3 | // that can be found in the LICENSE file. |
4 | module json |
5 | |
6 | #flag -I @VEXEROOT/thirdparty/cJSON |
7 | #flag @VEXEROOT/thirdparty/cJSON/cJSON.o |
8 | #include "cJSON.h" |
9 | #define js_get(object, key) cJSON_GetObjectItemCaseSensitive((object), (key)) |
10 | |
11 | pub const used = 1 |
12 | |
13 | pub struct C.cJSON { |
14 | valueint int |
15 | valuedouble f64 |
16 | valuestring &char |
17 | } |
18 | |
19 | fn C.cJSON_IsTrue(&C.cJSON) bool |
20 | |
21 | fn C.cJSON_CreateNumber(f64) &C.cJSON |
22 | |
23 | fn C.cJSON_CreateBool(bool) &C.cJSON |
24 | |
25 | fn C.cJSON_CreateString(&char) &C.cJSON |
26 | |
27 | fn C.cJSON_Parse(&char) &C.cJSON |
28 | |
29 | fn C.cJSON_PrintUnformatted(&C.cJSON) &char |
30 | |
31 | fn C.cJSON_Print(&C.cJSON) &char |
32 | |
33 | fn C.cJSON_free(voidptr) |
34 | |
35 | // decode tries to decode the provided JSON string, into a V structure. |
36 | // If it can not do that, it returns an error describing the reason for |
37 | // the parsing failure. |
38 | pub fn decode(typ voidptr, s string) !voidptr { |
39 | // compiler implementation |
40 | return 0 |
41 | } |
42 | |
43 | // encode serialises the provided V value as a JSON string, optimised for shortness. |
44 | pub fn encode(x voidptr) string { |
45 | // compiler implementation |
46 | return '' |
47 | } |
48 | |
49 | // encode_pretty serialises the provided V value as a JSON string, in a formatted way, optimised for viewing by humans. |
50 | pub fn encode_pretty(x voidptr) string { |
51 | // compiler implementation |
52 | return '' |
53 | } |
54 | |
55 | [markused] |
56 | fn decode_int(root &C.cJSON) int { |
57 | if isnil(root) { |
58 | return 0 |
59 | } |
60 | return root.valueint |
61 | } |
62 | |
63 | [markused] |
64 | fn decode_i8(root &C.cJSON) i8 { |
65 | if isnil(root) { |
66 | return i8(0) |
67 | } |
68 | return i8(root.valueint) |
69 | } |
70 | |
71 | [markused] |
72 | fn decode_i16(root &C.cJSON) i16 { |
73 | if isnil(root) { |
74 | return i16(0) |
75 | } |
76 | return i16(root.valueint) |
77 | } |
78 | |
79 | [markused] |
80 | fn decode_i64(root &C.cJSON) i64 { |
81 | if isnil(root) { |
82 | return i64(0) |
83 | } |
84 | return i64(root.valuedouble) // i64 is double in C |
85 | } |
86 | |
87 | // TODO: remove when `byte` is removed |
88 | [markused] |
89 | fn decode_byte(root &C.cJSON) byte { |
90 | return byte(decode_u8(root)) |
91 | } |
92 | |
93 | [markused] |
94 | fn decode_u8(root &C.cJSON) u8 { |
95 | if isnil(root) { |
96 | return u8(0) |
97 | } |
98 | return u8(root.valueint) |
99 | } |
100 | |
101 | [markused] |
102 | fn decode_u16(root &C.cJSON) u16 { |
103 | if isnil(root) { |
104 | return u16(0) |
105 | } |
106 | return u16(root.valueint) |
107 | } |
108 | |
109 | [markused] |
110 | fn decode_u32(root &C.cJSON) u32 { |
111 | if isnil(root) { |
112 | return u32(0) |
113 | } |
114 | return u32(root.valueint) |
115 | } |
116 | |
117 | [markused] |
118 | fn decode_u64(root &C.cJSON) u64 { |
119 | if isnil(root) { |
120 | return u64(0) |
121 | } |
122 | return u64(root.valuedouble) |
123 | } |
124 | |
125 | [markused] |
126 | fn decode_f32(root &C.cJSON) f32 { |
127 | if isnil(root) { |
128 | return f32(0) |
129 | } |
130 | return f32(root.valuedouble) |
131 | } |
132 | |
133 | [markused] |
134 | fn decode_f64(root &C.cJSON) f64 { |
135 | if isnil(root) { |
136 | return f64(0) |
137 | } |
138 | return root.valuedouble |
139 | } |
140 | |
141 | [markused] |
142 | fn decode_rune(root &C.cJSON) rune { |
143 | if isnil(root) { |
144 | return rune(0) |
145 | } |
146 | if isnil(root.valuestring) { |
147 | return rune(0) |
148 | } |
149 | |
150 | // TODO: Parse as runes, bypassing string casting...? |
151 | return unsafe { tos_clone(&u8(root.valuestring)).runes().first() } |
152 | } |
153 | |
154 | [markused] |
155 | fn decode_string(root &C.cJSON) string { |
156 | if isnil(root) { |
157 | return '' |
158 | } |
159 | if isnil(root.valuestring) { |
160 | return '' |
161 | } |
162 | return unsafe { tos_clone(&u8(root.valuestring)) } // , _strlen(root.valuestring)) |
163 | } |
164 | |
165 | [markused] |
166 | fn decode_bool(root &C.cJSON) bool { |
167 | if isnil(root) { |
168 | return false |
169 | } |
170 | return C.cJSON_IsTrue(root) |
171 | } |
172 | |
173 | // /////////////////// |
174 | |
175 | [markused] |
176 | fn encode_int(val int) &C.cJSON { |
177 | return C.cJSON_CreateNumber(val) |
178 | } |
179 | |
180 | [markused] |
181 | fn encode_i8(val i8) &C.cJSON { |
182 | return C.cJSON_CreateNumber(val) |
183 | } |
184 | |
185 | [markused] |
186 | fn encode_i16(val i16) &C.cJSON { |
187 | return C.cJSON_CreateNumber(val) |
188 | } |
189 | |
190 | [markused] |
191 | fn encode_i64(val i64) &C.cJSON { |
192 | return C.cJSON_CreateNumber(val) |
193 | } |
194 | |
195 | // TODO: remove when `byte` is removed |
196 | [markused] |
197 | fn encode_byte(root byte) &C.cJSON { |
198 | return encode_u8(u8(root)) |
199 | } |
200 | |
201 | [markused] |
202 | fn encode_u8(val u8) &C.cJSON { |
203 | return C.cJSON_CreateNumber(val) |
204 | } |
205 | |
206 | [markused] |
207 | fn encode_u16(val u16) &C.cJSON { |
208 | return C.cJSON_CreateNumber(val) |
209 | } |
210 | |
211 | [markused] |
212 | fn encode_u32(val u32) &C.cJSON { |
213 | return C.cJSON_CreateNumber(val) |
214 | } |
215 | |
216 | [markused] |
217 | fn encode_u64(val u64) &C.cJSON { |
218 | return C.cJSON_CreateNumber(val) |
219 | } |
220 | |
221 | [markused] |
222 | fn encode_f32(val f32) &C.cJSON { |
223 | return C.cJSON_CreateNumber(val) |
224 | } |
225 | |
226 | [markused] |
227 | fn encode_f64(val f64) &C.cJSON { |
228 | return C.cJSON_CreateNumber(val) |
229 | } |
230 | |
231 | [markused] |
232 | fn encode_bool(val bool) &C.cJSON { |
233 | return C.cJSON_CreateBool(val) |
234 | } |
235 | |
236 | [markused] |
237 | fn encode_rune(val rune) &C.cJSON { |
238 | return C.cJSON_CreateString(&char(val.str().str)) |
239 | } |
240 | |
241 | [markused] |
242 | fn encode_string(val string) &C.cJSON { |
243 | return C.cJSON_CreateString(&char(val.str)) |
244 | } |
245 | |
246 | // /////////////////////// |
247 | // user := decode_User(json_parse(js_string_var)) |
248 | [markused] |
249 | fn json_parse(s string) &C.cJSON { |
250 | return C.cJSON_Parse(&char(s.str)) |
251 | } |
252 | |
253 | // json_string := json_print(encode_User(user)) |
254 | [markused] |
255 | fn json_print(data &C.cJSON) string { |
256 | s := C.cJSON_PrintUnformatted(data) |
257 | r := unsafe { tos_clone(&u8(s)) } |
258 | C.cJSON_free(s) |
259 | return r |
260 | } |
261 | |
262 | fn json_print_pretty(data &C.cJSON) string { |
263 | s := C.cJSON_Print(data) |
264 | r := unsafe { tos_clone(&u8(s)) } |
265 | C.cJSON_free(s) |
266 | return r |
267 | } |
268 | |
269 | // / cjson wrappers |
270 | // fn json_array_for_each(val, root &C.cJSON) { |
271 | // #cJSON_ArrayForEach (val ,root) |
272 | // } |