… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include "assert.h" |
6 | #include "string.h" |
6 | #include "string.h" |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
|
|
8 | #include "stdio.h" |
8 | |
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
|
|
13 | |
9 | #define F_ASCII 0x00000001 |
14 | #define F_ASCII 0x00000001UL |
|
|
15 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002 |
16 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004 |
17 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008 |
18 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
19 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020 |
20 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
21 | #define F_ALLOW_NONREF 0x00000100UL |
16 | #define F_SHRINK 0x00000100 |
22 | #define F_SHRINK 0x00000200UL |
|
|
23 | #define F_MAXDEPTH 0xf8000000UL |
|
|
24 | #define S_MAXDEPTH 27 |
|
|
25 | |
|
|
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
27 | |
|
|
28 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
29 | // F_BLESSED? <=> { $__class__$ => } |
17 | |
30 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
20 | |
33 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | #define INDENT_STEP 3 // spaces per indentation level |
35 | #define INDENT_STEP 3 // spaces per indentation level |
23 | |
36 | |
24 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
25 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
26 | |
38 | |
27 | #define SB do { |
39 | #define SB do { |
28 | #define SE } while (0) |
40 | #define SE } while (0) |
29 | |
41 | |
… | |
… | |
69 | { |
81 | { |
70 | *clen = 2; |
82 | *clen = 2; |
71 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
83 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
72 | } |
84 | } |
73 | else |
85 | else |
|
|
86 | { |
|
|
87 | *clen = (STRLEN)-1; |
74 | return (UV)-1; |
88 | return (UV)-1; |
|
|
89 | } |
75 | } |
90 | } |
76 | |
91 | |
77 | ///////////////////////////////////////////////////////////////////////////// |
92 | ///////////////////////////////////////////////////////////////////////////// |
78 | // encoder |
93 | // encoder |
79 | |
94 | |
… | |
… | |
81 | typedef struct |
96 | typedef struct |
82 | { |
97 | { |
83 | char *cur; // SvPVX (sv) + current output position |
98 | char *cur; // SvPVX (sv) + current output position |
84 | char *end; // SvEND (sv) |
99 | char *end; // SvEND (sv) |
85 | SV *sv; // result scalar |
100 | SV *sv; // result scalar |
86 | UV flags; // F_* |
101 | U32 flags; // F_* |
87 | int indent; // indentation level |
102 | U32 indent; // indentation level |
88 | int max_depth; // max. recursion level |
103 | U32 maxdepth; // max. indentation/recursion level |
89 | } enc_t; |
104 | } enc_t; |
90 | |
105 | |
91 | static void |
106 | static void |
92 | need (enc_t *enc, STRLEN len) |
107 | need (enc_t *enc, STRLEN len) |
93 | { |
108 | { |
94 | if (enc->cur + len >= enc->end) |
109 | if (enc->cur + len >= enc->end) |
95 | { |
110 | { |
96 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
111 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
97 | SvGROW (enc->sv, cur + len + 1); |
112 | SvGROW (enc->sv, cur + len + 1); |
98 | enc->cur = SvPVX (enc->sv) + cur; |
113 | enc->cur = SvPVX (enc->sv) + cur; |
99 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
114 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
100 | } |
115 | } |
101 | } |
116 | } |
102 | |
117 | |
103 | static void |
118 | static void |
104 | encode_ch (enc_t *enc, char ch) |
119 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
166 | } |
181 | } |
167 | |
182 | |
168 | if (uch > 0x10FFFFUL) |
183 | if (uch > 0x10FFFFUL) |
169 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
184 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
170 | |
185 | |
171 | if (uch < 0x80 || enc->flags & F_ASCII) |
186 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
172 | { |
187 | { |
173 | if (uch > 0xFFFFUL) |
188 | if (uch > 0xFFFFUL) |
174 | { |
189 | { |
175 | need (enc, len += 11); |
190 | need (enc, len += 11); |
176 | sprintf (enc->cur, "\\u%04x\\u%04x", |
191 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
190 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
205 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
191 | } |
206 | } |
192 | |
207 | |
193 | str += clen; |
208 | str += clen; |
194 | } |
209 | } |
|
|
210 | else if (enc->flags & F_LATIN1) |
|
|
211 | { |
|
|
212 | *enc->cur++ = uch; |
|
|
213 | str += clen; |
|
|
214 | } |
195 | else if (is_utf8) |
215 | else if (is_utf8) |
196 | { |
216 | { |
197 | need (enc, len += clen); |
217 | need (enc, len += clen); |
198 | do |
218 | do |
199 | { |
219 | { |
… | |
… | |
201 | } |
221 | } |
202 | while (--clen); |
222 | while (--clen); |
203 | } |
223 | } |
204 | else |
224 | else |
205 | { |
225 | { |
206 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
226 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
207 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
227 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
208 | ++str; |
228 | ++str; |
209 | } |
229 | } |
210 | } |
230 | } |
211 | } |
231 | } |
… | |
… | |
261 | static void |
281 | static void |
262 | encode_av (enc_t *enc, AV *av) |
282 | encode_av (enc_t *enc, AV *av) |
263 | { |
283 | { |
264 | int i, len = av_len (av); |
284 | int i, len = av_len (av); |
265 | |
285 | |
|
|
286 | if (enc->indent >= enc->maxdepth) |
|
|
287 | croak ("data structure too deep (hit recursion limit)"); |
|
|
288 | |
266 | encode_ch (enc, '['); encode_nl (enc); |
289 | encode_ch (enc, '['); encode_nl (enc); |
267 | ++enc->indent; |
290 | ++enc->indent; |
268 | |
291 | |
269 | for (i = 0; i <= len; ++i) |
292 | for (i = 0; i <= len; ++i) |
270 | { |
293 | { |
… | |
… | |
336 | static void |
359 | static void |
337 | encode_hv (enc_t *enc, HV *hv) |
360 | encode_hv (enc_t *enc, HV *hv) |
338 | { |
361 | { |
339 | int count, i; |
362 | int count, i; |
340 | |
363 | |
|
|
364 | if (enc->indent >= enc->maxdepth) |
|
|
365 | croak ("data structure too deep (hit recursion limit)"); |
|
|
366 | |
341 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
367 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
342 | |
368 | |
343 | if ((count = hv_iterinit (hv))) |
369 | if ((count = hv_iterinit (hv))) |
344 | { |
370 | { |
345 | // for canonical output we have to sort by keys first |
371 | // for canonical output we have to sort by keys first |
346 | // actually, this is mostly due to the stupid so-called |
372 | // actually, this is mostly due to the stupid so-called |
347 | // security workaround added somewhere in 5.8.x. |
373 | // security workaround added somewhere in 5.8.x. |
348 | // that randomises hash orderings |
374 | // that randomises hash orderings |
349 | if (enc->flags & F_CANONICAL) |
375 | if (enc->flags & F_CANONICAL) |
350 | { |
376 | { |
351 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
352 | int fast = 1; |
377 | int fast = 1; |
|
|
378 | HE *he; |
|
|
379 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
380 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
381 | #else |
|
|
382 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
383 | #endif |
353 | |
384 | |
354 | i = 0; |
385 | i = 0; |
355 | while ((he = hv_iternext (hv))) |
386 | while ((he = hv_iternext (hv))) |
356 | { |
387 | { |
357 | hes [i++] = he; |
388 | hes [i++] = he; |
… | |
… | |
392 | |
423 | |
393 | encode_nl (enc); |
424 | encode_nl (enc); |
394 | } |
425 | } |
395 | else |
426 | else |
396 | { |
427 | { |
397 | SV *sv; |
|
|
398 | HE *he = hv_iternext (hv); |
428 | HE *he = hv_iternext (hv); |
399 | |
429 | |
400 | for (;;) |
430 | for (;;) |
401 | { |
431 | { |
402 | encode_indent (enc); |
432 | encode_indent (enc); |
… | |
… | |
411 | encode_nl (enc); |
441 | encode_nl (enc); |
412 | } |
442 | } |
413 | } |
443 | } |
414 | |
444 | |
415 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
445 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
446 | } |
|
|
447 | |
|
|
448 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
449 | static void |
|
|
450 | encode_rv (enc_t *enc, SV *sv) |
|
|
451 | { |
|
|
452 | svtype svt; |
|
|
453 | |
|
|
454 | SvGETMAGIC (sv); |
|
|
455 | svt = SvTYPE (sv); |
|
|
456 | |
|
|
457 | if (svt == SVt_PVHV) |
|
|
458 | encode_hv (enc, (HV *)sv); |
|
|
459 | else if (svt == SVt_PVAV) |
|
|
460 | encode_av (enc, (AV *)sv); |
|
|
461 | else if (svt < SVt_PVAV) |
|
|
462 | { |
|
|
463 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
464 | encode_str (enc, "false", 5, 0); |
|
|
465 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
466 | encode_str (enc, "true", 4, 0); |
|
|
467 | else |
|
|
468 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
469 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
470 | } |
|
|
471 | else |
|
|
472 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
473 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
416 | } |
474 | } |
417 | |
475 | |
418 | static void |
476 | static void |
419 | encode_sv (enc_t *enc, SV *sv) |
477 | encode_sv (enc_t *enc, SV *sv) |
420 | { |
478 | { |
… | |
… | |
441 | SvIsUV(sv) |
499 | SvIsUV(sv) |
442 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
500 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
443 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
501 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
444 | } |
502 | } |
445 | else if (SvROK (sv)) |
503 | else if (SvROK (sv)) |
446 | { |
504 | encode_rv (enc, SvRV (sv)); |
447 | SV *rv = SvRV (sv); |
|
|
448 | |
|
|
449 | if (enc->indent >= enc->max_depth) |
|
|
450 | croak ("data structure too deep (hit recursion limit)"); |
|
|
451 | |
|
|
452 | switch (SvTYPE (rv)) |
|
|
453 | { |
|
|
454 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
455 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
456 | |
|
|
457 | default: |
|
|
458 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
459 | SvPV_nolen (sv)); |
|
|
460 | } |
|
|
461 | } |
|
|
462 | else if (!SvOK (sv)) |
505 | else if (!SvOK (sv)) |
463 | encode_str (enc, "null", 4, 0); |
506 | encode_str (enc, "null", 4, 0); |
464 | else |
507 | else |
465 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
508 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
466 | SvPV_nolen (sv), SvFLAGS (sv)); |
509 | SvPV_nolen (sv), SvFLAGS (sv)); |
467 | } |
510 | } |
468 | |
511 | |
469 | static SV * |
512 | static SV * |
470 | encode_json (SV *scalar, UV flags) |
513 | encode_json (SV *scalar, U32 flags) |
471 | { |
514 | { |
|
|
515 | enc_t enc; |
|
|
516 | |
472 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
517 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
473 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
518 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
474 | |
519 | |
475 | enc_t enc; |
|
|
476 | enc.flags = flags; |
520 | enc.flags = flags; |
477 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
521 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
478 | enc.cur = SvPVX (enc.sv); |
522 | enc.cur = SvPVX (enc.sv); |
479 | enc.end = SvEND (enc.sv); |
523 | enc.end = SvEND (enc.sv); |
480 | enc.indent = 0; |
524 | enc.indent = 0; |
481 | enc.max_depth = 0x7fffffffUL; |
525 | enc.maxdepth = DEC_DEPTH (flags); |
482 | |
526 | |
483 | SvPOK_only (enc.sv); |
527 | SvPOK_only (enc.sv); |
484 | encode_sv (&enc, scalar); |
528 | encode_sv (&enc, scalar); |
485 | |
529 | |
|
|
530 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
531 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
532 | |
486 | if (!(flags & (F_ASCII | F_UTF8))) |
533 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
487 | SvUTF8_on (enc.sv); |
534 | SvUTF8_on (enc.sv); |
488 | |
|
|
489 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
490 | |
535 | |
491 | if (enc.flags & F_SHRINK) |
536 | if (enc.flags & F_SHRINK) |
492 | shrink (enc.sv); |
537 | shrink (enc.sv); |
493 | |
538 | |
494 | return enc.sv; |
539 | return enc.sv; |
… | |
… | |
501 | typedef struct |
546 | typedef struct |
502 | { |
547 | { |
503 | char *cur; // current parser pointer |
548 | char *cur; // current parser pointer |
504 | char *end; // end of input string |
549 | char *end; // end of input string |
505 | const char *err; // parse error, if != 0 |
550 | const char *err; // parse error, if != 0 |
506 | UV flags; // F_* |
551 | U32 flags; // F_* |
|
|
552 | U32 depth; // recursion depth |
|
|
553 | U32 maxdepth; // recursion depth limit |
507 | } dec_t; |
554 | } dec_t; |
508 | |
555 | |
509 | static void |
556 | static void |
510 | decode_ws (dec_t *dec) |
557 | decode_ws (dec_t *dec) |
511 | { |
558 | { |
… | |
… | |
520 | ++dec->cur; |
567 | ++dec->cur; |
521 | } |
568 | } |
522 | } |
569 | } |
523 | |
570 | |
524 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
571 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
572 | |
525 | #define EXPECT_CH(ch) SB \ |
573 | #define EXPECT_CH(ch) SB \ |
526 | if (*dec->cur != ch) \ |
574 | if (*dec->cur != ch) \ |
527 | ERR (# ch " expected"); \ |
575 | ERR (# ch " expected"); \ |
528 | ++dec->cur; \ |
576 | ++dec->cur; \ |
529 | SE |
577 | SE |
530 | |
578 | |
|
|
579 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
580 | #define DEC_DEC_DEPTH --dec->depth |
|
|
581 | |
531 | static SV *decode_sv (dec_t *dec); |
582 | static SV *decode_sv (dec_t *dec); |
532 | |
583 | |
533 | static signed char decode_hexdigit[256]; |
584 | static signed char decode_hexdigit[256]; |
534 | |
585 | |
535 | static UV |
586 | static UV |
… | |
… | |
560 | SV *sv = 0; |
611 | SV *sv = 0; |
561 | int utf8 = 0; |
612 | int utf8 = 0; |
562 | |
613 | |
563 | do |
614 | do |
564 | { |
615 | { |
565 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
616 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
566 | char *cur = buf; |
617 | char *cur = buf; |
567 | |
618 | |
568 | do |
619 | do |
569 | { |
620 | { |
570 | unsigned char ch = *(unsigned char *)dec->cur++; |
621 | unsigned char ch = *(unsigned char *)dec->cur++; |
… | |
… | |
636 | } |
687 | } |
637 | else if (ch >= 0x20 && ch <= 0x7f) |
688 | else if (ch >= 0x20 && ch <= 0x7f) |
638 | *cur++ = ch; |
689 | *cur++ = ch; |
639 | else if (ch >= 0x80) |
690 | else if (ch >= 0x80) |
640 | { |
691 | { |
|
|
692 | STRLEN clen; |
|
|
693 | UV uch; |
|
|
694 | |
641 | --dec->cur; |
695 | --dec->cur; |
642 | |
696 | |
643 | STRLEN clen; |
|
|
644 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
697 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
645 | if (clen == (STRLEN)-1) |
698 | if (clen == (STRLEN)-1) |
646 | ERR ("malformed UTF-8 character in JSON string"); |
699 | ERR ("malformed UTF-8 character in JSON string"); |
647 | |
700 | |
648 | do |
701 | do |
649 | { |
|
|
650 | *cur++ = *dec->cur++; |
702 | *cur++ = *dec->cur++; |
651 | } |
|
|
652 | while (--clen); |
703 | while (--clen); |
653 | |
704 | |
654 | utf8 = 1; |
705 | utf8 = 1; |
655 | } |
706 | } |
656 | else if (!ch) |
|
|
657 | ERR ("unexpected end of string while parsing json string"); |
|
|
658 | else |
707 | else |
|
|
708 | { |
|
|
709 | --dec->cur; |
|
|
710 | |
|
|
711 | if (!ch) |
|
|
712 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
713 | else |
659 | ERR ("invalid character encountered"); |
714 | ERR ("invalid character encountered while parsing JSON string"); |
660 | |
715 | } |
661 | } |
716 | } |
662 | while (cur < buf + SHORT_STRING_LEN); |
717 | while (cur < buf + SHORT_STRING_LEN); |
663 | |
718 | |
|
|
719 | { |
664 | STRLEN len = cur - buf; |
720 | STRLEN len = cur - buf; |
665 | |
721 | |
666 | if (sv) |
722 | if (sv) |
667 | { |
723 | { |
668 | SvGROW (sv, SvCUR (sv) + len + 1); |
724 | SvGROW (sv, SvCUR (sv) + len + 1); |
669 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
725 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
670 | SvCUR_set (sv, SvCUR (sv) + len); |
726 | SvCUR_set (sv, SvCUR (sv) + len); |
671 | } |
727 | } |
672 | else |
728 | else |
673 | sv = newSVpvn (buf, len); |
729 | sv = newSVpvn (buf, len); |
|
|
730 | } |
674 | } |
731 | } |
675 | while (*dec->cur != '"'); |
732 | while (*dec->cur != '"'); |
676 | |
733 | |
677 | ++dec->cur; |
734 | ++dec->cur; |
678 | |
735 | |
… | |
… | |
778 | static SV * |
835 | static SV * |
779 | decode_av (dec_t *dec) |
836 | decode_av (dec_t *dec) |
780 | { |
837 | { |
781 | AV *av = newAV (); |
838 | AV *av = newAV (); |
782 | |
839 | |
|
|
840 | DEC_INC_DEPTH; |
783 | decode_ws (dec); |
841 | decode_ws (dec); |
|
|
842 | |
784 | if (*dec->cur == ']') |
843 | if (*dec->cur == ']') |
785 | ++dec->cur; |
844 | ++dec->cur; |
786 | else |
845 | else |
787 | for (;;) |
846 | for (;;) |
788 | { |
847 | { |
… | |
… | |
806 | ERR (", or ] expected while parsing array"); |
865 | ERR (", or ] expected while parsing array"); |
807 | |
866 | |
808 | ++dec->cur; |
867 | ++dec->cur; |
809 | } |
868 | } |
810 | |
869 | |
|
|
870 | DEC_DEC_DEPTH; |
811 | return newRV_noinc ((SV *)av); |
871 | return newRV_noinc ((SV *)av); |
812 | |
872 | |
813 | fail: |
873 | fail: |
814 | SvREFCNT_dec (av); |
874 | SvREFCNT_dec (av); |
|
|
875 | DEC_DEC_DEPTH; |
815 | return 0; |
876 | return 0; |
816 | } |
877 | } |
817 | |
878 | |
818 | static SV * |
879 | static SV * |
819 | decode_hv (dec_t *dec) |
880 | decode_hv (dec_t *dec) |
820 | { |
881 | { |
821 | HV *hv = newHV (); |
882 | HV *hv = newHV (); |
822 | |
883 | |
|
|
884 | DEC_INC_DEPTH; |
823 | decode_ws (dec); |
885 | decode_ws (dec); |
|
|
886 | |
824 | if (*dec->cur == '}') |
887 | if (*dec->cur == '}') |
825 | ++dec->cur; |
888 | ++dec->cur; |
826 | else |
889 | else |
827 | for (;;) |
890 | for (;;) |
828 | { |
891 | { |
… | |
… | |
841 | { |
904 | { |
842 | SvREFCNT_dec (key); |
905 | SvREFCNT_dec (key); |
843 | goto fail; |
906 | goto fail; |
844 | } |
907 | } |
845 | |
908 | |
846 | //TODO: optimise |
|
|
847 | hv_store_ent (hv, key, value, 0); |
909 | hv_store_ent (hv, key, value, 0); |
|
|
910 | SvREFCNT_dec (key); |
848 | |
911 | |
849 | decode_ws (dec); |
912 | decode_ws (dec); |
850 | |
913 | |
851 | if (*dec->cur == '}') |
914 | if (*dec->cur == '}') |
852 | { |
915 | { |
… | |
… | |
858 | ERR (", or } expected while parsing object/hash"); |
921 | ERR (", or } expected while parsing object/hash"); |
859 | |
922 | |
860 | ++dec->cur; |
923 | ++dec->cur; |
861 | } |
924 | } |
862 | |
925 | |
|
|
926 | DEC_DEC_DEPTH; |
863 | return newRV_noinc ((SV *)hv); |
927 | return newRV_noinc ((SV *)hv); |
864 | |
928 | |
865 | fail: |
929 | fail: |
866 | SvREFCNT_dec (hv); |
930 | SvREFCNT_dec (hv); |
|
|
931 | DEC_DEC_DEPTH; |
867 | return 0; |
932 | return 0; |
868 | } |
933 | } |
869 | |
934 | |
870 | static SV * |
935 | static SV * |
871 | decode_sv (dec_t *dec) |
936 | decode_sv (dec_t *dec) |
… | |
… | |
914 | ERR ("'null' expected"); |
979 | ERR ("'null' expected"); |
915 | |
980 | |
916 | break; |
981 | break; |
917 | |
982 | |
918 | default: |
983 | default: |
919 | ERR ("malformed json string, neither array, object, number, string or atom"); |
984 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
920 | break; |
985 | break; |
921 | } |
986 | } |
922 | |
987 | |
923 | fail: |
988 | fail: |
924 | return 0; |
989 | return 0; |
925 | } |
990 | } |
926 | |
991 | |
927 | static SV * |
992 | static SV * |
928 | decode_json (SV *string, UV flags) |
993 | decode_json (SV *string, U32 flags, UV *offset_return) |
929 | { |
994 | { |
|
|
995 | dec_t dec; |
|
|
996 | UV offset; |
930 | SV *sv; |
997 | SV *sv; |
|
|
998 | |
|
|
999 | SvGETMAGIC (string); |
|
|
1000 | SvUPGRADE (string, SVt_PV); |
931 | |
1001 | |
932 | if (flags & F_UTF8) |
1002 | if (flags & F_UTF8) |
933 | sv_utf8_downgrade (string, 0); |
1003 | sv_utf8_downgrade (string, 0); |
934 | else |
1004 | else |
935 | sv_utf8_upgrade (string); |
1005 | sv_utf8_upgrade (string); |
936 | |
1006 | |
937 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1007 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
938 | |
1008 | |
939 | dec_t dec; |
|
|
940 | dec.flags = flags; |
1009 | dec.flags = flags; |
941 | dec.cur = SvPVX (string); |
1010 | dec.cur = SvPVX (string); |
942 | dec.end = SvEND (string); |
1011 | dec.end = SvEND (string); |
943 | dec.err = 0; |
1012 | dec.err = 0; |
|
|
1013 | dec.depth = 0; |
|
|
1014 | dec.maxdepth = DEC_DEPTH (dec.flags); |
944 | |
1015 | |
|
|
1016 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
945 | sv = decode_sv (&dec); |
1017 | sv = decode_sv (&dec); |
946 | |
1018 | |
|
|
1019 | if (!(offset_return || !sv)) |
|
|
1020 | { |
|
|
1021 | // check for trailing garbage |
|
|
1022 | decode_ws (&dec); |
|
|
1023 | |
|
|
1024 | if (*dec.cur) |
|
|
1025 | { |
|
|
1026 | dec.err = "garbage after JSON object"; |
|
|
1027 | SvREFCNT_dec (sv); |
|
|
1028 | sv = 0; |
|
|
1029 | } |
|
|
1030 | } |
|
|
1031 | |
|
|
1032 | if (offset_return || !sv) |
|
|
1033 | { |
|
|
1034 | offset = dec.flags & F_UTF8 |
|
|
1035 | ? dec.cur - SvPVX (string) |
|
|
1036 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1037 | |
|
|
1038 | if (offset_return) |
|
|
1039 | *offset_return = offset; |
|
|
1040 | } |
|
|
1041 | |
947 | if (!sv) |
1042 | if (!sv) |
948 | { |
1043 | { |
949 | IV offset = dec.flags & F_UTF8 |
|
|
950 | ? dec.cur - SvPVX (string) |
|
|
951 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
952 | SV *uni = sv_newmortal (); |
1044 | SV *uni = sv_newmortal (); |
953 | |
1045 | |
954 | // horrible hack to silence warning inside pv_uni_display |
1046 | // horrible hack to silence warning inside pv_uni_display |
955 | COP cop = *PL_curcop; |
1047 | COP cop = *PL_curcop; |
956 | cop.cop_warnings = pWARN_NONE; |
1048 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
958 | SAVEVPTR (PL_curcop); |
1050 | SAVEVPTR (PL_curcop); |
959 | PL_curcop = &cop; |
1051 | PL_curcop = &cop; |
960 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1052 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
961 | LEAVE; |
1053 | LEAVE; |
962 | |
1054 | |
963 | croak ("%s, at character offset %d (%s)", |
1055 | croak ("%s, at character offset %d [\"%s\"]", |
964 | dec.err, |
1056 | dec.err, |
965 | (int)offset, |
1057 | (int)offset, |
966 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1058 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
967 | } |
1059 | } |
968 | |
1060 | |
… | |
… | |
982 | BOOT: |
1074 | BOOT: |
983 | { |
1075 | { |
984 | int i; |
1076 | int i; |
985 | |
1077 | |
986 | memset (decode_hexdigit, 0xff, 256); |
1078 | memset (decode_hexdigit, 0xff, 256); |
|
|
1079 | |
987 | for (i = 10; i--; ) |
1080 | for (i = 0; i < 256; ++i) |
988 | decode_hexdigit ['0' + i] = i; |
1081 | decode_hexdigit [i] = |
989 | |
1082 | i >= '0' && i <= '9' ? i - '0' |
990 | for (i = 7; i--; ) |
1083 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
991 | { |
1084 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
992 | decode_hexdigit ['a' + i] = 10 + i; |
1085 | : -1; |
993 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
994 | } |
|
|
995 | |
1086 | |
996 | json_stash = gv_stashpv ("JSON::XS", 1); |
1087 | json_stash = gv_stashpv ("JSON::XS", 1); |
997 | } |
1088 | } |
998 | |
1089 | |
999 | PROTOTYPES: DISABLE |
1090 | PROTOTYPES: DISABLE |
… | |
… | |
1005 | RETVAL |
1096 | RETVAL |
1006 | |
1097 | |
1007 | SV *ascii (SV *self, int enable = 1) |
1098 | SV *ascii (SV *self, int enable = 1) |
1008 | ALIAS: |
1099 | ALIAS: |
1009 | ascii = F_ASCII |
1100 | ascii = F_ASCII |
|
|
1101 | latin1 = F_LATIN1 |
1010 | utf8 = F_UTF8 |
1102 | utf8 = F_UTF8 |
1011 | indent = F_INDENT |
1103 | indent = F_INDENT |
1012 | canonical = F_CANONICAL |
1104 | canonical = F_CANONICAL |
1013 | space_before = F_SPACE_BEFORE |
1105 | space_before = F_SPACE_BEFORE |
1014 | space_after = F_SPACE_AFTER |
1106 | space_after = F_SPACE_AFTER |
… | |
… | |
1026 | RETVAL = newSVsv (self); |
1118 | RETVAL = newSVsv (self); |
1027 | } |
1119 | } |
1028 | OUTPUT: |
1120 | OUTPUT: |
1029 | RETVAL |
1121 | RETVAL |
1030 | |
1122 | |
|
|
1123 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1124 | CODE: |
|
|
1125 | { |
|
|
1126 | UV *uv = SvJSON (self); |
|
|
1127 | UV log2 = 0; |
|
|
1128 | |
|
|
1129 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1130 | |
|
|
1131 | while ((1UL << log2) < max_depth) |
|
|
1132 | ++log2; |
|
|
1133 | |
|
|
1134 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1135 | |
|
|
1136 | RETVAL = newSVsv (self); |
|
|
1137 | } |
|
|
1138 | OUTPUT: |
|
|
1139 | RETVAL |
|
|
1140 | |
1031 | void encode (SV *self, SV *scalar) |
1141 | void encode (SV *self, SV *scalar) |
1032 | PPCODE: |
1142 | PPCODE: |
1033 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1143 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1034 | |
1144 | |
1035 | void decode (SV *self, SV *jsonstr) |
1145 | void decode (SV *self, SV *jsonstr) |
1036 | PPCODE: |
1146 | PPCODE: |
1037 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1147 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1148 | |
|
|
1149 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1150 | PPCODE: |
|
|
1151 | { |
|
|
1152 | UV offset; |
|
|
1153 | EXTEND (SP, 2); |
|
|
1154 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1155 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1156 | } |
1038 | |
1157 | |
1039 | PROTOTYPES: ENABLE |
1158 | PROTOTYPES: ENABLE |
1040 | |
1159 | |
1041 | void to_json (SV *scalar) |
1160 | void to_json (SV *scalar) |
1042 | ALIAS: |
1161 | ALIAS: |
1043 | objToJson = 0 |
1162 | objToJson = 0 |
1044 | PPCODE: |
1163 | PPCODE: |
1045 | XPUSHs (encode_json (scalar, F_UTF8)); |
1164 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1046 | |
1165 | |
1047 | void from_json (SV *jsonstr) |
1166 | void from_json (SV *jsonstr) |
1048 | ALIAS: |
1167 | ALIAS: |
1049 | jsonToObj = 0 |
1168 | jsonToObj = 0 |
1050 | PPCODE: |
1169 | PPCODE: |
1051 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1170 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1052 | |
1171 | |