… | |
… | |
79 | #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" |
79 | #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" |
80 | |
80 | |
81 | #ifdef USE_ITHREADS |
81 | #ifdef USE_ITHREADS |
82 | # define JSON_SLOW 1 |
82 | # define JSON_SLOW 1 |
83 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
83 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
84 | # define BOOL_STASH (bool_stash ? bool_stash : gv_stashpv ("Types::Serialiser::Boolean", 1)) |
84 | #else |
85 | #else |
85 | # define JSON_SLOW 0 |
86 | # define JSON_SLOW 0 |
86 | # define JSON_STASH json_stash |
87 | # define JSON_STASH json_stash |
|
|
88 | # define BOOL_STASH bool_stash |
87 | #endif |
89 | #endif |
88 | |
90 | |
89 | // the amount of HEs to allocate on the stack, when sorting keys |
91 | // the amount of HEs to allocate on the stack, when sorting keys |
90 | #define STACK_HES 64 |
92 | #define STACK_HES 64 |
91 | |
93 | |
92 | static HV *json_stash, *types_boolean_stash; // JSON::XS:: |
94 | static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean:: |
93 | static SV *types_true, *types_false, *sv_json; |
95 | static SV *bool_true, *bool_false, *sv_json; |
94 | |
96 | |
95 | enum { |
97 | enum { |
96 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
98 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
97 | INCR_M_STR, // inside string |
99 | INCR_M_STR, // inside string |
98 | INCR_M_BS, // inside backslash |
100 | INCR_M_BS, // inside backslash |
… | |
… | |
299 | // a recursion depth of ten gives us >>500 bits |
301 | // a recursion depth of ten gives us >>500 bits |
300 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
302 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
301 | |
303 | |
302 | return neg ? -accum : accum; |
304 | return neg ? -accum : accum; |
303 | } |
305 | } |
|
|
306 | |
|
|
307 | // target of scalar reference is bool? -1 == nope, 0 == false, 1 == true |
|
|
308 | static int |
|
|
309 | ref_bool_type (SV *sv) |
|
|
310 | { |
|
|
311 | svtype svt = SvTYPE (sv); |
|
|
312 | |
|
|
313 | if (svt < SVt_PVAV) |
|
|
314 | { |
|
|
315 | STRLEN len = 0; |
|
|
316 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
317 | |
|
|
318 | if (len == 1) |
|
|
319 | if (*pv == '1') |
|
|
320 | return 1; |
|
|
321 | else if (*pv == '0') |
|
|
322 | return 0; |
|
|
323 | |
|
|
324 | } |
|
|
325 | |
|
|
326 | return -1; |
|
|
327 | } |
|
|
328 | |
|
|
329 | // returns whether scalar is not a reference in the sense of allow_nonref |
|
|
330 | static int |
|
|
331 | json_nonref (SV *scalar) |
|
|
332 | { |
|
|
333 | if (!SvROK (scalar)) |
|
|
334 | return 1; |
|
|
335 | |
|
|
336 | scalar = SvRV (scalar); |
|
|
337 | |
|
|
338 | if (SvSTASH (scalar) == bool_stash) |
|
|
339 | return 1; |
|
|
340 | |
|
|
341 | if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0) |
|
|
342 | return 1; |
|
|
343 | |
|
|
344 | return 0; |
|
|
345 | } |
|
|
346 | |
304 | ///////////////////////////////////////////////////////////////////////////// |
347 | ///////////////////////////////////////////////////////////////////////////// |
305 | // encoder |
348 | // encoder |
306 | |
349 | |
307 | // structure used for encoding JSON |
350 | // structure used for encoding JSON |
308 | typedef struct |
351 | typedef struct |
… | |
… | |
701 | SvGETMAGIC (sv); |
744 | SvGETMAGIC (sv); |
702 | svt = SvTYPE (sv); |
745 | svt = SvTYPE (sv); |
703 | |
746 | |
704 | if (expect_false (SvOBJECT (sv))) |
747 | if (expect_false (SvOBJECT (sv))) |
705 | { |
748 | { |
706 | HV *boolean_stash = !JSON_SLOW || types_boolean_stash |
|
|
707 | ? types_boolean_stash |
|
|
708 | : gv_stashpv ("Types::Serialiser::Boolean", 1); |
|
|
709 | HV *stash = SvSTASH (sv); |
749 | HV *stash = SvSTASH (sv); |
710 | |
750 | |
711 | if (stash == boolean_stash) |
751 | if (stash == bool_stash) |
712 | { |
752 | { |
713 | if (SvIV (sv)) |
753 | if (SvIV (sv)) |
714 | encode_str (enc, "true", 4, 0); |
754 | encode_str (enc, "true", 4, 0); |
715 | else |
755 | else |
716 | encode_str (enc, "false", 5, 0); |
756 | encode_str (enc, "false", 5, 0); |
… | |
… | |
718 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
758 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
719 | { |
759 | { |
720 | int count; |
760 | int count; |
721 | dSP; |
761 | dSP; |
722 | |
762 | |
723 | ENTER; SAVETMPS; PUSHMARK (SP); |
763 | ENTER; SAVETMPS; |
|
|
764 | SAVESTACK_POS (); |
|
|
765 | PUSHMARK (SP); |
724 | EXTEND (SP, 2); |
766 | EXTEND (SP, 2); |
725 | // we re-bless the reference to get overload and other niceties right |
767 | // we re-bless the reference to get overload and other niceties right |
726 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
768 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
727 | PUSHs (sv_json); |
769 | PUSHs (sv_json); |
728 | |
770 | |
… | |
… | |
730 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
772 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
731 | SPAGAIN; |
773 | SPAGAIN; |
732 | |
774 | |
733 | // catch this surprisingly common error |
775 | // catch this surprisingly common error |
734 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
776 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
735 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
777 | croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
736 | |
778 | |
737 | encode_ch (enc, '('); |
779 | encode_ch (enc, '('); |
738 | encode_ch (enc, '"'); |
780 | encode_ch (enc, '"'); |
739 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
781 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
740 | encode_ch (enc, '"'); |
782 | encode_ch (enc, '"'); |
… | |
… | |
749 | encode_ch (enc, ','); |
791 | encode_ch (enc, ','); |
750 | } |
792 | } |
751 | |
793 | |
752 | encode_ch (enc, ']'); |
794 | encode_ch (enc, ']'); |
753 | |
795 | |
754 | PUTBACK; |
|
|
755 | |
|
|
756 | FREETMPS; LEAVE; |
796 | FREETMPS; LEAVE; |
757 | } |
797 | } |
758 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
798 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
759 | { |
799 | { |
760 | dSP; |
800 | dSP; |
761 | |
801 | |
762 | ENTER; SAVETMPS; PUSHMARK (SP); |
802 | ENTER; SAVETMPS; |
|
|
803 | PUSHMARK (SP); |
763 | // we re-bless the reference to get overload and other niceties right |
804 | // we re-bless the reference to get overload and other niceties right |
764 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
805 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
765 | |
806 | |
766 | // calling with G_SCALAR ensures that we always get a 1 return value |
807 | // calling with G_SCALAR ensures that we always get a 1 return value |
767 | PUTBACK; |
808 | PUTBACK; |
… | |
… | |
789 | encode_hv (enc, (HV *)sv); |
830 | encode_hv (enc, (HV *)sv); |
790 | else if (svt == SVt_PVAV) |
831 | else if (svt == SVt_PVAV) |
791 | encode_av (enc, (AV *)sv); |
832 | encode_av (enc, (AV *)sv); |
792 | else if (svt < SVt_PVAV) |
833 | else if (svt < SVt_PVAV) |
793 | { |
834 | { |
794 | STRLEN len = 0; |
835 | int bool_type = ref_bool_type (sv); |
795 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
796 | |
836 | |
797 | if (len == 1 && *pv == '1') |
837 | if (bool_type == 1) |
798 | encode_str (enc, "true", 4, 0); |
838 | encode_str (enc, "true", 4, 0); |
799 | else if (len == 1 && *pv == '0') |
839 | else if (bool_type == 0) |
800 | encode_str (enc, "false", 5, 0); |
840 | encode_str (enc, "false", 5, 0); |
801 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
841 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
802 | encode_str (enc, "null", 4, 0); |
842 | encode_str (enc, "null", 4, 0); |
803 | else |
843 | else |
804 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
844 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
… | |
… | |
887 | static SV * |
927 | static SV * |
888 | encode_json (SV *scalar, JSON *json) |
928 | encode_json (SV *scalar, JSON *json) |
889 | { |
929 | { |
890 | enc_t enc; |
930 | enc_t enc; |
891 | |
931 | |
892 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
932 | if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar)) |
893 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
933 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
894 | |
934 | |
895 | enc.json = *json; |
935 | enc.json = *json; |
896 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
936 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
897 | enc.cur = SvPVX (enc.sv); |
937 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
1104 | *cur++ = *dec_cur++; |
1144 | *cur++ = *dec_cur++; |
1105 | while (--clen); |
1145 | while (--clen); |
1106 | |
1146 | |
1107 | utf8 = 1; |
1147 | utf8 = 1; |
1108 | } |
1148 | } |
|
|
1149 | else if (ch == '\t' && dec->json.flags & F_RELAXED) |
|
|
1150 | *cur++ = ch; |
1109 | else |
1151 | else |
1110 | { |
1152 | { |
1111 | --dec_cur; |
1153 | --dec_cur; |
1112 | |
1154 | |
1113 | if (!ch) |
1155 | if (!ch) |
… | |
… | |
1435 | |
1477 | |
1436 | hv_iterinit (hv); |
1478 | hv_iterinit (hv); |
1437 | he = hv_iternext (hv); |
1479 | he = hv_iternext (hv); |
1438 | hv_iterinit (hv); |
1480 | hv_iterinit (hv); |
1439 | |
1481 | |
1440 | // the next line creates a mortal sv each time its called. |
1482 | // the next line creates a mortal sv each time it's called. |
1441 | // might want to optimise this for common cases. |
1483 | // might want to optimise this for common cases. |
1442 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1484 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1443 | |
1485 | |
1444 | if (cb) |
1486 | if (cb) |
1445 | { |
1487 | { |
1446 | dSP; |
1488 | dSP; |
1447 | int count; |
1489 | int count; |
1448 | |
1490 | |
1449 | ENTER; SAVETMPS; PUSHMARK (SP); |
1491 | ENTER; SAVETMPS; |
|
|
1492 | SAVESTACK_POS (); |
|
|
1493 | PUSHMARK (SP); |
1450 | XPUSHs (HeVAL (he)); |
1494 | XPUSHs (HeVAL (he)); |
1451 | sv_2mortal (sv); |
1495 | sv_2mortal (sv); |
1452 | |
1496 | |
1453 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1497 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1454 | |
1498 | |
… | |
… | |
1467 | if (dec->json.cb_object) |
1511 | if (dec->json.cb_object) |
1468 | { |
1512 | { |
1469 | dSP; |
1513 | dSP; |
1470 | int count; |
1514 | int count; |
1471 | |
1515 | |
1472 | ENTER; SAVETMPS; PUSHMARK (SP); |
1516 | ENTER; SAVETMPS; |
|
|
1517 | SAVESTACK_POS (); |
|
|
1518 | PUSHMARK (SP); |
1473 | XPUSHs (sv_2mortal (sv)); |
1519 | XPUSHs (sv_2mortal (sv)); |
1474 | |
1520 | |
1475 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1521 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1476 | |
1522 | |
1477 | if (count == 1) |
1523 | if (count == 1) |
… | |
… | |
1544 | if (!method) |
1590 | if (!method) |
1545 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1591 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1546 | |
1592 | |
1547 | dSP; |
1593 | dSP; |
1548 | |
1594 | |
1549 | ENTER; SAVETMPS; PUSHMARK (SP); |
1595 | ENTER; SAVETMPS; |
|
|
1596 | PUSHMARK (SP); |
1550 | EXTEND (SP, len + 2); |
1597 | EXTEND (SP, len + 2); |
1551 | // we re-bless the reference to get overload and other niceties right |
1598 | // we re-bless the reference to get overload and other niceties right |
1552 | PUSHs (tag); |
1599 | PUSHs (tag); |
1553 | PUSHs (sv_json); |
1600 | PUSHs (sv_json); |
1554 | |
1601 | |
… | |
… | |
1596 | case 't': |
1643 | case 't': |
1597 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1644 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1598 | { |
1645 | { |
1599 | dec->cur += 4; |
1646 | dec->cur += 4; |
1600 | #if JSON_SLOW |
1647 | #if JSON_SLOW |
1601 | types_true = get_bool ("Types::Serialiser::true"); |
1648 | bool_true = get_bool ("Types::Serialiser::true"); |
1602 | #endif |
1649 | #endif |
1603 | return newSVsv (types_true); |
1650 | return newSVsv (bool_true); |
1604 | } |
1651 | } |
1605 | else |
1652 | else |
1606 | ERR ("'true' expected"); |
1653 | ERR ("'true' expected"); |
1607 | |
1654 | |
1608 | break; |
1655 | break; |
… | |
… | |
1610 | case 'f': |
1657 | case 'f': |
1611 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1658 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1612 | { |
1659 | { |
1613 | dec->cur += 5; |
1660 | dec->cur += 5; |
1614 | #if JSON_SLOW |
1661 | #if JSON_SLOW |
1615 | types_false = get_bool ("Types::Serialiser::false"); |
1662 | bool_false = get_bool ("Types::Serialiser::false"); |
1616 | #endif |
1663 | #endif |
1617 | return newSVsv (types_false); |
1664 | return newSVsv (bool_false); |
1618 | } |
1665 | } |
1619 | else |
1666 | else |
1620 | ERR ("'false' expected"); |
1667 | ERR ("'false' expected"); |
1621 | |
1668 | |
1622 | break; |
1669 | break; |
… | |
… | |
1649 | |
1696 | |
1650 | /* work around bugs in 5.10 where manipulating magic values |
1697 | /* work around bugs in 5.10 where manipulating magic values |
1651 | * makes perl ignore the magic in subsequent accesses. |
1698 | * makes perl ignore the magic in subsequent accesses. |
1652 | * also make a copy of non-PV values, to get them into a clean |
1699 | * also make a copy of non-PV values, to get them into a clean |
1653 | * state (SvPV should do that, but it's buggy, see below). |
1700 | * state (SvPV should do that, but it's buggy, see below). |
|
|
1701 | * |
|
|
1702 | * SvIsCOW_shared_hash works around a bug in perl (possibly 5.16), |
|
|
1703 | * as reported by Reini Urban. |
1654 | */ |
1704 | */ |
1655 | /*SvGETMAGIC (string);*/ |
1705 | /*SvGETMAGIC (string);*/ |
1656 | if (SvMAGICAL (string) || !SvPOK (string)) |
1706 | if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) |
1657 | string = sv_2mortal (newSVsv (string)); |
1707 | string = sv_2mortal (newSVsv (string)); |
1658 | |
1708 | |
1659 | SvUPGRADE (string, SVt_PV); |
1709 | SvUPGRADE (string, SVt_PV); |
1660 | |
1710 | |
1661 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1711 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
… | |
… | |
1736 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1786 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1737 | } |
1787 | } |
1738 | |
1788 | |
1739 | sv = sv_2mortal (sv); |
1789 | sv = sv_2mortal (sv); |
1740 | |
1790 | |
1741 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1791 | if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv)) |
1742 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1792 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1743 | |
1793 | |
1744 | return sv; |
1794 | return sv; |
1745 | } |
1795 | } |
1746 | |
1796 | |
… | |
… | |
1916 | i >= '0' && i <= '9' ? i - '0' |
1966 | i >= '0' && i <= '9' ? i - '0' |
1917 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1967 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1918 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1968 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1919 | : -1; |
1969 | : -1; |
1920 | |
1970 | |
1921 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1971 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1922 | types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1972 | bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1923 | |
|
|
1924 | types_true = get_bool ("Types::Serialiser::true"); |
1973 | bool_true = get_bool ("Types::Serialiser::true"); |
1925 | types_false = get_bool ("Types::Serialiser::false"); |
1974 | bool_false = get_bool ("Types::Serialiser::false"); |
1926 | |
1975 | |
1927 | sv_json = newSVpv ("JSON", 0); |
1976 | sv_json = newSVpv ("JSON", 0); |
1928 | SvREADONLY_on (sv_json); |
1977 | SvREADONLY_on (sv_json); |
1929 | |
1978 | |
1930 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1979 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
… | |
… | |
1932 | |
1981 | |
1933 | PROTOTYPES: DISABLE |
1982 | PROTOTYPES: DISABLE |
1934 | |
1983 | |
1935 | void CLONE (...) |
1984 | void CLONE (...) |
1936 | CODE: |
1985 | CODE: |
1937 | json_stash = 0; |
1986 | json_stash = 0; |
1938 | types_boolean_stash = 0; |
1987 | bool_stash = 0; |
1939 | |
1988 | |
1940 | void new (char *klass) |
1989 | void new (char *klass) |
1941 | PPCODE: |
1990 | PPCODE: |
1942 | { |
1991 | { |
1943 | SV *pv = NEWSV (0, sizeof (JSON)); |
1992 | SV *pv = NEWSV (0, sizeof (JSON)); |