… | |
… | |
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 |
10 | #define F_UTF8 0x00000002 |
15 | #define F_UTF8 0x00000002UL |
11 | #define F_INDENT 0x00000004 |
16 | #define F_INDENT 0x00000004UL |
12 | #define F_CANONICAL 0x00000008 |
17 | #define F_CANONICAL 0x00000008UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
18 | #define F_SPACE_BEFORE 0x00000010UL |
14 | #define F_SPACE_AFTER 0x00000020 |
19 | #define F_SPACE_AFTER 0x00000020UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
20 | #define F_ALLOW_NONREF 0x00000080UL |
16 | #define F_SHRINK 0x00000100 |
21 | #define F_SHRINK 0x00000100UL |
|
|
22 | #define F_MAXDEPTH 0xf8000000UL |
|
|
23 | #define S_MAXDEPTH 27 |
17 | |
24 | |
18 | // F_SKIPINVALID? |
25 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
19 | // F_EXECCODEREF? |
26 | |
20 | // F_SELFCONVERT? <=> { &__class__ => } |
27 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
28 | // F_BLESSED? <=> { $__class__$ => } |
21 | |
29 | |
22 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
30 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
23 | #define F_DEFAULT 0 |
31 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
24 | |
32 | |
25 | #define INIT_SIZE 32 // initial scalar size to be allocated |
33 | #define INIT_SIZE 32 // initial scalar size to be allocated |
26 | #define INDENT_STEP 3 // spaces per indentation level |
34 | #define INDENT_STEP 3 // spaces per indentation level |
27 | |
35 | |
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 |
36 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
30 | |
37 | |
31 | #define SB do { |
38 | #define SB do { |
32 | #define SE } while (0) |
39 | #define SE } while (0) |
33 | |
40 | |
… | |
… | |
73 | { |
80 | { |
74 | *clen = 2; |
81 | *clen = 2; |
75 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
82 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
76 | } |
83 | } |
77 | else |
84 | else |
|
|
85 | { |
|
|
86 | *clen = (STRLEN)-1; |
78 | return (UV)-1; |
87 | return (UV)-1; |
|
|
88 | } |
79 | } |
89 | } |
80 | |
90 | |
81 | ///////////////////////////////////////////////////////////////////////////// |
91 | ///////////////////////////////////////////////////////////////////////////// |
82 | // encoder |
92 | // encoder |
83 | |
93 | |
… | |
… | |
85 | typedef struct |
95 | typedef struct |
86 | { |
96 | { |
87 | char *cur; // SvPVX (sv) + current output position |
97 | char *cur; // SvPVX (sv) + current output position |
88 | char *end; // SvEND (sv) |
98 | char *end; // SvEND (sv) |
89 | SV *sv; // result scalar |
99 | SV *sv; // result scalar |
90 | UV flags; // F_* |
100 | U32 flags; // F_* |
91 | int indent; // indentation level |
101 | U32 indent; // indentation level |
92 | int max_depth; // max. recursion level |
102 | U32 maxdepth; // max. indentation/recursion level |
93 | } enc_t; |
103 | } enc_t; |
94 | |
104 | |
95 | static void |
105 | static void |
96 | need (enc_t *enc, STRLEN len) |
106 | need (enc_t *enc, STRLEN len) |
97 | { |
107 | { |
98 | if (enc->cur + len >= enc->end) |
108 | if (enc->cur + len >= enc->end) |
99 | { |
109 | { |
100 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
110 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
101 | SvGROW (enc->sv, cur + len + 1); |
111 | SvGROW (enc->sv, cur + len + 1); |
102 | enc->cur = SvPVX (enc->sv) + cur; |
112 | enc->cur = SvPVX (enc->sv) + cur; |
103 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
113 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
104 | } |
114 | } |
105 | } |
115 | } |
106 | |
116 | |
107 | static void |
117 | static void |
108 | encode_ch (enc_t *enc, char ch) |
118 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
205 | } |
215 | } |
206 | while (--clen); |
216 | while (--clen); |
207 | } |
217 | } |
208 | else |
218 | else |
209 | { |
219 | { |
210 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
220 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
211 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
221 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
212 | ++str; |
222 | ++str; |
213 | } |
223 | } |
214 | } |
224 | } |
215 | } |
225 | } |
… | |
… | |
265 | static void |
275 | static void |
266 | encode_av (enc_t *enc, AV *av) |
276 | encode_av (enc_t *enc, AV *av) |
267 | { |
277 | { |
268 | int i, len = av_len (av); |
278 | int i, len = av_len (av); |
269 | |
279 | |
|
|
280 | if (enc->indent >= enc->maxdepth) |
|
|
281 | croak ("data structure too deep (hit recursion limit)"); |
|
|
282 | |
270 | encode_ch (enc, '['); encode_nl (enc); |
283 | encode_ch (enc, '['); encode_nl (enc); |
271 | ++enc->indent; |
284 | ++enc->indent; |
272 | |
285 | |
273 | for (i = 0; i <= len; ++i) |
286 | for (i = 0; i <= len; ++i) |
274 | { |
287 | { |
… | |
… | |
339 | |
352 | |
340 | static void |
353 | static void |
341 | encode_hv (enc_t *enc, HV *hv) |
354 | encode_hv (enc_t *enc, HV *hv) |
342 | { |
355 | { |
343 | int count, i; |
356 | int count, i; |
|
|
357 | |
|
|
358 | if (enc->indent >= enc->maxdepth) |
|
|
359 | croak ("data structure too deep (hit recursion limit)"); |
344 | |
360 | |
345 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
361 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
346 | |
362 | |
347 | if ((count = hv_iterinit (hv))) |
363 | if ((count = hv_iterinit (hv))) |
348 | { |
364 | { |
… | |
… | |
396 | |
412 | |
397 | encode_nl (enc); |
413 | encode_nl (enc); |
398 | } |
414 | } |
399 | else |
415 | else |
400 | { |
416 | { |
401 | SV *sv; |
|
|
402 | HE *he = hv_iternext (hv); |
417 | HE *he = hv_iternext (hv); |
403 | |
418 | |
404 | for (;;) |
419 | for (;;) |
405 | { |
420 | { |
406 | encode_indent (enc); |
421 | encode_indent (enc); |
… | |
… | |
415 | encode_nl (enc); |
430 | encode_nl (enc); |
416 | } |
431 | } |
417 | } |
432 | } |
418 | |
433 | |
419 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
434 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
435 | } |
|
|
436 | |
|
|
437 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
438 | static void |
|
|
439 | encode_rv (enc_t *enc, SV *sv) |
|
|
440 | { |
|
|
441 | svtype svt; |
|
|
442 | |
|
|
443 | SvGETMAGIC (sv); |
|
|
444 | svt = SvTYPE (sv); |
|
|
445 | |
|
|
446 | if (svt == SVt_PVHV) |
|
|
447 | encode_hv (enc, (HV *)sv); |
|
|
448 | else if (svt == SVt_PVAV) |
|
|
449 | encode_av (enc, (AV *)sv); |
|
|
450 | else if (svt < SVt_PVAV) |
|
|
451 | { |
|
|
452 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
453 | encode_str (enc, "false", 5, 0); |
|
|
454 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
455 | encode_str (enc, "true", 4, 0); |
|
|
456 | else |
|
|
457 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
458 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
459 | } |
|
|
460 | else |
|
|
461 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
462 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
420 | } |
463 | } |
421 | |
464 | |
422 | static void |
465 | static void |
423 | encode_sv (enc_t *enc, SV *sv) |
466 | encode_sv (enc_t *enc, SV *sv) |
424 | { |
467 | { |
… | |
… | |
445 | SvIsUV(sv) |
488 | SvIsUV(sv) |
446 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
489 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
447 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
490 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
448 | } |
491 | } |
449 | else if (SvROK (sv)) |
492 | else if (SvROK (sv)) |
450 | { |
493 | encode_rv (enc, SvRV (sv)); |
451 | SV *rv = SvRV (sv); |
|
|
452 | |
|
|
453 | if (enc->indent >= enc->max_depth) |
|
|
454 | croak ("data structure too deep (hit recursion limit)"); |
|
|
455 | |
|
|
456 | switch (SvTYPE (rv)) |
|
|
457 | { |
|
|
458 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
459 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
460 | |
|
|
461 | default: |
|
|
462 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
463 | SvPV_nolen (sv)); |
|
|
464 | } |
|
|
465 | } |
|
|
466 | else if (!SvOK (sv)) |
494 | else if (!SvOK (sv)) |
467 | encode_str (enc, "null", 4, 0); |
495 | encode_str (enc, "null", 4, 0); |
468 | else |
496 | else |
469 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
497 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
470 | SvPV_nolen (sv), SvFLAGS (sv)); |
498 | SvPV_nolen (sv), SvFLAGS (sv)); |
471 | } |
499 | } |
472 | |
500 | |
473 | static SV * |
501 | static SV * |
474 | encode_json (SV *scalar, UV flags) |
502 | encode_json (SV *scalar, U32 flags) |
475 | { |
503 | { |
|
|
504 | enc_t enc; |
|
|
505 | |
476 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
506 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
477 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
507 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
478 | |
508 | |
479 | enc_t enc; |
|
|
480 | enc.flags = flags; |
509 | enc.flags = flags; |
481 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
510 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
482 | enc.cur = SvPVX (enc.sv); |
511 | enc.cur = SvPVX (enc.sv); |
483 | enc.end = SvEND (enc.sv); |
512 | enc.end = SvEND (enc.sv); |
484 | enc.indent = 0; |
513 | enc.indent = 0; |
485 | enc.max_depth = 0x7fffffffUL; |
514 | enc.maxdepth = DEC_DEPTH (flags); |
486 | |
515 | |
487 | SvPOK_only (enc.sv); |
516 | SvPOK_only (enc.sv); |
488 | encode_sv (&enc, scalar); |
517 | encode_sv (&enc, scalar); |
489 | |
518 | |
490 | if (!(flags & (F_ASCII | F_UTF8))) |
519 | if (!(flags & (F_ASCII | F_UTF8))) |
491 | SvUTF8_on (enc.sv); |
520 | SvUTF8_on (enc.sv); |
492 | |
521 | |
493 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
522 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
523 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
494 | |
524 | |
495 | if (enc.flags & F_SHRINK) |
525 | if (enc.flags & F_SHRINK) |
496 | shrink (enc.sv); |
526 | shrink (enc.sv); |
497 | |
527 | |
498 | return enc.sv; |
528 | return enc.sv; |
… | |
… | |
505 | typedef struct |
535 | typedef struct |
506 | { |
536 | { |
507 | char *cur; // current parser pointer |
537 | char *cur; // current parser pointer |
508 | char *end; // end of input string |
538 | char *end; // end of input string |
509 | const char *err; // parse error, if != 0 |
539 | const char *err; // parse error, if != 0 |
510 | UV flags; // F_* |
540 | U32 flags; // F_* |
|
|
541 | U32 depth; // recursion depth |
|
|
542 | U32 maxdepth; // recursion depth limit |
511 | } dec_t; |
543 | } dec_t; |
512 | |
544 | |
513 | static void |
545 | static void |
514 | decode_ws (dec_t *dec) |
546 | decode_ws (dec_t *dec) |
515 | { |
547 | { |
… | |
… | |
524 | ++dec->cur; |
556 | ++dec->cur; |
525 | } |
557 | } |
526 | } |
558 | } |
527 | |
559 | |
528 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
560 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
561 | |
529 | #define EXPECT_CH(ch) SB \ |
562 | #define EXPECT_CH(ch) SB \ |
530 | if (*dec->cur != ch) \ |
563 | if (*dec->cur != ch) \ |
531 | ERR (# ch " expected"); \ |
564 | ERR (# ch " expected"); \ |
532 | ++dec->cur; \ |
565 | ++dec->cur; \ |
533 | SE |
566 | SE |
534 | |
567 | |
|
|
568 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
569 | #define DEC_DEC_DEPTH --dec->depth |
|
|
570 | |
535 | static SV *decode_sv (dec_t *dec); |
571 | static SV *decode_sv (dec_t *dec); |
536 | |
572 | |
537 | static signed char decode_hexdigit[256]; |
573 | static signed char decode_hexdigit[256]; |
538 | |
574 | |
539 | static UV |
575 | static UV |
… | |
… | |
564 | SV *sv = 0; |
600 | SV *sv = 0; |
565 | int utf8 = 0; |
601 | int utf8 = 0; |
566 | |
602 | |
567 | do |
603 | do |
568 | { |
604 | { |
569 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
605 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
570 | char *cur = buf; |
606 | char *cur = buf; |
571 | |
607 | |
572 | do |
608 | do |
573 | { |
609 | { |
574 | unsigned char ch = *(unsigned char *)dec->cur++; |
610 | unsigned char ch = *(unsigned char *)dec->cur++; |
… | |
… | |
640 | } |
676 | } |
641 | else if (ch >= 0x20 && ch <= 0x7f) |
677 | else if (ch >= 0x20 && ch <= 0x7f) |
642 | *cur++ = ch; |
678 | *cur++ = ch; |
643 | else if (ch >= 0x80) |
679 | else if (ch >= 0x80) |
644 | { |
680 | { |
|
|
681 | STRLEN clen; |
|
|
682 | UV uch; |
|
|
683 | |
645 | --dec->cur; |
684 | --dec->cur; |
646 | |
685 | |
647 | STRLEN clen; |
|
|
648 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
686 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
649 | if (clen == (STRLEN)-1) |
687 | if (clen == (STRLEN)-1) |
650 | ERR ("malformed UTF-8 character in JSON string"); |
688 | ERR ("malformed UTF-8 character in JSON string"); |
651 | |
689 | |
652 | do |
690 | do |
653 | { |
|
|
654 | *cur++ = *dec->cur++; |
691 | *cur++ = *dec->cur++; |
655 | } |
|
|
656 | while (--clen); |
692 | while (--clen); |
657 | |
693 | |
658 | utf8 = 1; |
694 | utf8 = 1; |
659 | } |
695 | } |
660 | else if (!ch) |
|
|
661 | ERR ("unexpected end of string while parsing json string"); |
|
|
662 | else |
696 | else |
|
|
697 | { |
|
|
698 | --dec->cur; |
|
|
699 | |
|
|
700 | if (!ch) |
|
|
701 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
702 | else |
663 | ERR ("invalid character encountered"); |
703 | ERR ("invalid character encountered while parsing JSON string"); |
664 | |
704 | } |
665 | } |
705 | } |
666 | while (cur < buf + SHORT_STRING_LEN); |
706 | while (cur < buf + SHORT_STRING_LEN); |
667 | |
707 | |
|
|
708 | { |
668 | STRLEN len = cur - buf; |
709 | STRLEN len = cur - buf; |
669 | |
710 | |
670 | if (sv) |
711 | if (sv) |
671 | { |
712 | { |
672 | SvGROW (sv, SvCUR (sv) + len + 1); |
713 | SvGROW (sv, SvCUR (sv) + len + 1); |
673 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
714 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
674 | SvCUR_set (sv, SvCUR (sv) + len); |
715 | SvCUR_set (sv, SvCUR (sv) + len); |
675 | } |
716 | } |
676 | else |
717 | else |
677 | sv = newSVpvn (buf, len); |
718 | sv = newSVpvn (buf, len); |
|
|
719 | } |
678 | } |
720 | } |
679 | while (*dec->cur != '"'); |
721 | while (*dec->cur != '"'); |
680 | |
722 | |
681 | ++dec->cur; |
723 | ++dec->cur; |
682 | |
724 | |
… | |
… | |
782 | static SV * |
824 | static SV * |
783 | decode_av (dec_t *dec) |
825 | decode_av (dec_t *dec) |
784 | { |
826 | { |
785 | AV *av = newAV (); |
827 | AV *av = newAV (); |
786 | |
828 | |
|
|
829 | DEC_INC_DEPTH; |
787 | decode_ws (dec); |
830 | decode_ws (dec); |
|
|
831 | |
788 | if (*dec->cur == ']') |
832 | if (*dec->cur == ']') |
789 | ++dec->cur; |
833 | ++dec->cur; |
790 | else |
834 | else |
791 | for (;;) |
835 | for (;;) |
792 | { |
836 | { |
… | |
… | |
810 | ERR (", or ] expected while parsing array"); |
854 | ERR (", or ] expected while parsing array"); |
811 | |
855 | |
812 | ++dec->cur; |
856 | ++dec->cur; |
813 | } |
857 | } |
814 | |
858 | |
|
|
859 | DEC_DEC_DEPTH; |
815 | return newRV_noinc ((SV *)av); |
860 | return newRV_noinc ((SV *)av); |
816 | |
861 | |
817 | fail: |
862 | fail: |
818 | SvREFCNT_dec (av); |
863 | SvREFCNT_dec (av); |
|
|
864 | DEC_DEC_DEPTH; |
819 | return 0; |
865 | return 0; |
820 | } |
866 | } |
821 | |
867 | |
822 | static SV * |
868 | static SV * |
823 | decode_hv (dec_t *dec) |
869 | decode_hv (dec_t *dec) |
824 | { |
870 | { |
825 | HV *hv = newHV (); |
871 | HV *hv = newHV (); |
826 | |
872 | |
|
|
873 | DEC_INC_DEPTH; |
827 | decode_ws (dec); |
874 | decode_ws (dec); |
|
|
875 | |
828 | if (*dec->cur == '}') |
876 | if (*dec->cur == '}') |
829 | ++dec->cur; |
877 | ++dec->cur; |
830 | else |
878 | else |
831 | for (;;) |
879 | for (;;) |
832 | { |
880 | { |
… | |
… | |
845 | { |
893 | { |
846 | SvREFCNT_dec (key); |
894 | SvREFCNT_dec (key); |
847 | goto fail; |
895 | goto fail; |
848 | } |
896 | } |
849 | |
897 | |
850 | //TODO: optimise |
|
|
851 | hv_store_ent (hv, key, value, 0); |
898 | hv_store_ent (hv, key, value, 0); |
|
|
899 | SvREFCNT_dec (key); |
852 | |
900 | |
853 | decode_ws (dec); |
901 | decode_ws (dec); |
854 | |
902 | |
855 | if (*dec->cur == '}') |
903 | if (*dec->cur == '}') |
856 | { |
904 | { |
… | |
… | |
862 | ERR (", or } expected while parsing object/hash"); |
910 | ERR (", or } expected while parsing object/hash"); |
863 | |
911 | |
864 | ++dec->cur; |
912 | ++dec->cur; |
865 | } |
913 | } |
866 | |
914 | |
|
|
915 | DEC_DEC_DEPTH; |
867 | return newRV_noinc ((SV *)hv); |
916 | return newRV_noinc ((SV *)hv); |
868 | |
917 | |
869 | fail: |
918 | fail: |
870 | SvREFCNT_dec (hv); |
919 | SvREFCNT_dec (hv); |
|
|
920 | DEC_DEC_DEPTH; |
871 | return 0; |
921 | return 0; |
872 | } |
922 | } |
873 | |
923 | |
874 | static SV * |
924 | static SV * |
875 | decode_sv (dec_t *dec) |
925 | decode_sv (dec_t *dec) |
… | |
… | |
918 | ERR ("'null' expected"); |
968 | ERR ("'null' expected"); |
919 | |
969 | |
920 | break; |
970 | break; |
921 | |
971 | |
922 | default: |
972 | default: |
923 | ERR ("malformed json string, neither array, object, number, string or atom"); |
973 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
924 | break; |
974 | break; |
925 | } |
975 | } |
926 | |
976 | |
927 | fail: |
977 | fail: |
928 | return 0; |
978 | return 0; |
929 | } |
979 | } |
930 | |
980 | |
931 | static SV * |
981 | static SV * |
932 | decode_json (SV *string, UV flags) |
982 | decode_json (SV *string, U32 flags) |
933 | { |
983 | { |
|
|
984 | dec_t dec; |
934 | SV *sv; |
985 | SV *sv; |
|
|
986 | |
|
|
987 | SvUPGRADE (string, SVt_PV); |
935 | |
988 | |
936 | if (flags & F_UTF8) |
989 | if (flags & F_UTF8) |
937 | sv_utf8_downgrade (string, 0); |
990 | sv_utf8_downgrade (string, 0); |
938 | else |
991 | else |
939 | sv_utf8_upgrade (string); |
992 | sv_utf8_upgrade (string); |
940 | |
993 | |
941 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
994 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
942 | |
995 | |
943 | dec_t dec; |
|
|
944 | dec.flags = flags; |
996 | dec.flags = flags; |
945 | dec.cur = SvPVX (string); |
997 | dec.cur = SvPVX (string); |
946 | dec.end = SvEND (string); |
998 | dec.end = SvEND (string); |
947 | dec.err = 0; |
999 | dec.err = 0; |
|
|
1000 | dec.depth = 0; |
|
|
1001 | dec.maxdepth = DEC_DEPTH (dec.flags); |
948 | |
1002 | |
|
|
1003 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
949 | sv = decode_sv (&dec); |
1004 | sv = decode_sv (&dec); |
950 | |
1005 | |
951 | if (!sv) |
1006 | if (!sv) |
952 | { |
1007 | { |
953 | IV offset = dec.flags & F_UTF8 |
1008 | IV offset = dec.flags & F_UTF8 |
… | |
… | |
962 | SAVEVPTR (PL_curcop); |
1017 | SAVEVPTR (PL_curcop); |
963 | PL_curcop = &cop; |
1018 | PL_curcop = &cop; |
964 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1019 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
965 | LEAVE; |
1020 | LEAVE; |
966 | |
1021 | |
967 | croak ("%s, at character offset %d (%s)", |
1022 | croak ("%s, at character offset %d [\"%s\"]", |
968 | dec.err, |
1023 | dec.err, |
969 | (int)offset, |
1024 | (int)offset, |
970 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1025 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
971 | } |
1026 | } |
972 | |
1027 | |
… | |
… | |
986 | BOOT: |
1041 | BOOT: |
987 | { |
1042 | { |
988 | int i; |
1043 | int i; |
989 | |
1044 | |
990 | memset (decode_hexdigit, 0xff, 256); |
1045 | memset (decode_hexdigit, 0xff, 256); |
|
|
1046 | |
991 | for (i = 10; i--; ) |
1047 | for (i = 0; i < 256; ++i) |
992 | decode_hexdigit ['0' + i] = i; |
1048 | decode_hexdigit [i] = |
993 | |
1049 | i >= '0' && i <= '9' ? i - '0' |
994 | for (i = 7; i--; ) |
1050 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
995 | { |
1051 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
996 | decode_hexdigit ['a' + i] = 10 + i; |
1052 | : -1; |
997 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
998 | } |
|
|
999 | |
1053 | |
1000 | json_stash = gv_stashpv ("JSON::XS", 1); |
1054 | json_stash = gv_stashpv ("JSON::XS", 1); |
1001 | } |
1055 | } |
1002 | |
1056 | |
1003 | PROTOTYPES: DISABLE |
1057 | PROTOTYPES: DISABLE |
… | |
… | |
1030 | RETVAL = newSVsv (self); |
1084 | RETVAL = newSVsv (self); |
1031 | } |
1085 | } |
1032 | OUTPUT: |
1086 | OUTPUT: |
1033 | RETVAL |
1087 | RETVAL |
1034 | |
1088 | |
|
|
1089 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1090 | CODE: |
|
|
1091 | { |
|
|
1092 | UV *uv = SvJSON (self); |
|
|
1093 | UV log2 = 0; |
|
|
1094 | |
|
|
1095 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1096 | |
|
|
1097 | while ((1UL << log2) < max_depth) |
|
|
1098 | ++log2; |
|
|
1099 | |
|
|
1100 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1101 | |
|
|
1102 | RETVAL = newSVsv (self); |
|
|
1103 | } |
|
|
1104 | OUTPUT: |
|
|
1105 | RETVAL |
|
|
1106 | |
1035 | void encode (SV *self, SV *scalar) |
1107 | void encode (SV *self, SV *scalar) |
1036 | PPCODE: |
1108 | PPCODE: |
1037 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1109 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1038 | |
1110 | |
1039 | void decode (SV *self, SV *jsonstr) |
1111 | void decode (SV *self, SV *jsonstr) |
… | |
… | |
1044 | |
1116 | |
1045 | void to_json (SV *scalar) |
1117 | void to_json (SV *scalar) |
1046 | ALIAS: |
1118 | ALIAS: |
1047 | objToJson = 0 |
1119 | objToJson = 0 |
1048 | PPCODE: |
1120 | PPCODE: |
1049 | XPUSHs (encode_json (scalar, F_UTF8)); |
1121 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1050 | |
1122 | |
1051 | void from_json (SV *jsonstr) |
1123 | void from_json (SV *jsonstr) |
1052 | ALIAS: |
1124 | ALIAS: |
1053 | jsonToObj = 0 |
1125 | jsonToObj = 0 |
1054 | PPCODE: |
1126 | PPCODE: |
1055 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1127 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1056 | |
1128 | |