… | |
… | |
9 | |
9 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
12 | #endif |
13 | |
13 | |
|
|
14 | // some old perls do not have this, try to make it work, no |
|
|
15 | // guarentees, though. if it breaks, you get to keep the pieces. |
|
|
16 | #ifndef UTF8_MAXBYTES |
|
|
17 | # define UTF8_MAXBYTES 13 |
|
|
18 | #endif |
|
|
19 | |
14 | #define F_ASCII 0x00000001UL |
20 | #define F_ASCII 0x00000001UL |
15 | #define F_LATIN1 0x00000002UL |
21 | #define F_LATIN1 0x00000002UL |
16 | #define F_UTF8 0x00000004UL |
22 | #define F_UTF8 0x00000004UL |
17 | #define F_INDENT 0x00000008UL |
23 | #define F_INDENT 0x00000008UL |
18 | #define F_CANONICAL 0x00000010UL |
24 | #define F_CANONICAL 0x00000010UL |
19 | #define F_SPACE_BEFORE 0x00000020UL |
25 | #define F_SPACE_BEFORE 0x00000020UL |
20 | #define F_SPACE_AFTER 0x00000040UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
21 | #define F_ALLOW_NONREF 0x00000100UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
22 | #define F_SHRINK 0x00000200UL |
28 | #define F_SHRINK 0x00000200UL |
|
|
29 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
23 | #define F_MAXDEPTH 0xf8000000UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
24 | #define S_MAXDEPTH 27 |
32 | #define S_MAXDEPTH 27 |
25 | |
33 | |
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
34 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
27 | |
|
|
28 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
29 | // F_BLESSED? <=> { $__class__$ => } |
|
|
30 | |
35 | |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
36 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
37 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
33 | |
38 | |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
39 | #define INIT_SIZE 32 // initial scalar size to be allocated |
35 | #define INDENT_STEP 3 // spaces per indentation level |
40 | #define INDENT_STEP 3 // spaces per indentation level |
36 | |
41 | |
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
42 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
38 | |
43 | |
39 | #define SB do { |
44 | #define SB do { |
40 | #define SE } while (0) |
45 | #define SE } while (0) |
41 | |
46 | |
42 | #if __GNUC__ >= 3 |
47 | #if __GNUC__ >= 3 |
… | |
… | |
48 | #endif |
53 | #endif |
49 | |
54 | |
50 | #define expect_false(expr) expect ((expr) != 0, 0) |
55 | #define expect_false(expr) expect ((expr) != 0, 0) |
51 | #define expect_true(expr) expect ((expr) != 0, 1) |
56 | #define expect_true(expr) expect ((expr) != 0, 1) |
52 | |
57 | |
53 | static HV *json_stash; // JSON::XS:: |
58 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
59 | static SV *json_true, *json_false; |
54 | |
60 | |
55 | ///////////////////////////////////////////////////////////////////////////// |
61 | ///////////////////////////////////////////////////////////////////////////// |
56 | // utility functions |
62 | // utility functions |
57 | |
63 | |
58 | static UV * |
64 | static UV * |
… | |
… | |
178 | STRLEN clen; |
184 | STRLEN clen; |
179 | UV uch; |
185 | UV uch; |
180 | |
186 | |
181 | if (is_utf8) |
187 | if (is_utf8) |
182 | { |
188 | { |
183 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
184 | uch = decode_utf8 (str, end - str, &clen); |
189 | uch = decode_utf8 (str, end - str, &clen); |
185 | if (clen == (STRLEN)-1) |
190 | if (clen == (STRLEN)-1) |
186 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
191 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
187 | } |
192 | } |
188 | else |
193 | else |
… | |
… | |
463 | svtype svt; |
468 | svtype svt; |
464 | |
469 | |
465 | SvGETMAGIC (sv); |
470 | SvGETMAGIC (sv); |
466 | svt = SvTYPE (sv); |
471 | svt = SvTYPE (sv); |
467 | |
472 | |
|
|
473 | if (expect_false (SvOBJECT (sv))) |
|
|
474 | { |
|
|
475 | if (SvSTASH (sv) == json_boolean_stash) |
|
|
476 | { |
|
|
477 | if (SvIV (sv) == 0) |
|
|
478 | encode_str (enc, "false", 5, 0); |
|
|
479 | else |
|
|
480 | encode_str (enc, "true", 4, 0); |
|
|
481 | } |
|
|
482 | else |
|
|
483 | { |
|
|
484 | #if 0 |
|
|
485 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
486 | { |
|
|
487 | // not yet |
|
|
488 | } |
|
|
489 | #endif |
|
|
490 | if (enc->flags & F_CONV_BLESSED) |
|
|
491 | { |
|
|
492 | // we re-bless the reference to get overload and other niceties right |
|
|
493 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
|
|
494 | |
|
|
495 | if (to_json) |
|
|
496 | { |
|
|
497 | dSP; |
|
|
498 | ENTER; |
|
|
499 | SAVETMPS; |
|
|
500 | PUSHMARK (SP); |
|
|
501 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
502 | |
|
|
503 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
|
|
504 | // check anyways. |
|
|
505 | PUTBACK; |
|
|
506 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
|
|
507 | SPAGAIN; |
|
|
508 | |
|
|
509 | encode_sv (enc, POPs); |
|
|
510 | |
|
|
511 | FREETMPS; |
|
|
512 | LEAVE; |
|
|
513 | } |
|
|
514 | else if (enc->flags & F_ALLOW_BLESSED) |
|
|
515 | encode_str (enc, "null", 4, 0); |
|
|
516 | else |
|
|
517 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
518 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
519 | } |
|
|
520 | else if (enc->flags & F_ALLOW_BLESSED) |
|
|
521 | encode_str (enc, "null", 4, 0); |
|
|
522 | else |
|
|
523 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
524 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
525 | } |
|
|
526 | } |
468 | if (svt == SVt_PVHV) |
527 | else if (svt == SVt_PVHV) |
469 | encode_hv (enc, (HV *)sv); |
528 | encode_hv (enc, (HV *)sv); |
470 | else if (svt == SVt_PVAV) |
529 | else if (svt == SVt_PVAV) |
471 | encode_av (enc, (AV *)sv); |
530 | encode_av (enc, (AV *)sv); |
472 | else if (svt < SVt_PVAV) |
531 | else if (svt < SVt_PVAV) |
473 | { |
532 | { |
… | |
… | |
497 | encode_str (enc, str, len, SvUTF8 (sv)); |
556 | encode_str (enc, str, len, SvUTF8 (sv)); |
498 | encode_ch (enc, '"'); |
557 | encode_ch (enc, '"'); |
499 | } |
558 | } |
500 | else if (SvNOKp (sv)) |
559 | else if (SvNOKp (sv)) |
501 | { |
560 | { |
|
|
561 | // trust that perl will do the right thing w.r.t. JSON syntax. |
502 | need (enc, NV_DIG + 32); |
562 | need (enc, NV_DIG + 32); |
503 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
563 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
504 | enc->cur += strlen (enc->cur); |
564 | enc->cur += strlen (enc->cur); |
505 | } |
565 | } |
506 | else if (SvIOKp (sv)) |
566 | else if (SvIOKp (sv)) |
507 | { |
567 | { |
508 | // we assume we can always read an IV as a UV |
568 | // we assume we can always read an IV as a UV |
509 | if (SvUV (sv) & ~(UV)0x7fff) |
569 | if (SvUV (sv) & ~(UV)0x7fff) |
510 | { |
570 | { |
|
|
571 | // large integer, use the (rather slow) snprintf way. |
511 | need (enc, 32); |
572 | need (enc, sizeof (UV) * 3); |
512 | enc->cur += |
573 | enc->cur += |
513 | SvIsUV(sv) |
574 | SvIsUV(sv) |
514 | ? snprintf (enc->cur, 32, "%"UVuf, (UV)SvUVX (sv)) |
575 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
515 | : snprintf (enc->cur, 32, "%"IVdf, (IV)SvIVX (sv)); |
576 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
516 | } |
577 | } |
517 | else |
578 | else |
518 | { |
579 | { |
519 | // optimise the "small number case" |
580 | // optimise the "small number case" |
520 | // code will likely be branchless and use only a single multiplication |
581 | // code will likely be branchless and use only a single multiplication |
521 | I32 i = SvIV (sv); |
582 | I32 i = SvIV (sv); |
522 | U32 u; |
583 | U32 u; |
|
|
584 | char digit, nz = 0; |
523 | |
585 | |
524 | need (enc, 6); |
586 | need (enc, 6); |
525 | |
587 | |
526 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
588 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
527 | u = i < 0 ? -i : i; |
589 | u = i < 0 ? -i : i; |
528 | |
590 | |
529 | // convert to 4.28 fixed-point representation |
591 | // convert to 4.28 fixed-point representation |
530 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
592 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
531 | |
593 | |
532 | char digit, nz = 0; |
594 | // now output digit by digit, each time masking out the integer part |
533 | |
595 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
596 | // resulting in a net multiplication by 10. |
|
|
597 | // we always write the digit to memory but conditionally increment |
|
|
598 | // the pointer, to ease the usage of conditional move instructions. |
534 | digit = u >> 28; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0xfffffff) * 5; |
599 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
535 | digit = u >> 27; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x7ffffff) * 5; |
600 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
536 | digit = u >> 26; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x3ffffff) * 5; |
601 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
537 | digit = u >> 25; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x1ffffff) * 5; |
602 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
538 | digit = u >> 24; *enc->cur = digit + '0'; nz |= digit; enc->cur += 1; |
603 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
539 | } |
604 | } |
540 | } |
605 | } |
541 | else if (SvROK (sv)) |
606 | else if (SvROK (sv)) |
542 | encode_rv (enc, SvRV (sv)); |
607 | encode_rv (enc, SvRV (sv)); |
543 | else if (!SvOK (sv)) |
608 | else if (!SvOK (sv)) |
… | |
… | |
625 | decode_4hex (dec_t *dec) |
690 | decode_4hex (dec_t *dec) |
626 | { |
691 | { |
627 | signed char d1, d2, d3, d4; |
692 | signed char d1, d2, d3, d4; |
628 | unsigned char *cur = (unsigned char *)dec->cur; |
693 | unsigned char *cur = (unsigned char *)dec->cur; |
629 | |
694 | |
630 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); |
695 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
631 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); |
696 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
632 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); |
697 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
633 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); |
698 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
634 | |
699 | |
635 | dec->cur += 4; |
700 | dec->cur += 4; |
636 | |
701 | |
637 | return ((UV)d1) << 12 |
702 | return ((UV)d1) << 12 |
638 | | ((UV)d2) << 8 |
703 | | ((UV)d2) << 8 |
… | |
… | |
646 | static SV * |
711 | static SV * |
647 | decode_str (dec_t *dec) |
712 | decode_str (dec_t *dec) |
648 | { |
713 | { |
649 | SV *sv = 0; |
714 | SV *sv = 0; |
650 | int utf8 = 0; |
715 | int utf8 = 0; |
|
|
716 | char *dec_cur = dec->cur; |
651 | |
717 | |
652 | do |
718 | do |
653 | { |
719 | { |
654 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
720 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
655 | char *cur = buf; |
721 | char *cur = buf; |
656 | |
722 | |
657 | do |
723 | do |
658 | { |
724 | { |
659 | unsigned char ch = *(unsigned char *)dec->cur++; |
725 | unsigned char ch = *(unsigned char *)dec_cur++; |
660 | |
726 | |
661 | if (expect_false (ch == '"')) |
727 | if (expect_false (ch == '"')) |
662 | { |
728 | { |
663 | --dec->cur; |
729 | --dec_cur; |
664 | break; |
730 | break; |
665 | } |
731 | } |
666 | else if (expect_false (ch == '\\')) |
732 | else if (expect_false (ch == '\\')) |
667 | { |
733 | { |
668 | switch (*dec->cur) |
734 | switch (*dec_cur) |
669 | { |
735 | { |
670 | case '\\': |
736 | case '\\': |
671 | case '/': |
737 | case '/': |
672 | case '"': *cur++ = *dec->cur++; break; |
738 | case '"': *cur++ = *dec_cur++; break; |
673 | |
739 | |
674 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
740 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
675 | case 't': ++dec->cur; *cur++ = '\011'; break; |
741 | case 't': ++dec_cur; *cur++ = '\011'; break; |
676 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
742 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
677 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
743 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
678 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
744 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
679 | |
745 | |
680 | case 'u': |
746 | case 'u': |
681 | { |
747 | { |
682 | UV lo, hi; |
748 | UV lo, hi; |
683 | ++dec->cur; |
749 | ++dec_cur; |
684 | |
750 | |
|
|
751 | dec->cur = dec_cur; |
685 | hi = decode_4hex (dec); |
752 | hi = decode_4hex (dec); |
|
|
753 | dec_cur = dec->cur; |
686 | if (hi == (UV)-1) |
754 | if (hi == (UV)-1) |
687 | goto fail; |
755 | goto fail; |
688 | |
756 | |
689 | // possibly a surrogate pair |
757 | // possibly a surrogate pair |
690 | if (hi >= 0xd800) |
758 | if (hi >= 0xd800) |
691 | if (hi < 0xdc00) |
759 | if (hi < 0xdc00) |
692 | { |
760 | { |
693 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
761 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
694 | ERR ("missing low surrogate character in surrogate pair"); |
762 | ERR ("missing low surrogate character in surrogate pair"); |
695 | |
763 | |
696 | dec->cur += 2; |
764 | dec_cur += 2; |
697 | |
765 | |
|
|
766 | dec->cur = dec_cur; |
698 | lo = decode_4hex (dec); |
767 | lo = decode_4hex (dec); |
|
|
768 | dec_cur = dec->cur; |
699 | if (lo == (UV)-1) |
769 | if (lo == (UV)-1) |
700 | goto fail; |
770 | goto fail; |
701 | |
771 | |
702 | if (lo < 0xdc00 || lo >= 0xe000) |
772 | if (lo < 0xdc00 || lo >= 0xe000) |
703 | ERR ("surrogate pair expected"); |
773 | ERR ("surrogate pair expected"); |
… | |
… | |
717 | *cur++ = hi; |
787 | *cur++ = hi; |
718 | } |
788 | } |
719 | break; |
789 | break; |
720 | |
790 | |
721 | default: |
791 | default: |
722 | --dec->cur; |
792 | --dec_cur; |
723 | ERR ("illegal backslash escape sequence in string"); |
793 | ERR ("illegal backslash escape sequence in string"); |
724 | } |
794 | } |
725 | } |
795 | } |
726 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
796 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
727 | *cur++ = ch; |
797 | *cur++ = ch; |
728 | else if (ch >= 0x80) |
798 | else if (ch >= 0x80) |
729 | { |
799 | { |
730 | STRLEN clen; |
800 | STRLEN clen; |
731 | UV uch; |
801 | UV uch; |
732 | |
802 | |
733 | --dec->cur; |
803 | --dec_cur; |
734 | |
804 | |
735 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
805 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
736 | if (clen == (STRLEN)-1) |
806 | if (clen == (STRLEN)-1) |
737 | ERR ("malformed UTF-8 character in JSON string"); |
807 | ERR ("malformed UTF-8 character in JSON string"); |
738 | |
808 | |
739 | do |
809 | do |
740 | *cur++ = *dec->cur++; |
810 | *cur++ = *dec_cur++; |
741 | while (--clen); |
811 | while (--clen); |
742 | |
812 | |
743 | utf8 = 1; |
813 | utf8 = 1; |
744 | } |
814 | } |
745 | else |
815 | else |
746 | { |
816 | { |
747 | --dec->cur; |
817 | --dec_cur; |
748 | |
818 | |
749 | if (!ch) |
819 | if (!ch) |
750 | ERR ("unexpected end of string while parsing JSON string"); |
820 | ERR ("unexpected end of string while parsing JSON string"); |
751 | else |
821 | else |
752 | ERR ("invalid character encountered while parsing JSON string"); |
822 | ERR ("invalid character encountered while parsing JSON string"); |
… | |
… | |
765 | } |
835 | } |
766 | else |
836 | else |
767 | sv = newSVpvn (buf, len); |
837 | sv = newSVpvn (buf, len); |
768 | } |
838 | } |
769 | } |
839 | } |
770 | while (*dec->cur != '"'); |
840 | while (*dec_cur != '"'); |
771 | |
841 | |
772 | ++dec->cur; |
842 | ++dec_cur; |
773 | |
843 | |
774 | if (sv) |
844 | if (sv) |
775 | { |
845 | { |
776 | SvPOK_only (sv); |
846 | SvPOK_only (sv); |
777 | *SvEND (sv) = 0; |
847 | *SvEND (sv) = 0; |
… | |
… | |
780 | SvUTF8_on (sv); |
850 | SvUTF8_on (sv); |
781 | } |
851 | } |
782 | else |
852 | else |
783 | sv = newSVpvn ("", 0); |
853 | sv = newSVpvn ("", 0); |
784 | |
854 | |
|
|
855 | dec->cur = dec_cur; |
785 | return sv; |
856 | return sv; |
786 | |
857 | |
787 | fail: |
858 | fail: |
|
|
859 | dec->cur = dec_cur; |
788 | return 0; |
860 | return 0; |
789 | } |
861 | } |
790 | |
862 | |
791 | static SV * |
863 | static SV * |
792 | decode_num (dec_t *dec) |
864 | decode_num (dec_t *dec) |
… | |
… | |
854 | { |
926 | { |
855 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
927 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
856 | if (*start == '-') |
928 | if (*start == '-') |
857 | switch (dec->cur - start) |
929 | switch (dec->cur - start) |
858 | { |
930 | { |
859 | case 2: return newSViv (-( start [1] - '0' )); |
931 | case 2: return newSViv (-( start [1] - '0' * 1)); |
860 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
932 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
861 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
933 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
862 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
934 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
863 | } |
935 | } |
864 | else |
936 | else |
865 | switch (dec->cur - start) |
937 | switch (dec->cur - start) |
866 | { |
938 | { |
867 | case 1: return newSViv ( start [0] - '0' ); |
939 | case 1: return newSViv ( start [0] - '0' * 1); |
868 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
940 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
869 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
941 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
870 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
942 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
871 | } |
943 | } |
872 | |
944 | |
… | |
… | |
879 | if (uv < (UV)IV_MIN) |
951 | if (uv < (UV)IV_MIN) |
880 | return newSViv (-(IV)uv); |
952 | return newSViv (-(IV)uv); |
881 | } |
953 | } |
882 | else |
954 | else |
883 | return newSVuv (uv); |
955 | return newSVuv (uv); |
|
|
956 | |
|
|
957 | // here would likely be the place for bigint support |
884 | } |
958 | } |
885 | } |
959 | } |
886 | |
960 | |
|
|
961 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
887 | return newSVnv (Atof (start)); |
962 | return newSVnv (Atof (start)); |
888 | |
963 | |
889 | fail: |
964 | fail: |
890 | return 0; |
965 | return 0; |
891 | } |
966 | } |
… | |
… | |
992 | |
1067 | |
993 | static SV * |
1068 | static SV * |
994 | decode_sv (dec_t *dec) |
1069 | decode_sv (dec_t *dec) |
995 | { |
1070 | { |
996 | decode_ws (dec); |
1071 | decode_ws (dec); |
|
|
1072 | |
|
|
1073 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1074 | // to parse anything. |
997 | switch (*dec->cur) |
1075 | switch (*dec->cur) |
998 | { |
1076 | { |
999 | case '"': ++dec->cur; return decode_str (dec); |
1077 | case '"': ++dec->cur; return decode_str (dec); |
1000 | case '[': ++dec->cur; return decode_av (dec); |
1078 | case '[': ++dec->cur; return decode_av (dec); |
1001 | case '{': ++dec->cur; return decode_hv (dec); |
1079 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
1007 | |
1085 | |
1008 | case 't': |
1086 | case 't': |
1009 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1087 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1010 | { |
1088 | { |
1011 | dec->cur += 4; |
1089 | dec->cur += 4; |
1012 | return newSViv (1); |
1090 | return SvREFCNT_inc (json_true); |
1013 | } |
1091 | } |
1014 | else |
1092 | else |
1015 | ERR ("'true' expected"); |
1093 | ERR ("'true' expected"); |
1016 | |
1094 | |
1017 | break; |
1095 | break; |
1018 | |
1096 | |
1019 | case 'f': |
1097 | case 'f': |
1020 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1098 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1021 | { |
1099 | { |
1022 | dec->cur += 5; |
1100 | dec->cur += 5; |
1023 | return newSViv (0); |
1101 | return SvREFCNT_inc (json_false); |
1024 | } |
1102 | } |
1025 | else |
1103 | else |
1026 | ERR ("'false' expected"); |
1104 | ERR ("'false' expected"); |
1027 | |
1105 | |
1028 | break; |
1106 | break; |
… | |
… | |
1131 | |
1209 | |
1132 | BOOT: |
1210 | BOOT: |
1133 | { |
1211 | { |
1134 | int i; |
1212 | int i; |
1135 | |
1213 | |
1136 | memset (decode_hexdigit, 0xff, 256); |
|
|
1137 | |
|
|
1138 | for (i = 0; i < 256; ++i) |
1214 | for (i = 0; i < 256; ++i) |
1139 | decode_hexdigit [i] = |
1215 | decode_hexdigit [i] = |
1140 | i >= '0' && i <= '9' ? i - '0' |
1216 | i >= '0' && i <= '9' ? i - '0' |
1141 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1217 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1142 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1218 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1143 | : -1; |
1219 | : -1; |
1144 | |
1220 | |
1145 | json_stash = gv_stashpv ("JSON::XS", 1); |
1221 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1222 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1223 | |
|
|
1224 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1225 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1146 | } |
1226 | } |
1147 | |
1227 | |
1148 | PROTOTYPES: DISABLE |
1228 | PROTOTYPES: DISABLE |
1149 | |
1229 | |
1150 | SV *new (char *dummy) |
1230 | SV *new (char *dummy) |
… | |
… | |
1153 | OUTPUT: |
1233 | OUTPUT: |
1154 | RETVAL |
1234 | RETVAL |
1155 | |
1235 | |
1156 | SV *ascii (SV *self, int enable = 1) |
1236 | SV *ascii (SV *self, int enable = 1) |
1157 | ALIAS: |
1237 | ALIAS: |
1158 | ascii = F_ASCII |
1238 | ascii = F_ASCII |
1159 | latin1 = F_LATIN1 |
1239 | latin1 = F_LATIN1 |
1160 | utf8 = F_UTF8 |
1240 | utf8 = F_UTF8 |
1161 | indent = F_INDENT |
1241 | indent = F_INDENT |
1162 | canonical = F_CANONICAL |
1242 | canonical = F_CANONICAL |
1163 | space_before = F_SPACE_BEFORE |
1243 | space_before = F_SPACE_BEFORE |
1164 | space_after = F_SPACE_AFTER |
1244 | space_after = F_SPACE_AFTER |
1165 | pretty = F_PRETTY |
1245 | pretty = F_PRETTY |
1166 | allow_nonref = F_ALLOW_NONREF |
1246 | allow_nonref = F_ALLOW_NONREF |
1167 | shrink = F_SHRINK |
1247 | shrink = F_SHRINK |
|
|
1248 | allow_blessed = F_ALLOW_BLESSED |
|
|
1249 | convert_blessed = F_CONV_BLESSED |
1168 | CODE: |
1250 | CODE: |
1169 | { |
1251 | { |
1170 | UV *uv = SvJSON (self); |
1252 | UV *uv = SvJSON (self); |
1171 | if (enable) |
1253 | if (enable) |
1172 | *uv |= ix; |
1254 | *uv |= ix; |