… | |
… | |
15 | |
15 | |
16 | // some old perls do not have this, try to make it work, no |
16 | // some old perls do not have this, try to make it work, no |
17 | // guarantees, though. if it breaks, you get to keep the pieces. |
17 | // guarantees, though. if it breaks, you get to keep the pieces. |
18 | #ifndef UTF8_MAXBYTES |
18 | #ifndef UTF8_MAXBYTES |
19 | # define UTF8_MAXBYTES 13 |
19 | # define UTF8_MAXBYTES 13 |
|
|
20 | #endif |
|
|
21 | |
|
|
22 | // compatibility with perl <5.18 |
|
|
23 | #ifndef HvNAMELEN_get |
|
|
24 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
|
|
25 | #endif |
|
|
26 | #ifndef HvNAMELEN |
|
|
27 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
|
|
28 | #endif |
|
|
29 | #ifndef HvNAMEUTF8 |
|
|
30 | # define HvNAMEUTF8(hv) 0 |
20 | #endif |
31 | #endif |
21 | |
32 | |
22 | // three extra for rounding, sign, and end of string |
33 | // three extra for rounding, sign, and end of string |
23 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) |
34 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) |
24 | |
35 | |
… | |
… | |
683 | // encode objects, arrays and special \0=false and \1=true values. |
694 | // encode objects, arrays and special \0=false and \1=true values. |
684 | static void |
695 | static void |
685 | encode_rv (enc_t *enc, SV *sv) |
696 | encode_rv (enc_t *enc, SV *sv) |
686 | { |
697 | { |
687 | svtype svt; |
698 | svtype svt; |
|
|
699 | GV *method; |
688 | |
700 | |
689 | SvGETMAGIC (sv); |
701 | SvGETMAGIC (sv); |
690 | svt = SvTYPE (sv); |
702 | svt = SvTYPE (sv); |
691 | |
703 | |
692 | if (expect_false (SvOBJECT (sv))) |
704 | if (expect_false (SvOBJECT (sv))) |
… | |
… | |
701 | if (SvIV (sv)) |
713 | if (SvIV (sv)) |
702 | encode_str (enc, "true", 4, 0); |
714 | encode_str (enc, "true", 4, 0); |
703 | else |
715 | else |
704 | encode_str (enc, "false", 5, 0); |
716 | encode_str (enc, "false", 5, 0); |
705 | } |
717 | } |
706 | else if (enc->json.flags & F_CONV_BLESSED) |
718 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
707 | { |
719 | { |
708 | GV *to_json = gv_fetchmethod_autoload (stash, "TO_JSON", 0); |
720 | int count; |
|
|
721 | dSP; |
709 | |
722 | |
|
|
723 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
724 | EXTEND (SP, 2); |
|
|
725 | // we re-bless the reference to get overload and other niceties right |
|
|
726 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
710 | if (to_json) |
727 | PUSHs (sv_json); |
|
|
728 | |
|
|
729 | PUTBACK; |
|
|
730 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
|
|
731 | SPAGAIN; |
|
|
732 | |
|
|
733 | // catch this surprisingly common error |
|
|
734 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
735 | croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
736 | |
|
|
737 | encode_ch (enc, '('); |
|
|
738 | encode_ch (enc, '"'); |
|
|
739 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
|
|
740 | encode_ch (enc, '"'); |
|
|
741 | encode_ch (enc, ')'); |
|
|
742 | encode_ch (enc, '['); |
|
|
743 | |
|
|
744 | while (count) |
711 | { |
745 | { |
712 | dSP; |
746 | encode_sv (enc, SP[1 - count--]); |
713 | |
747 | |
714 | ENTER; SAVETMPS; PUSHMARK (SP); |
748 | if (count) |
715 | // we re-bless the reference to get overload and other niceties right |
|
|
716 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
717 | |
|
|
718 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
719 | PUTBACK; |
|
|
720 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
|
|
721 | SPAGAIN; |
|
|
722 | |
|
|
723 | // catch this surprisingly common error |
|
|
724 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
725 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
726 | |
|
|
727 | sv = POPs; |
|
|
728 | PUTBACK; |
|
|
729 | |
|
|
730 | encode_sv (enc, sv); |
749 | encode_ch (enc, ','); |
731 | |
|
|
732 | FREETMPS; LEAVE; |
|
|
733 | } |
750 | } |
734 | else if (enc->json.flags & F_ALLOW_BLESSED) |
751 | |
735 | encode_str (enc, "null", 4, 0); |
752 | encode_ch (enc, ']'); |
736 | else |
753 | |
737 | croak ("encountered object '%s' when conv_object is enabled, but TO_JSON is missing and allow_blessed disabled", |
754 | PUTBACK; |
738 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
755 | |
|
|
756 | FREETMPS; LEAVE; |
739 | } |
757 | } |
740 | else if (enc->json.flags & F_ALLOW_TAGS) |
758 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
741 | { |
759 | { |
742 | GV *method = gv_fetchmethod_autoload (stash, "FREEZE", 0); |
|
|
743 | |
|
|
744 | if (method) |
|
|
745 | { |
|
|
746 | int count; |
|
|
747 | dSP; |
760 | dSP; |
748 | |
761 | |
749 | ENTER; SAVETMPS; PUSHMARK (SP); |
762 | ENTER; SAVETMPS; PUSHMARK (SP); |
750 | EXTEND (SP, 2); |
|
|
751 | // we re-bless the reference to get overload and other niceties right |
763 | // we re-bless the reference to get overload and other niceties right |
752 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
764 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
753 | PUSHs (sv_json); |
|
|
754 | |
765 | |
|
|
766 | // calling with G_SCALAR ensures that we always get a 1 return value |
755 | PUTBACK; |
767 | PUTBACK; |
756 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
768 | call_sv ((SV *)GvCV (method), G_SCALAR); |
757 | SPAGAIN; |
769 | SPAGAIN; |
758 | |
770 | |
759 | // catch this surprisingly common error |
771 | // catch this surprisingly common error |
760 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
772 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
761 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
773 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
762 | |
774 | |
763 | encode_ch (enc, '('); |
775 | sv = POPs; |
764 | encode_ch (enc, '"'); |
|
|
765 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
|
|
766 | encode_ch (enc, '"'); |
|
|
767 | encode_ch (enc, ')'); |
|
|
768 | encode_ch (enc, '['); |
|
|
769 | |
|
|
770 | while (count) |
|
|
771 | { |
|
|
772 | encode_sv (enc, SP[1 - count--]); |
|
|
773 | |
|
|
774 | if (count) |
|
|
775 | encode_ch (enc, ','); |
|
|
776 | } |
|
|
777 | |
|
|
778 | encode_ch (enc, ']'); |
|
|
779 | |
|
|
780 | PUTBACK; |
776 | PUTBACK; |
781 | |
777 | |
|
|
778 | encode_sv (enc, sv); |
|
|
779 | |
782 | FREETMPS; LEAVE; |
780 | FREETMPS; LEAVE; |
783 | } |
|
|
784 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
785 | encode_str (enc, "null", 4, 0); |
|
|
786 | else |
|
|
787 | croak ("encountered object '%s' when allow_tags is enabled, but FREEZE is missing and allow_blessed disabled", |
|
|
788 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
789 | } |
781 | } |
790 | else if (enc->json.flags & F_ALLOW_BLESSED) |
782 | else if (enc->json.flags & F_ALLOW_BLESSED) |
791 | encode_str (enc, "null", 4, 0); |
783 | encode_str (enc, "null", 4, 0); |
792 | else |
784 | else |
793 | croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled", |
785 | croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)", |
794 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
786 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
795 | } |
787 | } |
796 | else if (svt == SVt_PVHV) |
788 | else if (svt == SVt_PVHV) |
797 | encode_hv (enc, (HV *)sv); |
789 | encode_hv (enc, (HV *)sv); |
798 | else if (svt == SVt_PVAV) |
790 | else if (svt == SVt_PVAV) |
… | |
… | |
1112 | *cur++ = *dec_cur++; |
1104 | *cur++ = *dec_cur++; |
1113 | while (--clen); |
1105 | while (--clen); |
1114 | |
1106 | |
1115 | utf8 = 1; |
1107 | utf8 = 1; |
1116 | } |
1108 | } |
|
|
1109 | else if (ch == '\t' && dec->json.flags & F_RELAXED) |
|
|
1110 | *cur++ = ch; |
1117 | else |
1111 | else |
1118 | { |
1112 | { |
1119 | --dec_cur; |
1113 | --dec_cur; |
1120 | |
1114 | |
1121 | if (!ch) |
1115 | if (!ch) |
… | |
… | |
1511 | if (!(dec->json.flags & F_ALLOW_TAGS)) |
1505 | if (!(dec->json.flags & F_ALLOW_TAGS)) |
1512 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1506 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1513 | |
1507 | |
1514 | ++dec->cur; |
1508 | ++dec->cur; |
1515 | |
1509 | |
|
|
1510 | decode_ws (dec); |
|
|
1511 | |
1516 | tag = decode_sv (dec); |
1512 | tag = decode_sv (dec); |
1517 | if (!tag) |
1513 | if (!tag) |
1518 | goto fail; |
1514 | goto fail; |
1519 | |
1515 | |
1520 | if (!SvPOK (tag)) |
1516 | if (!SvPOK (tag)) |
1521 | ERR ("malformed JSON string, (tag) must be a string"); |
1517 | ERR ("malformed JSON string, (tag) must be a string"); |
1522 | |
1518 | |
|
|
1519 | decode_ws (dec); |
|
|
1520 | |
1523 | if (*dec->cur != ')') |
1521 | if (*dec->cur != ')') |
1524 | ERR (") expected after tag"); |
1522 | ERR (") expected after tag"); |
1525 | |
1523 | |
1526 | ++dec->cur; |
1524 | ++dec->cur; |
|
|
1525 | |
|
|
1526 | decode_ws (dec); |
1527 | |
1527 | |
1528 | val = decode_sv (dec); |
1528 | val = decode_sv (dec); |
1529 | if (!val) |
1529 | if (!val) |
1530 | goto fail; |
1530 | goto fail; |
1531 | |
1531 | |