… | |
… | |
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_ALLOW_NONREF 0x00000080 |
15 | #define F_ALLOW_NONREF 0x00000080 |
16 | #define F_SHRINK 0x00000100 |
16 | #define F_SHRINK 0x00000100 |
17 | |
17 | |
|
|
18 | // F_SKIPINVALID? |
|
|
19 | // F_EXECCODEREF? |
|
|
20 | // F_SELFCONVERT? |
|
|
21 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
22 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
23 | #define F_DEFAULT 0 |
20 | |
24 | |
21 | #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 |
22 | |
30 | |
23 | #define SB do { |
31 | #define SB do { |
24 | #define SE } while (0) |
32 | #define SE } while (0) |
25 | |
33 | |
26 | 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 |
27 | |
83 | |
28 | // structure used for encoding JSON |
84 | // structure used for encoding JSON |
29 | typedef struct |
85 | typedef struct |
30 | { |
86 | { |
31 | char *cur; |
87 | char *cur; // SvPVX (sv) + current output position |
32 | STRLEN len; // SvLEN (sv) |
|
|
33 | char *end; // SvEND (sv) |
88 | char *end; // SvEND (sv) |
34 | SV *sv; |
89 | SV *sv; // result scalar |
35 | UV flags; |
90 | UV flags; // F_* |
36 | int max_recurse; |
91 | int indent; // indentation level |
37 | int indent; |
92 | int max_depth; // max. recursion level |
38 | } enc_t; |
93 | } enc_t; |
39 | |
|
|
40 | // structure used for decoding JSON |
|
|
41 | typedef struct |
|
|
42 | { |
|
|
43 | char *cur; |
|
|
44 | char *end; |
|
|
45 | const char *err; |
|
|
46 | UV flags; |
|
|
47 | } dec_t; |
|
|
48 | |
|
|
49 | static UV * |
|
|
50 | SvJSON (SV *sv) |
|
|
51 | { |
|
|
52 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
53 | croak ("object is not of type JSON::XS"); |
|
|
54 | |
|
|
55 | return &SvUVX (SvRV (sv)); |
|
|
56 | } |
|
|
57 | |
|
|
58 | static void |
|
|
59 | shrink (SV *sv) |
|
|
60 | { |
|
|
61 | sv_utf8_downgrade (sv, 1); |
|
|
62 | #ifdef SvPV_shrink_to_cur |
|
|
63 | SvPV_shrink_to_cur (sv); |
|
|
64 | #endif |
|
|
65 | } |
|
|
66 | |
|
|
67 | ///////////////////////////////////////////////////////////////////////////// |
|
|
68 | |
94 | |
69 | static void |
95 | static void |
70 | need (enc_t *enc, STRLEN len) |
96 | need (enc_t *enc, STRLEN len) |
71 | { |
97 | { |
72 | if (enc->cur + len >= enc->end) |
98 | if (enc->cur + len >= enc->end) |
… | |
… | |
130 | STRLEN clen; |
156 | STRLEN clen; |
131 | UV uch; |
157 | UV uch; |
132 | |
158 | |
133 | if (is_utf8) |
159 | if (is_utf8) |
134 | { |
160 | { |
135 | 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); |
136 | if (clen == (STRLEN)-1) |
163 | if (clen == (STRLEN)-1) |
137 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
164 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
138 | } |
165 | } |
139 | else |
166 | else |
140 | { |
167 | { |
… | |
… | |
178 | } |
205 | } |
179 | while (--clen); |
206 | while (--clen); |
180 | } |
207 | } |
181 | else |
208 | else |
182 | { |
209 | { |
183 | need (enc, len += 10); // never more than 11 bytes needed |
210 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
184 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
211 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
185 | ++str; |
212 | ++str; |
186 | } |
213 | } |
187 | } |
214 | } |
188 | } |
215 | } |
… | |
… | |
190 | |
217 | |
191 | --len; |
218 | --len; |
192 | } |
219 | } |
193 | } |
220 | } |
194 | |
221 | |
195 | #define INDENT SB \ |
222 | static void |
|
|
223 | encode_indent (enc_t *enc) |
|
|
224 | { |
196 | if (enc->flags & F_INDENT) \ |
225 | if (enc->flags & F_INDENT) |
197 | { \ |
226 | { |
198 | int i_; \ |
227 | int spaces = enc->indent * INDENT_STEP; |
199 | need (enc, enc->indent); \ |
|
|
200 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
201 | encode_ch (enc, ' '); \ |
|
|
202 | } \ |
|
|
203 | SE |
|
|
204 | |
228 | |
205 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
229 | need (enc, spaces); |
206 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
230 | memset (enc->cur, ' ', spaces); |
207 | #define COMMA SB \ |
231 | enc->cur += spaces; |
|
|
232 | } |
|
|
233 | } |
|
|
234 | |
|
|
235 | static void |
|
|
236 | encode_space (enc_t *enc) |
|
|
237 | { |
|
|
238 | need (enc, 1); |
208 | encode_ch (enc, ','); \ |
239 | encode_ch (enc, ' '); |
|
|
240 | } |
|
|
241 | |
|
|
242 | static void |
|
|
243 | encode_nl (enc_t *enc) |
|
|
244 | { |
209 | if (enc->flags & F_INDENT) \ |
245 | if (enc->flags & F_INDENT) |
210 | 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); |
211 | else if (enc->flags & F_SPACE_AFTER) \ |
259 | else if (enc->flags & F_SPACE_AFTER) |
212 | SPACE; \ |
260 | encode_space (enc); |
213 | SE |
261 | } |
214 | |
262 | |
215 | static void encode_sv (enc_t *enc, SV *sv); |
263 | static void encode_sv (enc_t *enc, SV *sv); |
216 | |
264 | |
217 | static void |
265 | static void |
218 | encode_av (enc_t *enc, AV *av) |
266 | encode_av (enc_t *enc, AV *av) |
219 | { |
267 | { |
220 | int i, len = av_len (av); |
268 | int i, len = av_len (av); |
221 | |
269 | |
222 | encode_ch (enc, '['); NL; |
270 | encode_ch (enc, '['); encode_nl (enc); |
223 | ++enc->indent; |
271 | ++enc->indent; |
224 | |
272 | |
225 | for (i = 0; i <= len; ++i) |
273 | for (i = 0; i <= len; ++i) |
226 | { |
274 | { |
227 | INDENT; |
275 | encode_indent (enc); |
228 | encode_sv (enc, *av_fetch (av, i, 0)); |
276 | encode_sv (enc, *av_fetch (av, i, 0)); |
229 | |
277 | |
230 | if (i < len) |
278 | if (i < len) |
231 | COMMA; |
279 | encode_comma (enc); |
232 | } |
280 | } |
233 | |
281 | |
234 | NL; |
282 | encode_nl (enc); |
235 | |
283 | |
236 | --enc->indent; |
284 | --enc->indent; |
237 | INDENT; encode_ch (enc, ']'); |
285 | encode_indent (enc); encode_ch (enc, ']'); |
238 | } |
286 | } |
239 | |
287 | |
240 | static void |
288 | static void |
241 | encode_he (enc_t *enc, HE *he) |
289 | encode_he (enc_t *enc, HE *he) |
242 | { |
290 | { |
… | |
… | |
256 | else |
304 | else |
257 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
305 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
258 | |
306 | |
259 | encode_ch (enc, '"'); |
307 | encode_ch (enc, '"'); |
260 | |
308 | |
261 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
309 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
262 | encode_ch (enc, ':'); |
310 | encode_ch (enc, ':'); |
263 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
311 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
264 | encode_sv (enc, HeVAL (he)); |
312 | encode_sv (enc, HeVAL (he)); |
265 | } |
313 | } |
266 | |
314 | |
267 | // compare hash entries, used when all keys are bytestrings |
315 | // compare hash entries, used when all keys are bytestrings |
268 | static int |
316 | static int |
… | |
… | |
274 | HE *b = *(HE **)b_; |
322 | HE *b = *(HE **)b_; |
275 | |
323 | |
276 | STRLEN la = HeKLEN (a); |
324 | STRLEN la = HeKLEN (a); |
277 | STRLEN lb = HeKLEN (b); |
325 | STRLEN lb = HeKLEN (b); |
278 | |
326 | |
279 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
327 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
280 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
328 | cmp = la - lb; |
281 | |
329 | |
282 | return cmp; |
330 | return cmp; |
283 | } |
331 | } |
284 | |
332 | |
285 | // 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 |
… | |
… | |
292 | static void |
340 | static void |
293 | encode_hv (enc_t *enc, HV *hv) |
341 | encode_hv (enc_t *enc, HV *hv) |
294 | { |
342 | { |
295 | int count, i; |
343 | int count, i; |
296 | |
344 | |
297 | encode_ch (enc, '{'); NL; ++enc->indent; |
345 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
298 | |
346 | |
299 | if ((count = hv_iterinit (hv))) |
347 | if ((count = hv_iterinit (hv))) |
300 | { |
348 | { |
301 | // for canonical output we have to sort by keys first |
349 | // for canonical output we have to sort by keys first |
302 | // actually, this is mostly due to the stupid so-called |
350 | // actually, this is mostly due to the stupid so-called |
303 | // security workaround added somewhere in 5.8.x. |
351 | // security workaround added somewhere in 5.8.x. |
304 | // that randomises hash orderings |
352 | // that randomises hash orderings |
305 | if (enc->flags & F_CANONICAL) |
353 | if (enc->flags & F_CANONICAL) |
306 | { |
354 | { |
307 | HE *he, *hes [count]; |
355 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
308 | int fast = 1; |
356 | int fast = 1; |
309 | |
357 | |
310 | i = 0; |
358 | i = 0; |
311 | while ((he = hv_iternext (hv))) |
359 | while ((he = hv_iternext (hv))) |
312 | { |
360 | { |
… | |
… | |
337 | LEAVE; |
385 | LEAVE; |
338 | } |
386 | } |
339 | |
387 | |
340 | for (i = 0; i < count; ++i) |
388 | for (i = 0; i < count; ++i) |
341 | { |
389 | { |
342 | INDENT; |
390 | encode_indent (enc); |
343 | encode_he (enc, hes [i]); |
391 | encode_he (enc, hes [i]); |
344 | |
392 | |
345 | if (i < count - 1) |
393 | if (i < count - 1) |
346 | COMMA; |
394 | encode_comma (enc); |
347 | } |
395 | } |
348 | |
396 | |
349 | NL; |
397 | encode_nl (enc); |
350 | } |
398 | } |
351 | else |
399 | else |
352 | { |
400 | { |
353 | SV *sv; |
401 | SV *sv; |
354 | HE *he = hv_iternext (hv); |
402 | HE *he = hv_iternext (hv); |
355 | |
403 | |
356 | for (;;) |
404 | for (;;) |
357 | { |
405 | { |
358 | INDENT; |
406 | encode_indent (enc); |
359 | encode_he (enc, he); |
407 | encode_he (enc, he); |
360 | |
408 | |
361 | if (!(he = hv_iternext (hv))) |
409 | if (!(he = hv_iternext (hv))) |
362 | break; |
410 | break; |
363 | |
411 | |
364 | COMMA; |
412 | encode_comma (enc); |
365 | } |
413 | } |
366 | |
414 | |
367 | NL; |
415 | encode_nl (enc); |
368 | } |
416 | } |
369 | } |
417 | } |
370 | |
418 | |
371 | --enc->indent; INDENT; encode_ch (enc, '}'); |
419 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
372 | } |
420 | } |
373 | |
421 | |
374 | static void |
422 | static void |
375 | encode_sv (enc_t *enc, SV *sv) |
423 | encode_sv (enc_t *enc, SV *sv) |
376 | { |
424 | { |
… | |
… | |
400 | } |
448 | } |
401 | else if (SvROK (sv)) |
449 | else if (SvROK (sv)) |
402 | { |
450 | { |
403 | SV *rv = SvRV (sv); |
451 | SV *rv = SvRV (sv); |
404 | |
452 | |
405 | if (!--enc->max_recurse) |
453 | if (enc->indent >= enc->max_depth) |
406 | croak ("data structure too deep (hit recursion limit)"); |
454 | croak ("data structure too deep (hit recursion limit)"); |
407 | |
455 | |
408 | switch (SvTYPE (rv)) |
456 | switch (SvTYPE (rv)) |
409 | { |
457 | { |
410 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
458 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
… | |
… | |
427 | { |
475 | { |
428 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
476 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
429 | croak ("hash- or arrayref expected (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)"); |
430 | |
478 | |
431 | enc_t enc; |
479 | enc_t enc; |
432 | enc.flags = flags; |
480 | enc.flags = flags; |
433 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
481 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
434 | enc.cur = SvPVX (enc.sv); |
482 | enc.cur = SvPVX (enc.sv); |
435 | enc.end = SvEND (enc.sv); |
483 | enc.end = SvEND (enc.sv); |
436 | enc.max_recurse = 0; |
|
|
437 | enc.indent = 0; |
484 | enc.indent = 0; |
|
|
485 | enc.max_depth = 0x7fffffffUL; |
438 | |
486 | |
439 | SvPOK_only (enc.sv); |
487 | SvPOK_only (enc.sv); |
440 | encode_sv (&enc, scalar); |
488 | encode_sv (&enc, scalar); |
441 | |
489 | |
442 | if (!(flags & (F_ASCII | F_UTF8))) |
490 | if (!(flags & (F_ASCII | F_UTF8))) |
… | |
… | |
449 | |
497 | |
450 | return enc.sv; |
498 | return enc.sv; |
451 | } |
499 | } |
452 | |
500 | |
453 | ///////////////////////////////////////////////////////////////////////////// |
501 | ///////////////////////////////////////////////////////////////////////////// |
|
|
502 | // decoder |
454 | |
503 | |
455 | #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 | { |
456 | for (;;) \ |
516 | for (;;) |
457 | { \ |
517 | { |
458 | char ch = *dec->cur; \ |
518 | char ch = *dec->cur; |
|
|
519 | |
459 | if (ch > 0x20 \ |
520 | if (ch > 0x20 |
460 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
521 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
461 | break; \ |
522 | break; |
|
|
523 | |
462 | ++dec->cur; \ |
524 | ++dec->cur; |
463 | } |
525 | } |
|
|
526 | } |
464 | |
527 | |
465 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
528 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
466 | #define EXPECT_CH(ch) SB \ |
529 | #define EXPECT_CH(ch) SB \ |
467 | if (*dec->cur != ch) \ |
530 | if (*dec->cur != ch) \ |
468 | ERR (# ch " expected"); \ |
531 | ERR (# ch " expected"); \ |
… | |
… | |
475 | |
538 | |
476 | static UV |
539 | static UV |
477 | decode_4hex (dec_t *dec) |
540 | decode_4hex (dec_t *dec) |
478 | { |
541 | { |
479 | signed char d1, d2, d3, d4; |
542 | signed char d1, d2, d3, d4; |
|
|
543 | unsigned char *cur = (unsigned char *)dec->cur; |
480 | |
544 | |
481 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
482 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
545 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
483 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
|
|
484 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
546 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
485 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
486 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
547 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
487 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
488 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
548 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
489 | |
549 | |
490 | dec->cur += 4; |
550 | dec->cur += 4; |
491 | |
551 | |
492 | return ((UV)d1) << 12 |
552 | return ((UV)d1) << 12 |
493 | | ((UV)d2) << 8 |
553 | | ((UV)d2) << 8 |
… | |
… | |
496 | |
556 | |
497 | fail: |
557 | fail: |
498 | return (UV)-1; |
558 | return (UV)-1; |
499 | } |
559 | } |
500 | |
560 | |
501 | #define APPEND_GROW(n) SB \ |
|
|
502 | if (cur + (n) >= end) \ |
|
|
503 | { \ |
|
|
504 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
505 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
506 | cur = SvPVX (sv) + ofs; \ |
|
|
507 | end = SvEND (sv); \ |
|
|
508 | } \ |
|
|
509 | SE |
|
|
510 | |
|
|
511 | #define APPEND_CH(ch) SB \ |
|
|
512 | APPEND_GROW (1); \ |
|
|
513 | *cur++ = (ch); \ |
|
|
514 | SE |
|
|
515 | |
|
|
516 | static SV * |
561 | static SV * |
517 | decode_str (dec_t *dec) |
562 | decode_str (dec_t *dec) |
518 | { |
563 | { |
519 | SV *sv = NEWSV (0,2); |
564 | SV *sv = 0; |
520 | int utf8 = 0; |
565 | int utf8 = 0; |
521 | char *cur = SvPVX (sv); |
|
|
522 | char *end = SvEND (sv); |
|
|
523 | |
566 | |
524 | for (;;) |
567 | do |
525 | { |
568 | { |
526 | unsigned char ch = *(unsigned char *)dec->cur; |
569 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
|
|
570 | char *cur = buf; |
527 | |
571 | |
528 | if (ch == '"') |
572 | do |
529 | break; |
|
|
530 | else if (ch == '\\') |
|
|
531 | { |
573 | { |
532 | switch (*++dec->cur) |
574 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
575 | |
|
|
576 | if (ch == '"') |
533 | { |
577 | { |
534 | case '\\': |
578 | --dec->cur; |
535 | case '/': |
579 | break; |
536 | case '"': APPEND_CH (*dec->cur++); break; |
580 | } |
537 | |
581 | else if (ch == '\\') |
538 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
582 | { |
539 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
583 | switch (*dec->cur) |
540 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
541 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
542 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
543 | |
|
|
544 | case 'u': |
|
|
545 | { |
584 | { |
546 | UV lo, hi; |
585 | case '\\': |
547 | ++dec->cur; |
586 | case '/': |
|
|
587 | case '"': *cur++ = *dec->cur++; break; |
548 | |
588 | |
549 | hi = decode_4hex (dec); |
589 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
550 | if (hi == (UV)-1) |
590 | case 't': ++dec->cur; *cur++ = '\011'; break; |
551 | 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; |
552 | |
594 | |
553 | // possibly a surrogate pair |
595 | case 'u': |
554 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
555 | { |
596 | { |
556 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
597 | UV lo, hi; |
557 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
558 | |
|
|
559 | dec->cur += 2; |
598 | ++dec->cur; |
560 | |
599 | |
561 | lo = decode_4hex (dec); |
600 | hi = decode_4hex (dec); |
562 | if (lo == (UV)-1) |
601 | if (hi == (UV)-1) |
563 | goto fail; |
602 | goto fail; |
564 | |
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 | |
565 | if (lo < 0xdc00 || lo >= 0xe000) |
617 | if (lo < 0xdc00 || lo >= 0xe000) |
566 | ERR ("surrogate pair expected"); |
618 | ERR ("surrogate pair expected"); |
567 | |
619 | |
568 | 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; |
569 | } |
633 | } |
570 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
571 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
572 | |
|
|
573 | if (hi >= 0x80) |
|
|
574 | { |
634 | break; |
575 | utf8 = 1; |
|
|
576 | |
635 | |
577 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
578 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
579 | } |
|
|
580 | else |
636 | default: |
581 | APPEND_CH (hi); |
637 | --dec->cur; |
|
|
638 | ERR ("illegal backslash escape sequence in string"); |
582 | } |
639 | } |
583 | break; |
|
|
584 | |
|
|
585 | default: |
|
|
586 | --dec->cur; |
|
|
587 | ERR ("illegal backslash escape sequence in string"); |
|
|
588 | } |
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 | |
589 | } |
665 | } |
590 | else if (ch >= 0x20 && ch <= 0x7f) |
666 | while (cur < buf + SHORT_STRING_LEN); |
591 | APPEND_CH (*dec->cur++); |
667 | |
592 | else if (ch >= 0x80) |
668 | STRLEN len = cur - buf; |
|
|
669 | |
|
|
670 | if (sv) |
593 | { |
671 | { |
594 | STRLEN clen; |
672 | SvGROW (sv, SvCUR (sv) + len + 1); |
595 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
673 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
596 | if (clen == (STRLEN)-1) |
674 | SvCUR_set (sv, SvCUR (sv) + len); |
597 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
598 | |
|
|
599 | APPEND_GROW (clen); |
|
|
600 | do |
|
|
601 | { |
|
|
602 | *cur++ = *dec->cur++; |
|
|
603 | } |
|
|
604 | while (--clen); |
|
|
605 | |
|
|
606 | utf8 = 1; |
|
|
607 | } |
675 | } |
608 | else if (dec->cur == dec->end) |
|
|
609 | ERR ("unexpected end of string while parsing json string"); |
|
|
610 | else |
676 | else |
611 | ERR ("invalid character encountered"); |
677 | sv = newSVpvn (buf, len); |
612 | } |
678 | } |
|
|
679 | while (*dec->cur != '"'); |
613 | |
680 | |
614 | ++dec->cur; |
681 | ++dec->cur; |
615 | |
682 | |
616 | SvCUR_set (sv, cur - SvPVX (sv)); |
683 | if (sv) |
617 | |
684 | { |
618 | SvPOK_only (sv); |
685 | SvPOK_only (sv); |
619 | *SvEND (sv) = 0; |
686 | *SvEND (sv) = 0; |
620 | |
687 | |
621 | if (utf8) |
688 | if (utf8) |
622 | SvUTF8_on (sv); |
689 | SvUTF8_on (sv); |
623 | |
690 | } |
624 | if (dec->flags & F_SHRINK) |
691 | else |
625 | shrink (sv); |
692 | sv = newSVpvn ("", 0); |
626 | |
693 | |
627 | return sv; |
694 | return sv; |
628 | |
695 | |
629 | fail: |
696 | fail: |
630 | SvREFCNT_dec (sv); |
|
|
631 | return 0; |
697 | return 0; |
632 | } |
698 | } |
633 | |
699 | |
634 | static SV * |
700 | static SV * |
635 | decode_num (dec_t *dec) |
701 | decode_num (dec_t *dec) |
… | |
… | |
716 | static SV * |
782 | static SV * |
717 | decode_av (dec_t *dec) |
783 | decode_av (dec_t *dec) |
718 | { |
784 | { |
719 | AV *av = newAV (); |
785 | AV *av = newAV (); |
720 | |
786 | |
721 | WS; |
787 | decode_ws (dec); |
722 | if (*dec->cur == ']') |
788 | if (*dec->cur == ']') |
723 | ++dec->cur; |
789 | ++dec->cur; |
724 | else |
790 | else |
725 | for (;;) |
791 | for (;;) |
726 | { |
792 | { |
… | |
… | |
730 | if (!value) |
796 | if (!value) |
731 | goto fail; |
797 | goto fail; |
732 | |
798 | |
733 | av_push (av, value); |
799 | av_push (av, value); |
734 | |
800 | |
735 | WS; |
801 | decode_ws (dec); |
736 | |
802 | |
737 | if (*dec->cur == ']') |
803 | if (*dec->cur == ']') |
738 | { |
804 | { |
739 | ++dec->cur; |
805 | ++dec->cur; |
740 | break; |
806 | break; |
… | |
… | |
756 | static SV * |
822 | static SV * |
757 | decode_hv (dec_t *dec) |
823 | decode_hv (dec_t *dec) |
758 | { |
824 | { |
759 | HV *hv = newHV (); |
825 | HV *hv = newHV (); |
760 | |
826 | |
761 | WS; |
827 | decode_ws (dec); |
762 | if (*dec->cur == '}') |
828 | if (*dec->cur == '}') |
763 | ++dec->cur; |
829 | ++dec->cur; |
764 | else |
830 | else |
765 | for (;;) |
831 | for (;;) |
766 | { |
832 | { |
767 | SV *key, *value; |
833 | SV *key, *value; |
768 | |
834 | |
769 | WS; EXPECT_CH ('"'); |
835 | decode_ws (dec); EXPECT_CH ('"'); |
770 | |
836 | |
771 | key = decode_str (dec); |
837 | key = decode_str (dec); |
772 | if (!key) |
838 | if (!key) |
773 | goto fail; |
839 | goto fail; |
774 | |
840 | |
775 | WS; EXPECT_CH (':'); |
841 | decode_ws (dec); EXPECT_CH (':'); |
776 | |
842 | |
777 | value = decode_sv (dec); |
843 | value = decode_sv (dec); |
778 | if (!value) |
844 | if (!value) |
779 | { |
845 | { |
780 | SvREFCNT_dec (key); |
846 | SvREFCNT_dec (key); |
… | |
… | |
782 | } |
848 | } |
783 | |
849 | |
784 | //TODO: optimise |
850 | //TODO: optimise |
785 | hv_store_ent (hv, key, value, 0); |
851 | hv_store_ent (hv, key, value, 0); |
786 | |
852 | |
787 | WS; |
853 | decode_ws (dec); |
788 | |
854 | |
789 | if (*dec->cur == '}') |
855 | if (*dec->cur == '}') |
790 | { |
856 | { |
791 | ++dec->cur; |
857 | ++dec->cur; |
792 | break; |
858 | break; |
… | |
… | |
806 | } |
872 | } |
807 | |
873 | |
808 | static SV * |
874 | static SV * |
809 | decode_sv (dec_t *dec) |
875 | decode_sv (dec_t *dec) |
810 | { |
876 | { |
811 | WS; |
877 | decode_ws (dec); |
812 | switch (*dec->cur) |
878 | switch (*dec->cur) |
813 | { |
879 | { |
814 | case '"': ++dec->cur; return decode_str (dec); |
880 | case '"': ++dec->cur; return decode_str (dec); |
815 | case '[': ++dec->cur; return decode_av (dec); |
881 | case '[': ++dec->cur; return decode_av (dec); |
816 | case '{': ++dec->cur; return decode_hv (dec); |
882 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
909 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
975 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
910 | croak ("JSON text must be an object or array (but found number, string, true, false or null, 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)"); |
911 | |
977 | |
912 | return sv; |
978 | return sv; |
913 | } |
979 | } |
|
|
980 | |
|
|
981 | ///////////////////////////////////////////////////////////////////////////// |
|
|
982 | // XS interface functions |
914 | |
983 | |
915 | MODULE = JSON::XS PACKAGE = JSON::XS |
984 | MODULE = JSON::XS PACKAGE = JSON::XS |
916 | |
985 | |
917 | BOOT: |
986 | BOOT: |
918 | { |
987 | { |
… | |
… | |
972 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1041 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
973 | |
1042 | |
974 | PROTOTYPES: ENABLE |
1043 | PROTOTYPES: ENABLE |
975 | |
1044 | |
976 | void to_json (SV *scalar) |
1045 | void to_json (SV *scalar) |
|
|
1046 | ALIAS: |
|
|
1047 | objToJson = 0 |
977 | PPCODE: |
1048 | PPCODE: |
978 | XPUSHs (encode_json (scalar, F_UTF8)); |
1049 | XPUSHs (encode_json (scalar, F_UTF8)); |
979 | |
1050 | |
980 | void from_json (SV *jsonstr) |
1051 | void from_json (SV *jsonstr) |
|
|
1052 | ALIAS: |
|
|
1053 | jsonToObj = 0 |
981 | PPCODE: |
1054 | PPCODE: |
982 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1055 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
983 | |
1056 | |