… | |
… | |
10 | #define F_UTF8 0x00000002 |
10 | #define F_UTF8 0x00000002 |
11 | #define F_INDENT 0x00000004 |
11 | #define F_INDENT 0x00000004 |
12 | #define F_CANONICAL 0x00000008 |
12 | #define F_CANONICAL 0x00000008 |
13 | #define F_SPACE_BEFORE 0x00000010 |
13 | #define F_SPACE_BEFORE 0x00000010 |
14 | #define F_SPACE_AFTER 0x00000020 |
14 | #define F_SPACE_AFTER 0x00000020 |
15 | #define F_JSON_RPC 0x00000040 |
|
|
16 | #define F_ALLOW_NONREF 0x00000080 |
15 | #define F_ALLOW_NONREF 0x00000080 |
17 | #define F_SHRINK 0x00000100 |
16 | #define F_SHRINK 0x00000100 |
18 | |
17 | |
|
|
18 | // F_SKIPINVALID? |
|
|
19 | // F_EXECCODEREF? |
|
|
20 | // F_SELFCONVERT? <=> { &__class__ => } |
|
|
21 | |
19 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
22 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
20 | #define F_DEFAULT 0 |
23 | #define F_DEFAULT 0 |
21 | |
24 | |
22 | #define INIT_SIZE 32 // initial scalar size to be allocated |
25 | #define INIT_SIZE 32 // initial scalar size to be allocated |
|
|
26 | #define INDENT_STEP 3 // spaces per indentation level |
|
|
27 | |
|
|
28 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
29 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
23 | |
30 | |
24 | #define SB do { |
31 | #define SB do { |
25 | #define SE } while (0) |
32 | #define SE } while (0) |
26 | |
33 | |
27 | static HV *json_stash; |
34 | static HV *json_stash; // JSON::XS:: |
|
|
35 | |
|
|
36 | ///////////////////////////////////////////////////////////////////////////// |
|
|
37 | // utility functions |
|
|
38 | |
|
|
39 | static UV * |
|
|
40 | SvJSON (SV *sv) |
|
|
41 | { |
|
|
42 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
43 | croak ("object is not of type JSON::XS"); |
|
|
44 | |
|
|
45 | return &SvUVX (SvRV (sv)); |
|
|
46 | } |
|
|
47 | |
|
|
48 | static void |
|
|
49 | shrink (SV *sv) |
|
|
50 | { |
|
|
51 | sv_utf8_downgrade (sv, 1); |
|
|
52 | if (SvLEN (sv) > SvCUR (sv) + 1) |
|
|
53 | { |
|
|
54 | #ifdef SvPV_shrink_to_cur |
|
|
55 | SvPV_shrink_to_cur (sv); |
|
|
56 | #elif defined (SvPV_renew) |
|
|
57 | SvPV_renew (sv, SvCUR (sv) + 1); |
|
|
58 | #endif |
|
|
59 | } |
|
|
60 | } |
|
|
61 | |
|
|
62 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
63 | // case of an error. |
|
|
64 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
65 | // but use the very good perl function to parse anything else. |
|
|
66 | // note that we never call this function for a ascii codepoints |
|
|
67 | static UV |
|
|
68 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
69 | { |
|
|
70 | if (s[0] > 0xdf || s[0] < 0xc2) |
|
|
71 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
72 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
73 | { |
|
|
74 | *clen = 2; |
|
|
75 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
76 | } |
|
|
77 | else |
|
|
78 | return (UV)-1; |
|
|
79 | } |
|
|
80 | |
|
|
81 | ///////////////////////////////////////////////////////////////////////////// |
|
|
82 | // encoder |
28 | |
83 | |
29 | // structure used for encoding JSON |
84 | // structure used for encoding JSON |
30 | typedef struct |
85 | typedef struct |
31 | { |
86 | { |
32 | char *cur; |
87 | char *cur; // SvPVX (sv) + current output position |
33 | STRLEN len; // SvLEN (sv) |
|
|
34 | char *end; // SvEND (sv) |
88 | char *end; // SvEND (sv) |
35 | SV *sv; |
89 | SV *sv; // result scalar |
36 | UV flags; |
90 | UV flags; // F_* |
37 | int max_recurse; |
91 | int indent; // indentation level |
38 | int indent; |
92 | int max_depth; // max. recursion level |
39 | } enc_t; |
93 | } enc_t; |
40 | |
|
|
41 | // structure used for decoding JSON |
|
|
42 | typedef struct |
|
|
43 | { |
|
|
44 | char *cur; |
|
|
45 | char *end; |
|
|
46 | const char *err; |
|
|
47 | UV flags; |
|
|
48 | } dec_t; |
|
|
49 | |
|
|
50 | static UV * |
|
|
51 | SvJSON (SV *sv) |
|
|
52 | { |
|
|
53 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
54 | croak ("object is not of type JSON::XS"); |
|
|
55 | |
|
|
56 | return &SvUVX (SvRV (sv)); |
|
|
57 | } |
|
|
58 | |
|
|
59 | static void |
|
|
60 | shrink (SV *sv) |
|
|
61 | { |
|
|
62 | sv_utf8_downgrade (sv, 1); |
|
|
63 | #ifdef SvPV_shrink_to_cur |
|
|
64 | SvPV_shrink_to_cur (sv); |
|
|
65 | #endif |
|
|
66 | } |
|
|
67 | |
|
|
68 | ///////////////////////////////////////////////////////////////////////////// |
|
|
69 | |
94 | |
70 | static void |
95 | static void |
71 | need (enc_t *enc, STRLEN len) |
96 | need (enc_t *enc, STRLEN len) |
72 | { |
97 | { |
73 | if (enc->cur + len >= enc->end) |
98 | if (enc->cur + len >= enc->end) |
… | |
… | |
131 | STRLEN clen; |
156 | STRLEN clen; |
132 | UV uch; |
157 | UV uch; |
133 | |
158 | |
134 | if (is_utf8) |
159 | if (is_utf8) |
135 | { |
160 | { |
136 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
161 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
162 | uch = decode_utf8 (str, end - str, &clen); |
137 | if (clen == (STRLEN)-1) |
163 | if (clen == (STRLEN)-1) |
138 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
164 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
139 | } |
165 | } |
140 | else |
166 | else |
141 | { |
167 | { |
142 | uch = ch; |
168 | uch = ch; |
143 | clen = 1; |
169 | clen = 1; |
144 | } |
170 | } |
145 | |
171 | |
|
|
172 | if (uch > 0x10FFFFUL) |
|
|
173 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
174 | |
146 | if (uch < 0x80 || enc->flags & F_ASCII) |
175 | if (uch < 0x80 || enc->flags & F_ASCII) |
147 | { |
176 | { |
148 | if (uch > 0xFFFFUL) |
177 | if (uch > 0xFFFFUL) |
149 | { |
178 | { |
150 | need (enc, len += 11); |
179 | need (enc, len += 11); |
151 | sprintf (enc->cur, "\\u%04x\\u%04x", |
180 | sprintf (enc->cur, "\\u%04x\\u%04x", |
152 | (uch - 0x10000) / 0x400 + 0xD800, |
181 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
153 | (uch - 0x10000) % 0x400 + 0xDC00); |
182 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
154 | enc->cur += 12; |
183 | enc->cur += 12; |
155 | } |
184 | } |
156 | else |
185 | else |
157 | { |
186 | { |
158 | static char hexdigit [16] = "0123456789abcdef"; |
187 | static char hexdigit [16] = "0123456789abcdef"; |
… | |
… | |
176 | } |
205 | } |
177 | while (--clen); |
206 | while (--clen); |
178 | } |
207 | } |
179 | else |
208 | else |
180 | { |
209 | { |
181 | need (enc, len += 10); // never more than 11 bytes needed |
210 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
182 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
211 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
183 | ++str; |
212 | ++str; |
184 | } |
213 | } |
185 | } |
214 | } |
186 | } |
215 | } |
… | |
… | |
188 | |
217 | |
189 | --len; |
218 | --len; |
190 | } |
219 | } |
191 | } |
220 | } |
192 | |
221 | |
193 | #define INDENT SB \ |
222 | static void |
|
|
223 | encode_indent (enc_t *enc) |
|
|
224 | { |
194 | if (enc->flags & F_INDENT) \ |
225 | if (enc->flags & F_INDENT) |
195 | { \ |
226 | { |
196 | int i_; \ |
227 | int spaces = enc->indent * INDENT_STEP; |
197 | need (enc, enc->indent); \ |
|
|
198 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
199 | encode_ch (enc, ' '); \ |
|
|
200 | } \ |
|
|
201 | SE |
|
|
202 | |
228 | |
203 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
229 | need (enc, spaces); |
204 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
230 | memset (enc->cur, ' ', spaces); |
205 | #define COMMA SB \ |
231 | enc->cur += spaces; |
|
|
232 | } |
|
|
233 | } |
|
|
234 | |
|
|
235 | static void |
|
|
236 | encode_space (enc_t *enc) |
|
|
237 | { |
|
|
238 | need (enc, 1); |
206 | encode_ch (enc, ','); \ |
239 | encode_ch (enc, ' '); |
|
|
240 | } |
|
|
241 | |
|
|
242 | static void |
|
|
243 | encode_nl (enc_t *enc) |
|
|
244 | { |
207 | if (enc->flags & F_INDENT) \ |
245 | if (enc->flags & F_INDENT) |
208 | NL; \ |
246 | { |
|
|
247 | need (enc, 1); |
|
|
248 | encode_ch (enc, '\n'); |
|
|
249 | } |
|
|
250 | } |
|
|
251 | |
|
|
252 | static void |
|
|
253 | encode_comma (enc_t *enc) |
|
|
254 | { |
|
|
255 | encode_ch (enc, ','); |
|
|
256 | |
|
|
257 | if (enc->flags & F_INDENT) |
|
|
258 | encode_nl (enc); |
209 | else if (enc->flags & F_SPACE_AFTER) \ |
259 | else if (enc->flags & F_SPACE_AFTER) |
210 | SPACE; \ |
260 | encode_space (enc); |
211 | SE |
261 | } |
212 | |
262 | |
213 | static void encode_sv (enc_t *enc, SV *sv); |
263 | static void encode_sv (enc_t *enc, SV *sv); |
214 | |
264 | |
215 | static void |
265 | static void |
216 | encode_av (enc_t *enc, AV *av) |
266 | encode_av (enc_t *enc, AV *av) |
217 | { |
267 | { |
218 | int i, len = av_len (av); |
268 | int i, len = av_len (av); |
219 | |
269 | |
220 | encode_ch (enc, '['); NL; |
270 | encode_ch (enc, '['); encode_nl (enc); |
221 | ++enc->indent; |
271 | ++enc->indent; |
222 | |
272 | |
223 | for (i = 0; i <= len; ++i) |
273 | for (i = 0; i <= len; ++i) |
224 | { |
274 | { |
225 | INDENT; |
275 | encode_indent (enc); |
226 | encode_sv (enc, *av_fetch (av, i, 0)); |
276 | encode_sv (enc, *av_fetch (av, i, 0)); |
227 | |
277 | |
228 | if (i < len) |
278 | if (i < len) |
229 | COMMA; |
279 | encode_comma (enc); |
230 | } |
280 | } |
231 | |
281 | |
232 | NL; |
282 | encode_nl (enc); |
233 | |
283 | |
234 | --enc->indent; |
284 | --enc->indent; |
235 | INDENT; encode_ch (enc, ']'); |
285 | encode_indent (enc); encode_ch (enc, ']'); |
236 | } |
286 | } |
237 | |
287 | |
238 | static void |
288 | static void |
239 | encode_he (enc_t *enc, HE *he) |
289 | encode_he (enc_t *enc, HE *he) |
240 | { |
290 | { |
… | |
… | |
254 | else |
304 | else |
255 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
305 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
256 | |
306 | |
257 | encode_ch (enc, '"'); |
307 | encode_ch (enc, '"'); |
258 | |
308 | |
259 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
309 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
260 | encode_ch (enc, ':'); |
310 | encode_ch (enc, ':'); |
261 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
311 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
262 | encode_sv (enc, HeVAL (he)); |
312 | encode_sv (enc, HeVAL (he)); |
263 | } |
313 | } |
264 | |
314 | |
265 | // compare hash entries, used when all keys are bytestrings |
315 | // compare hash entries, used when all keys are bytestrings |
266 | static int |
316 | static int |
… | |
… | |
272 | HE *b = *(HE **)b_; |
322 | HE *b = *(HE **)b_; |
273 | |
323 | |
274 | STRLEN la = HeKLEN (a); |
324 | STRLEN la = HeKLEN (a); |
275 | STRLEN lb = HeKLEN (b); |
325 | STRLEN lb = HeKLEN (b); |
276 | |
326 | |
277 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
327 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
278 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
328 | cmp = la - lb; |
279 | |
329 | |
280 | return cmp; |
330 | return cmp; |
281 | } |
331 | } |
282 | |
332 | |
283 | // compare hash entries, used when some keys are sv's or utf-x |
333 | // compare hash entries, used when some keys are sv's or utf-x |
… | |
… | |
290 | static void |
340 | static void |
291 | encode_hv (enc_t *enc, HV *hv) |
341 | encode_hv (enc_t *enc, HV *hv) |
292 | { |
342 | { |
293 | int count, i; |
343 | int count, i; |
294 | |
344 | |
295 | encode_ch (enc, '{'); NL; ++enc->indent; |
345 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
296 | |
346 | |
297 | if ((count = hv_iterinit (hv))) |
347 | if ((count = hv_iterinit (hv))) |
298 | { |
348 | { |
299 | // for canonical output we have to sort by keys first |
349 | // for canonical output we have to sort by keys first |
300 | // actually, this is mostly due to the stupid so-called |
350 | // actually, this is mostly due to the stupid so-called |
301 | // security workaround added somewhere in 5.8.x. |
351 | // security workaround added somewhere in 5.8.x. |
302 | // that randomises hash orderings |
352 | // that randomises hash orderings |
303 | if (enc->flags & F_CANONICAL) |
353 | if (enc->flags & F_CANONICAL) |
304 | { |
354 | { |
305 | HE *he, *hes [count]; |
355 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
306 | int fast = 1; |
356 | int fast = 1; |
307 | |
357 | |
308 | i = 0; |
358 | i = 0; |
309 | while ((he = hv_iternext (hv))) |
359 | while ((he = hv_iternext (hv))) |
310 | { |
360 | { |
… | |
… | |
317 | |
367 | |
318 | if (fast) |
368 | if (fast) |
319 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
369 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
320 | else |
370 | else |
321 | { |
371 | { |
322 | // hack to disable "use bytes" |
372 | // hack to forcefully disable "use bytes" |
323 | COP *oldcop = PL_curcop, cop; |
373 | COP cop = *PL_curcop; |
324 | cop.op_private = 0; |
374 | cop.op_private = 0; |
|
|
375 | |
|
|
376 | ENTER; |
|
|
377 | SAVETMPS; |
|
|
378 | |
|
|
379 | SAVEVPTR (PL_curcop); |
325 | PL_curcop = &cop; |
380 | PL_curcop = &cop; |
326 | |
381 | |
327 | SAVETMPS; |
|
|
328 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
382 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
383 | |
329 | FREETMPS; |
384 | FREETMPS; |
330 | |
385 | LEAVE; |
331 | PL_curcop = oldcop; |
|
|
332 | } |
386 | } |
333 | |
387 | |
334 | for (i = 0; i < count; ++i) |
388 | for (i = 0; i < count; ++i) |
335 | { |
389 | { |
336 | INDENT; |
390 | encode_indent (enc); |
337 | encode_he (enc, hes [i]); |
391 | encode_he (enc, hes [i]); |
338 | |
392 | |
339 | if (i < count - 1) |
393 | if (i < count - 1) |
340 | COMMA; |
394 | encode_comma (enc); |
341 | } |
395 | } |
342 | |
396 | |
343 | NL; |
397 | encode_nl (enc); |
344 | } |
398 | } |
345 | else |
399 | else |
346 | { |
400 | { |
347 | SV *sv; |
401 | SV *sv; |
348 | HE *he = hv_iternext (hv); |
402 | HE *he = hv_iternext (hv); |
349 | |
403 | |
350 | for (;;) |
404 | for (;;) |
351 | { |
405 | { |
352 | INDENT; |
406 | encode_indent (enc); |
353 | encode_he (enc, he); |
407 | encode_he (enc, he); |
354 | |
408 | |
355 | if (!(he = hv_iternext (hv))) |
409 | if (!(he = hv_iternext (hv))) |
356 | break; |
410 | break; |
357 | |
411 | |
358 | COMMA; |
412 | encode_comma (enc); |
359 | } |
413 | } |
360 | |
414 | |
361 | NL; |
415 | encode_nl (enc); |
362 | } |
416 | } |
363 | } |
417 | } |
364 | |
418 | |
365 | --enc->indent; INDENT; encode_ch (enc, '}'); |
419 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
366 | } |
420 | } |
367 | |
421 | |
368 | static void |
422 | static void |
369 | encode_sv (enc_t *enc, SV *sv) |
423 | encode_sv (enc_t *enc, SV *sv) |
370 | { |
424 | { |
… | |
… | |
392 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
446 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
393 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
447 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
394 | } |
448 | } |
395 | else if (SvROK (sv)) |
449 | else if (SvROK (sv)) |
396 | { |
450 | { |
397 | if (!--enc->max_recurse) |
451 | SV *rv = SvRV (sv); |
|
|
452 | |
|
|
453 | if (enc->indent >= enc->max_depth) |
398 | croak ("data structure too deep (hit recursion limit)"); |
454 | croak ("data structure too deep (hit recursion limit)"); |
399 | |
455 | |
400 | sv = SvRV (sv); |
|
|
401 | |
|
|
402 | switch (SvTYPE (sv)) |
456 | switch (SvTYPE (rv)) |
403 | { |
457 | { |
404 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
458 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
405 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
459 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
406 | |
460 | |
407 | default: |
461 | default: |
408 | croak ("JSON can only represent references to arrays or hashes"); |
462 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
463 | SvPV_nolen (sv)); |
409 | } |
464 | } |
410 | } |
465 | } |
411 | else if (!SvOK (sv)) |
466 | else if (!SvOK (sv)) |
412 | encode_str (enc, "null", 4, 0); |
467 | encode_str (enc, "null", 4, 0); |
413 | else |
468 | else |
414 | croak ("encountered perl type that JSON cannot handle"); |
469 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
|
|
470 | SvPV_nolen (sv), SvFLAGS (sv)); |
415 | } |
471 | } |
416 | |
472 | |
417 | static SV * |
473 | static SV * |
418 | encode_json (SV *scalar, UV flags) |
474 | encode_json (SV *scalar, UV flags) |
419 | { |
475 | { |
420 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
476 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
421 | croak ("hash- or arraref required (not a simple scalar, use allow_nonref to allow this)"); |
477 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
422 | |
478 | |
423 | enc_t enc; |
479 | enc_t enc; |
424 | enc.flags = flags; |
480 | enc.flags = flags; |
425 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
481 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
426 | enc.cur = SvPVX (enc.sv); |
482 | enc.cur = SvPVX (enc.sv); |
427 | enc.end = SvEND (enc.sv); |
483 | enc.end = SvEND (enc.sv); |
428 | enc.max_recurse = 0; |
|
|
429 | enc.indent = 0; |
484 | enc.indent = 0; |
|
|
485 | enc.max_depth = 0x7fffffffUL; |
430 | |
486 | |
431 | SvPOK_only (enc.sv); |
487 | SvPOK_only (enc.sv); |
432 | encode_sv (&enc, scalar); |
488 | encode_sv (&enc, scalar); |
433 | |
489 | |
434 | if (!(flags & (F_ASCII | F_UTF8))) |
490 | if (!(flags & (F_ASCII | F_UTF8))) |
… | |
… | |
441 | |
497 | |
442 | return enc.sv; |
498 | return enc.sv; |
443 | } |
499 | } |
444 | |
500 | |
445 | ///////////////////////////////////////////////////////////////////////////// |
501 | ///////////////////////////////////////////////////////////////////////////// |
|
|
502 | // decoder |
446 | |
503 | |
447 | #define WS \ |
504 | // structure used for decoding JSON |
|
|
505 | typedef struct |
|
|
506 | { |
|
|
507 | char *cur; // current parser pointer |
|
|
508 | char *end; // end of input string |
|
|
509 | const char *err; // parse error, if != 0 |
|
|
510 | UV flags; // F_* |
|
|
511 | } dec_t; |
|
|
512 | |
|
|
513 | static void |
|
|
514 | decode_ws (dec_t *dec) |
|
|
515 | { |
448 | for (;;) \ |
516 | for (;;) |
449 | { \ |
517 | { |
450 | char ch = *dec->cur; \ |
518 | char ch = *dec->cur; |
|
|
519 | |
451 | if (ch > 0x20 \ |
520 | if (ch > 0x20 |
452 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
521 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
453 | break; \ |
522 | break; |
|
|
523 | |
454 | ++dec->cur; \ |
524 | ++dec->cur; |
455 | } |
525 | } |
|
|
526 | } |
456 | |
527 | |
457 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
528 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
458 | #define EXPECT_CH(ch) SB \ |
529 | #define EXPECT_CH(ch) SB \ |
459 | if (*dec->cur != ch) \ |
530 | if (*dec->cur != ch) \ |
460 | ERR (# ch " expected"); \ |
531 | ERR (# ch " expected"); \ |
… | |
… | |
467 | |
538 | |
468 | static UV |
539 | static UV |
469 | decode_4hex (dec_t *dec) |
540 | decode_4hex (dec_t *dec) |
470 | { |
541 | { |
471 | signed char d1, d2, d3, d4; |
542 | signed char d1, d2, d3, d4; |
|
|
543 | unsigned char *cur = (unsigned char *)dec->cur; |
472 | |
544 | |
473 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
474 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
545 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
475 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
|
|
476 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
546 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
477 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
478 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
547 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
479 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
480 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
548 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
481 | |
549 | |
482 | dec->cur += 4; |
550 | dec->cur += 4; |
483 | |
551 | |
484 | return ((UV)d1) << 12 |
552 | return ((UV)d1) << 12 |
485 | | ((UV)d2) << 8 |
553 | | ((UV)d2) << 8 |
… | |
… | |
488 | |
556 | |
489 | fail: |
557 | fail: |
490 | return (UV)-1; |
558 | return (UV)-1; |
491 | } |
559 | } |
492 | |
560 | |
493 | #define APPEND_GROW(n) SB \ |
|
|
494 | if (cur + (n) >= end) \ |
|
|
495 | { \ |
|
|
496 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
497 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
498 | cur = SvPVX (sv) + ofs; \ |
|
|
499 | end = SvEND (sv); \ |
|
|
500 | } \ |
|
|
501 | SE |
|
|
502 | |
|
|
503 | #define APPEND_CH(ch) SB \ |
|
|
504 | APPEND_GROW (1); \ |
|
|
505 | *cur++ = (ch); \ |
|
|
506 | SE |
|
|
507 | |
|
|
508 | static SV * |
561 | static SV * |
509 | decode_str (dec_t *dec) |
562 | decode_str (dec_t *dec) |
510 | { |
563 | { |
511 | SV *sv = NEWSV (0,2); |
564 | SV *sv = 0; |
512 | int utf8 = 0; |
565 | int utf8 = 0; |
513 | char *cur = SvPVX (sv); |
|
|
514 | char *end = SvEND (sv); |
|
|
515 | |
566 | |
516 | for (;;) |
567 | do |
517 | { |
568 | { |
518 | unsigned char ch = *(unsigned char *)dec->cur; |
569 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
|
|
570 | char *cur = buf; |
519 | |
571 | |
520 | if (ch == '"') |
572 | do |
521 | break; |
|
|
522 | else if (ch == '\\') |
|
|
523 | { |
573 | { |
524 | switch (*++dec->cur) |
574 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
575 | |
|
|
576 | if (ch == '"') |
525 | { |
577 | { |
526 | case '\\': |
578 | --dec->cur; |
527 | case '/': |
579 | break; |
528 | case '"': APPEND_CH (*dec->cur++); break; |
580 | } |
529 | |
581 | else if (ch == '\\') |
530 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
582 | { |
531 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
583 | switch (*dec->cur) |
532 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
533 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
534 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
535 | |
|
|
536 | case 'u': |
|
|
537 | { |
584 | { |
538 | UV lo, hi; |
585 | case '\\': |
539 | ++dec->cur; |
586 | case '/': |
|
|
587 | case '"': *cur++ = *dec->cur++; break; |
540 | |
588 | |
541 | hi = decode_4hex (dec); |
589 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
542 | if (hi == (UV)-1) |
590 | case 't': ++dec->cur; *cur++ = '\011'; break; |
543 | goto fail; |
591 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
|
|
592 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
|
|
593 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
544 | |
594 | |
545 | // possibly a surrogate pair |
595 | case 'u': |
546 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
547 | { |
596 | { |
548 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
597 | UV lo, hi; |
549 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
550 | |
|
|
551 | dec->cur += 2; |
598 | ++dec->cur; |
552 | |
599 | |
553 | lo = decode_4hex (dec); |
600 | hi = decode_4hex (dec); |
554 | if (lo == (UV)-1) |
601 | if (hi == (UV)-1) |
555 | goto fail; |
602 | goto fail; |
556 | |
603 | |
|
|
604 | // possibly a surrogate pair |
|
|
605 | if (hi >= 0xd800) |
|
|
606 | if (hi < 0xdc00) |
|
|
607 | { |
|
|
608 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
|
|
609 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
610 | |
|
|
611 | dec->cur += 2; |
|
|
612 | |
|
|
613 | lo = decode_4hex (dec); |
|
|
614 | if (lo == (UV)-1) |
|
|
615 | goto fail; |
|
|
616 | |
557 | if (lo < 0xdc00 || lo >= 0xe000) |
617 | if (lo < 0xdc00 || lo >= 0xe000) |
558 | ERR ("surrogate pair expected"); |
618 | ERR ("surrogate pair expected"); |
559 | |
619 | |
560 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
620 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
621 | } |
|
|
622 | else if (hi < 0xe000) |
|
|
623 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
624 | |
|
|
625 | if (hi >= 0x80) |
|
|
626 | { |
|
|
627 | utf8 = 1; |
|
|
628 | |
|
|
629 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
630 | } |
|
|
631 | else |
|
|
632 | *cur++ = hi; |
561 | } |
633 | } |
562 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
563 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
564 | |
|
|
565 | if (hi >= 0x80) |
|
|
566 | { |
634 | break; |
567 | utf8 = 1; |
|
|
568 | |
635 | |
569 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
570 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
571 | } |
|
|
572 | else |
636 | default: |
573 | APPEND_CH (hi); |
637 | --dec->cur; |
|
|
638 | ERR ("illegal backslash escape sequence in string"); |
574 | } |
639 | } |
575 | break; |
|
|
576 | |
|
|
577 | default: |
|
|
578 | --dec->cur; |
|
|
579 | ERR ("illegal backslash escape sequence in string"); |
|
|
580 | } |
640 | } |
|
|
641 | else if (ch >= 0x20 && ch <= 0x7f) |
|
|
642 | *cur++ = ch; |
|
|
643 | else if (ch >= 0x80) |
|
|
644 | { |
|
|
645 | --dec->cur; |
|
|
646 | |
|
|
647 | STRLEN clen; |
|
|
648 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
|
|
649 | if (clen == (STRLEN)-1) |
|
|
650 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
651 | |
|
|
652 | do |
|
|
653 | { |
|
|
654 | *cur++ = *dec->cur++; |
|
|
655 | } |
|
|
656 | while (--clen); |
|
|
657 | |
|
|
658 | utf8 = 1; |
|
|
659 | } |
|
|
660 | else if (!ch) |
|
|
661 | ERR ("unexpected end of string while parsing json string"); |
|
|
662 | else |
|
|
663 | ERR ("invalid character encountered"); |
|
|
664 | |
581 | } |
665 | } |
582 | else if (ch >= 0x20 && ch <= 0x7f) |
666 | while (cur < buf + SHORT_STRING_LEN); |
583 | APPEND_CH (*dec->cur++); |
667 | |
584 | else if (ch >= 0x80) |
668 | STRLEN len = cur - buf; |
|
|
669 | |
|
|
670 | if (sv) |
585 | { |
671 | { |
586 | STRLEN clen; |
672 | SvGROW (sv, SvCUR (sv) + len + 1); |
587 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
673 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
588 | if (clen == (STRLEN)-1) |
674 | SvCUR_set (sv, SvCUR (sv) + len); |
589 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
590 | |
|
|
591 | APPEND_GROW (clen); |
|
|
592 | do |
|
|
593 | { |
|
|
594 | *cur++ = *dec->cur++; |
|
|
595 | } |
|
|
596 | while (--clen); |
|
|
597 | |
|
|
598 | utf8 = 1; |
|
|
599 | } |
675 | } |
600 | else if (dec->cur == dec->end) |
|
|
601 | ERR ("unexpected end of string while parsing json string"); |
|
|
602 | else |
676 | else |
603 | ERR ("invalid character encountered"); |
677 | sv = newSVpvn (buf, len); |
604 | } |
678 | } |
|
|
679 | while (*dec->cur != '"'); |
605 | |
680 | |
606 | ++dec->cur; |
681 | ++dec->cur; |
607 | |
682 | |
608 | SvCUR_set (sv, cur - SvPVX (sv)); |
683 | if (sv) |
609 | |
684 | { |
610 | SvPOK_only (sv); |
685 | SvPOK_only (sv); |
611 | *SvEND (sv) = 0; |
686 | *SvEND (sv) = 0; |
612 | |
687 | |
613 | if (utf8) |
688 | if (utf8) |
614 | SvUTF8_on (sv); |
689 | SvUTF8_on (sv); |
615 | |
690 | } |
616 | if (dec->flags & F_SHRINK) |
691 | else |
617 | shrink (sv); |
692 | sv = newSVpvn ("", 0); |
618 | |
693 | |
619 | return sv; |
694 | return sv; |
620 | |
695 | |
621 | fail: |
696 | fail: |
622 | SvREFCNT_dec (sv); |
|
|
623 | return 0; |
697 | return 0; |
624 | } |
698 | } |
625 | |
699 | |
626 | static SV * |
700 | static SV * |
627 | decode_num (dec_t *dec) |
701 | decode_num (dec_t *dec) |
… | |
… | |
708 | static SV * |
782 | static SV * |
709 | decode_av (dec_t *dec) |
783 | decode_av (dec_t *dec) |
710 | { |
784 | { |
711 | AV *av = newAV (); |
785 | AV *av = newAV (); |
712 | |
786 | |
713 | WS; |
787 | decode_ws (dec); |
714 | if (*dec->cur == ']') |
788 | if (*dec->cur == ']') |
715 | ++dec->cur; |
789 | ++dec->cur; |
716 | else |
790 | else |
717 | for (;;) |
791 | for (;;) |
718 | { |
792 | { |
… | |
… | |
722 | if (!value) |
796 | if (!value) |
723 | goto fail; |
797 | goto fail; |
724 | |
798 | |
725 | av_push (av, value); |
799 | av_push (av, value); |
726 | |
800 | |
727 | WS; |
801 | decode_ws (dec); |
728 | |
802 | |
729 | if (*dec->cur == ']') |
803 | if (*dec->cur == ']') |
730 | { |
804 | { |
731 | ++dec->cur; |
805 | ++dec->cur; |
732 | break; |
806 | break; |
… | |
… | |
748 | static SV * |
822 | static SV * |
749 | decode_hv (dec_t *dec) |
823 | decode_hv (dec_t *dec) |
750 | { |
824 | { |
751 | HV *hv = newHV (); |
825 | HV *hv = newHV (); |
752 | |
826 | |
753 | WS; |
827 | decode_ws (dec); |
754 | if (*dec->cur == '}') |
828 | if (*dec->cur == '}') |
755 | ++dec->cur; |
829 | ++dec->cur; |
756 | else |
830 | else |
757 | for (;;) |
831 | for (;;) |
758 | { |
832 | { |
759 | SV *key, *value; |
833 | SV *key, *value; |
760 | |
834 | |
761 | WS; EXPECT_CH ('"'); |
835 | decode_ws (dec); EXPECT_CH ('"'); |
762 | |
836 | |
763 | key = decode_str (dec); |
837 | key = decode_str (dec); |
764 | if (!key) |
838 | if (!key) |
765 | goto fail; |
839 | goto fail; |
766 | |
840 | |
767 | WS; EXPECT_CH (':'); |
841 | decode_ws (dec); EXPECT_CH (':'); |
768 | |
842 | |
769 | value = decode_sv (dec); |
843 | value = decode_sv (dec); |
770 | if (!value) |
844 | if (!value) |
771 | { |
845 | { |
772 | SvREFCNT_dec (key); |
846 | SvREFCNT_dec (key); |
… | |
… | |
774 | } |
848 | } |
775 | |
849 | |
776 | //TODO: optimise |
850 | //TODO: optimise |
777 | hv_store_ent (hv, key, value, 0); |
851 | hv_store_ent (hv, key, value, 0); |
778 | |
852 | |
779 | WS; |
853 | decode_ws (dec); |
780 | |
854 | |
781 | if (*dec->cur == '}') |
855 | if (*dec->cur == '}') |
782 | { |
856 | { |
783 | ++dec->cur; |
857 | ++dec->cur; |
784 | break; |
858 | break; |
… | |
… | |
798 | } |
872 | } |
799 | |
873 | |
800 | static SV * |
874 | static SV * |
801 | decode_sv (dec_t *dec) |
875 | decode_sv (dec_t *dec) |
802 | { |
876 | { |
803 | WS; |
877 | decode_ws (dec); |
804 | switch (*dec->cur) |
878 | switch (*dec->cur) |
805 | { |
879 | { |
806 | case '"': ++dec->cur; return decode_str (dec); |
880 | case '"': ++dec->cur; return decode_str (dec); |
807 | case '[': ++dec->cur; return decode_av (dec); |
881 | case '[': ++dec->cur; return decode_av (dec); |
808 | case '{': ++dec->cur; return decode_hv (dec); |
882 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
878 | { |
952 | { |
879 | IV offset = dec.flags & F_UTF8 |
953 | IV offset = dec.flags & F_UTF8 |
880 | ? dec.cur - SvPVX (string) |
954 | ? dec.cur - SvPVX (string) |
881 | : utf8_distance (dec.cur, SvPVX (string)); |
955 | : utf8_distance (dec.cur, SvPVX (string)); |
882 | SV *uni = sv_newmortal (); |
956 | SV *uni = sv_newmortal (); |
|
|
957 | |
883 | // horrible hack to silence warning inside pv_uni_display |
958 | // horrible hack to silence warning inside pv_uni_display |
884 | COP cop; |
959 | COP cop = *PL_curcop; |
885 | memset (&cop, 0, sizeof (cop)); |
|
|
886 | cop.cop_warnings = pWARN_NONE; |
960 | cop.cop_warnings = pWARN_NONE; |
|
|
961 | ENTER; |
887 | SAVEVPTR (PL_curcop); |
962 | SAVEVPTR (PL_curcop); |
888 | PL_curcop = &cop; |
963 | PL_curcop = &cop; |
889 | |
|
|
890 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
964 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
965 | LEAVE; |
|
|
966 | |
891 | croak ("%s, at character offset %d (%s)", |
967 | croak ("%s, at character offset %d (%s)", |
892 | dec.err, |
968 | dec.err, |
893 | (int)offset, |
969 | (int)offset, |
894 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
970 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
895 | } |
971 | } |
896 | |
972 | |
897 | sv = sv_2mortal (sv); |
973 | sv = sv_2mortal (sv); |
898 | |
974 | |
899 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
975 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
900 | croak ("JSON object or array expected (but number, string, true, false or null found, use allow_nonref to allow this)"); |
976 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
901 | |
977 | |
902 | return sv; |
978 | return sv; |
903 | } |
979 | } |
|
|
980 | |
|
|
981 | ///////////////////////////////////////////////////////////////////////////// |
|
|
982 | // XS interface functions |
904 | |
983 | |
905 | MODULE = JSON::XS PACKAGE = JSON::XS |
984 | MODULE = JSON::XS PACKAGE = JSON::XS |
906 | |
985 | |
907 | BOOT: |
986 | BOOT: |
908 | { |
987 | { |
… | |
… | |
935 | utf8 = F_UTF8 |
1014 | utf8 = F_UTF8 |
936 | indent = F_INDENT |
1015 | indent = F_INDENT |
937 | canonical = F_CANONICAL |
1016 | canonical = F_CANONICAL |
938 | space_before = F_SPACE_BEFORE |
1017 | space_before = F_SPACE_BEFORE |
939 | space_after = F_SPACE_AFTER |
1018 | space_after = F_SPACE_AFTER |
940 | json_rpc = F_JSON_RPC |
|
|
941 | pretty = F_PRETTY |
1019 | pretty = F_PRETTY |
942 | allow_nonref = F_ALLOW_NONREF |
1020 | allow_nonref = F_ALLOW_NONREF |
943 | shrink = F_SHRINK |
1021 | shrink = F_SHRINK |
944 | CODE: |
1022 | CODE: |
945 | { |
1023 | { |
… | |
… | |
963 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1041 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
964 | |
1042 | |
965 | PROTOTYPES: ENABLE |
1043 | PROTOTYPES: ENABLE |
966 | |
1044 | |
967 | void to_json (SV *scalar) |
1045 | void to_json (SV *scalar) |
|
|
1046 | ALIAS: |
|
|
1047 | objToJson = 0 |
968 | PPCODE: |
1048 | PPCODE: |
969 | XPUSHs (encode_json (scalar, F_UTF8)); |
1049 | XPUSHs (encode_json (scalar, F_UTF8)); |
970 | |
1050 | |
971 | void from_json (SV *jsonstr) |
1051 | void from_json (SV *jsonstr) |
|
|
1052 | ALIAS: |
|
|
1053 | jsonToObj = 0 |
972 | PPCODE: |
1054 | PPCODE: |
973 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1055 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
974 | |
1056 | |