… | |
… | |
33 | #define F_SHRINK 0x00000200UL |
33 | #define F_SHRINK 0x00000200UL |
34 | #define F_ALLOW_BLESSED 0x00000400UL |
34 | #define F_ALLOW_BLESSED 0x00000400UL |
35 | #define F_CONV_BLESSED 0x00000800UL |
35 | #define F_CONV_BLESSED 0x00000800UL |
36 | #define F_RELAXED 0x00001000UL |
36 | #define F_RELAXED 0x00001000UL |
37 | #define F_ALLOW_UNKNOWN 0x00002000UL |
37 | #define F_ALLOW_UNKNOWN 0x00002000UL |
|
|
38 | #define F_ALLOW_TAGS 0x00004000UL |
38 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
39 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
39 | |
40 | |
40 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
41 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
41 | |
42 | |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
43 | #define INIT_SIZE 32 // initial scalar size to be allocated |
… | |
… | |
75 | #endif |
76 | #endif |
76 | |
77 | |
77 | // the amount of HEs to allocate on the stack, when sorting keys |
78 | // the amount of HEs to allocate on the stack, when sorting keys |
78 | #define STACK_HES 64 |
79 | #define STACK_HES 64 |
79 | |
80 | |
80 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
81 | static HV *json_stash, *types_boolean_stash; // JSON::XS:: |
81 | static SV *json_true, *json_false; |
82 | static SV *types_true, *types_false, *sv_json; |
82 | |
83 | |
83 | enum { |
84 | enum { |
84 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
85 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
85 | INCR_M_STR, // inside string |
86 | INCR_M_STR, // inside string |
86 | INCR_M_BS, // inside backslash |
87 | INCR_M_BS, // inside backslash |
… | |
… | |
682 | // encode objects, arrays and special \0=false and \1=true values. |
683 | // encode objects, arrays and special \0=false and \1=true values. |
683 | static void |
684 | static void |
684 | encode_rv (enc_t *enc, SV *sv) |
685 | encode_rv (enc_t *enc, SV *sv) |
685 | { |
686 | { |
686 | svtype svt; |
687 | svtype svt; |
|
|
688 | GV *method; |
687 | |
689 | |
688 | SvGETMAGIC (sv); |
690 | SvGETMAGIC (sv); |
689 | svt = SvTYPE (sv); |
691 | svt = SvTYPE (sv); |
690 | |
692 | |
691 | if (expect_false (SvOBJECT (sv))) |
693 | if (expect_false (SvOBJECT (sv))) |
692 | { |
694 | { |
693 | HV *stash = !JSON_SLOW || json_boolean_stash |
695 | HV *boolean_stash = !JSON_SLOW || types_boolean_stash |
694 | ? json_boolean_stash |
696 | ? types_boolean_stash |
695 | : gv_stashpv ("JSON::XS::Boolean", 1); |
697 | : gv_stashpv ("Types::Serialiser::Boolean", 1); |
|
|
698 | HV *stash = SvSTASH (sv); |
696 | |
699 | |
697 | if (SvSTASH (sv) == stash) |
700 | if (stash == boolean_stash) |
698 | { |
701 | { |
699 | if (SvIV (sv)) |
702 | if (SvIV (sv)) |
700 | encode_str (enc, "true", 4, 0); |
703 | encode_str (enc, "true", 4, 0); |
701 | else |
704 | else |
702 | encode_str (enc, "false", 5, 0); |
705 | encode_str (enc, "false", 5, 0); |
703 | } |
706 | } |
|
|
707 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
|
|
708 | { |
|
|
709 | int count; |
|
|
710 | dSP; |
|
|
711 | |
|
|
712 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
713 | EXTEND (SP, 2); |
|
|
714 | // we re-bless the reference to get overload and other niceties right |
|
|
715 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
716 | PUSHs (sv_json); |
|
|
717 | |
|
|
718 | PUTBACK; |
|
|
719 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
|
|
720 | SPAGAIN; |
|
|
721 | |
|
|
722 | // catch this surprisingly common error |
|
|
723 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
724 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
725 | |
|
|
726 | encode_ch (enc, '('); |
|
|
727 | encode_ch (enc, '"'); |
|
|
728 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
|
|
729 | encode_ch (enc, '"'); |
|
|
730 | encode_ch (enc, ')'); |
|
|
731 | encode_ch (enc, '['); |
|
|
732 | |
|
|
733 | while (count) |
|
|
734 | { |
|
|
735 | encode_sv (enc, SP[1 - count--]); |
|
|
736 | |
|
|
737 | if (count) |
|
|
738 | encode_ch (enc, ','); |
|
|
739 | } |
|
|
740 | |
|
|
741 | encode_ch (enc, ']'); |
|
|
742 | |
|
|
743 | PUTBACK; |
|
|
744 | |
|
|
745 | FREETMPS; LEAVE; |
|
|
746 | } |
|
|
747 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
|
|
748 | { |
|
|
749 | dSP; |
|
|
750 | |
|
|
751 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
752 | // we re-bless the reference to get overload and other niceties right |
|
|
753 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
754 | |
|
|
755 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
756 | PUTBACK; |
|
|
757 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
758 | SPAGAIN; |
|
|
759 | |
|
|
760 | // catch this surprisingly common error |
|
|
761 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
762 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
763 | |
|
|
764 | sv = POPs; |
|
|
765 | PUTBACK; |
|
|
766 | |
|
|
767 | encode_sv (enc, sv); |
|
|
768 | |
|
|
769 | FREETMPS; LEAVE; |
|
|
770 | } |
|
|
771 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
772 | encode_str (enc, "null", 4, 0); |
704 | else |
773 | else |
705 | { |
|
|
706 | #if 0 |
|
|
707 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
708 | { |
|
|
709 | // not yet |
|
|
710 | } |
|
|
711 | #endif |
|
|
712 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
713 | { |
|
|
714 | // we re-bless the reference to get overload and other niceties right |
|
|
715 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
|
|
716 | |
|
|
717 | if (to_json) |
|
|
718 | { |
|
|
719 | dSP; |
|
|
720 | |
|
|
721 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
722 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
723 | |
|
|
724 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
725 | PUTBACK; |
|
|
726 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
|
|
727 | SPAGAIN; |
|
|
728 | |
|
|
729 | // catch this surprisingly common error |
|
|
730 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
731 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
732 | |
|
|
733 | sv = POPs; |
|
|
734 | PUTBACK; |
|
|
735 | |
|
|
736 | encode_sv (enc, sv); |
|
|
737 | |
|
|
738 | FREETMPS; LEAVE; |
|
|
739 | } |
|
|
740 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
741 | encode_str (enc, "null", 4, 0); |
|
|
742 | else |
|
|
743 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
744 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
745 | } |
|
|
746 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
747 | encode_str (enc, "null", 4, 0); |
|
|
748 | else |
|
|
749 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
774 | croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)", |
750 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
775 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
751 | } |
|
|
752 | } |
776 | } |
753 | else if (svt == SVt_PVHV) |
777 | else if (svt == SVt_PVHV) |
754 | encode_hv (enc, (HV *)sv); |
778 | encode_hv (enc, (HV *)sv); |
755 | else if (svt == SVt_PVAV) |
779 | else if (svt == SVt_PVAV) |
756 | encode_av (enc, (AV *)sv); |
780 | encode_av (enc, (AV *)sv); |
… | |
… | |
1458 | DEC_DEC_DEPTH; |
1482 | DEC_DEC_DEPTH; |
1459 | return 0; |
1483 | return 0; |
1460 | } |
1484 | } |
1461 | |
1485 | |
1462 | static SV * |
1486 | static SV * |
|
|
1487 | decode_tag (dec_t *dec) |
|
|
1488 | { |
|
|
1489 | SV *tag = 0; |
|
|
1490 | SV *val = 0; |
|
|
1491 | |
|
|
1492 | if (!(dec->json.flags & F_ALLOW_TAGS)) |
|
|
1493 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
|
|
1494 | |
|
|
1495 | ++dec->cur; |
|
|
1496 | |
|
|
1497 | tag = decode_sv (dec); |
|
|
1498 | if (!tag) |
|
|
1499 | goto fail; |
|
|
1500 | |
|
|
1501 | if (!SvPOK (tag)) |
|
|
1502 | ERR ("malformed JSON string, (tag) must be a string"); |
|
|
1503 | |
|
|
1504 | if (*dec->cur != ')') |
|
|
1505 | ERR (") expected after tag"); |
|
|
1506 | |
|
|
1507 | ++dec->cur; |
|
|
1508 | |
|
|
1509 | val = decode_sv (dec); |
|
|
1510 | if (!val) |
|
|
1511 | goto fail; |
|
|
1512 | |
|
|
1513 | if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV) |
|
|
1514 | ERR ("malformed JSON string, tag value must be an array"); |
|
|
1515 | |
|
|
1516 | { |
|
|
1517 | AV *av = (AV *)SvRV (val); |
|
|
1518 | int i, len = av_len (av) + 1; |
|
|
1519 | HV *stash = gv_stashsv (tag, 0); |
|
|
1520 | SV *sv; |
|
|
1521 | |
|
|
1522 | if (!stash) |
|
|
1523 | ERR ("cannot decode perl-object (package does not exist)"); |
|
|
1524 | |
|
|
1525 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
|
|
1526 | |
|
|
1527 | if (!method) |
|
|
1528 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
|
|
1529 | |
|
|
1530 | dSP; |
|
|
1531 | |
|
|
1532 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1533 | EXTEND (SP, len + 2); |
|
|
1534 | // we re-bless the reference to get overload and other niceties right |
|
|
1535 | PUSHs (tag); |
|
|
1536 | PUSHs (sv_json); |
|
|
1537 | |
|
|
1538 | for (i = 0; i < len; ++i) |
|
|
1539 | PUSHs (*av_fetch (av, i, 1)); |
|
|
1540 | |
|
|
1541 | PUTBACK; |
|
|
1542 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
1543 | SPAGAIN; |
|
|
1544 | |
|
|
1545 | SvREFCNT_dec (tag); |
|
|
1546 | SvREFCNT_dec (val); |
|
|
1547 | sv = SvREFCNT_inc (POPs); |
|
|
1548 | |
|
|
1549 | PUTBACK; |
|
|
1550 | |
|
|
1551 | FREETMPS; LEAVE; |
|
|
1552 | |
|
|
1553 | return sv; |
|
|
1554 | } |
|
|
1555 | |
|
|
1556 | fail: |
|
|
1557 | SvREFCNT_dec (tag); |
|
|
1558 | SvREFCNT_dec (val); |
|
|
1559 | return 0; |
|
|
1560 | } |
|
|
1561 | |
|
|
1562 | static SV * |
1463 | decode_sv (dec_t *dec) |
1563 | decode_sv (dec_t *dec) |
1464 | { |
1564 | { |
1465 | // the beauty of JSON: you need exactly one character lookahead |
1565 | // the beauty of JSON: you need exactly one character lookahead |
1466 | // to parse everything. |
1566 | // to parse everything. |
1467 | switch (*dec->cur) |
1567 | switch (*dec->cur) |
1468 | { |
1568 | { |
1469 | case '"': ++dec->cur; return decode_str (dec); |
1569 | case '"': ++dec->cur; return decode_str (dec); |
1470 | case '[': ++dec->cur; return decode_av (dec); |
1570 | case '[': ++dec->cur; return decode_av (dec); |
1471 | case '{': ++dec->cur; return decode_hv (dec); |
1571 | case '{': ++dec->cur; return decode_hv (dec); |
|
|
1572 | case '(': return decode_tag (dec); |
1472 | |
1573 | |
1473 | case '-': |
1574 | case '-': |
1474 | case '0': case '1': case '2': case '3': case '4': |
1575 | case '0': case '1': case '2': case '3': case '4': |
1475 | case '5': case '6': case '7': case '8': case '9': |
1576 | case '5': case '6': case '7': case '8': case '9': |
1476 | return decode_num (dec); |
1577 | return decode_num (dec); |
… | |
… | |
1478 | case 't': |
1579 | case 't': |
1479 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1580 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1480 | { |
1581 | { |
1481 | dec->cur += 4; |
1582 | dec->cur += 4; |
1482 | #if JSON_SLOW |
1583 | #if JSON_SLOW |
1483 | json_true = get_bool ("JSON::XS::true"); |
1584 | types_true = get_bool ("Types::Serialiser::true"); |
1484 | #endif |
1585 | #endif |
1485 | return newSVsv (json_true); |
1586 | return newSVsv (types_true); |
1486 | } |
1587 | } |
1487 | else |
1588 | else |
1488 | ERR ("'true' expected"); |
1589 | ERR ("'true' expected"); |
1489 | |
1590 | |
1490 | break; |
1591 | break; |
… | |
… | |
1492 | case 'f': |
1593 | case 'f': |
1493 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1594 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1494 | { |
1595 | { |
1495 | dec->cur += 5; |
1596 | dec->cur += 5; |
1496 | #if JSON_SLOW |
1597 | #if JSON_SLOW |
1497 | json_false = get_bool ("JSON::XS::false"); |
1598 | types_false = get_bool ("Types::Serialiser::false"); |
1498 | #endif |
1599 | #endif |
1499 | return newSVsv (json_false); |
1600 | return newSVsv (types_false); |
1500 | } |
1601 | } |
1501 | else |
1602 | else |
1502 | ERR ("'false' expected"); |
1603 | ERR ("'false' expected"); |
1503 | |
1604 | |
1504 | break; |
1605 | break; |
… | |
… | |
1513 | ERR ("'null' expected"); |
1614 | ERR ("'null' expected"); |
1514 | |
1615 | |
1515 | break; |
1616 | break; |
1516 | |
1617 | |
1517 | default: |
1618 | default: |
1518 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1619 | ERR ("malformed JSON string, neither tag, array, object, number, string or atom"); |
1519 | break; |
1620 | break; |
1520 | } |
1621 | } |
1521 | |
1622 | |
1522 | fail: |
1623 | fail: |
1523 | return 0; |
1624 | return 0; |
… | |
… | |
1750 | self->incr_mode = INCR_M_STR; |
1851 | self->incr_mode = INCR_M_STR; |
1751 | goto incr_m_str; |
1852 | goto incr_m_str; |
1752 | |
1853 | |
1753 | case '[': |
1854 | case '[': |
1754 | case '{': |
1855 | case '{': |
|
|
1856 | case '(': |
1755 | if (++self->incr_nest > self->max_depth) |
1857 | if (++self->incr_nest > self->max_depth) |
1756 | croak (ERR_NESTING_EXCEEDED); |
1858 | croak (ERR_NESTING_EXCEEDED); |
1757 | break; |
1859 | break; |
1758 | |
1860 | |
1759 | case ']': |
1861 | case ']': |
1760 | case '}': |
1862 | case '}': |
1761 | if (--self->incr_nest <= 0) |
1863 | if (--self->incr_nest <= 0) |
1762 | goto interrupt; |
1864 | goto interrupt; |
|
|
1865 | break; |
|
|
1866 | |
|
|
1867 | case ')': |
|
|
1868 | --self->incr_nest; |
1763 | break; |
1869 | break; |
1764 | |
1870 | |
1765 | case '#': |
1871 | case '#': |
1766 | self->incr_mode = INCR_M_C1; |
1872 | self->incr_mode = INCR_M_C1; |
1767 | goto incr_m_c; |
1873 | goto incr_m_c; |
… | |
… | |
1793 | i >= '0' && i <= '9' ? i - '0' |
1899 | i >= '0' && i <= '9' ? i - '0' |
1794 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1900 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1795 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1901 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1796 | : -1; |
1902 | : -1; |
1797 | |
1903 | |
1798 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1904 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1799 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1905 | types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1800 | |
1906 | |
1801 | json_true = get_bool ("JSON::XS::true"); |
1907 | types_true = get_bool ("Types::Serialiser::true"); |
1802 | json_false = get_bool ("JSON::XS::false"); |
1908 | types_false = get_bool ("Types::Serialiser::false"); |
|
|
1909 | |
|
|
1910 | sv_json = newSVpv ("JSON", 0); |
|
|
1911 | SvREADONLY_on (sv_json); |
1803 | |
1912 | |
1804 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1913 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1805 | } |
1914 | } |
1806 | |
1915 | |
1807 | PROTOTYPES: DISABLE |
1916 | PROTOTYPES: DISABLE |
1808 | |
1917 | |
1809 | void CLONE (...) |
1918 | void CLONE (...) |
1810 | CODE: |
1919 | CODE: |
1811 | json_stash = 0; |
1920 | json_stash = 0; |
1812 | json_boolean_stash = 0; |
1921 | types_boolean_stash = 0; |
1813 | |
1922 | |
1814 | void new (char *klass) |
1923 | void new (char *klass) |
1815 | PPCODE: |
1924 | PPCODE: |
1816 | { |
1925 | { |
1817 | SV *pv = NEWSV (0, sizeof (JSON)); |
1926 | SV *pv = NEWSV (0, sizeof (JSON)); |
… | |
… | |
1837 | shrink = F_SHRINK |
1946 | shrink = F_SHRINK |
1838 | allow_blessed = F_ALLOW_BLESSED |
1947 | allow_blessed = F_ALLOW_BLESSED |
1839 | convert_blessed = F_CONV_BLESSED |
1948 | convert_blessed = F_CONV_BLESSED |
1840 | relaxed = F_RELAXED |
1949 | relaxed = F_RELAXED |
1841 | allow_unknown = F_ALLOW_UNKNOWN |
1950 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
1951 | allow_tags = F_ALLOW_TAGS |
1842 | PPCODE: |
1952 | PPCODE: |
1843 | { |
1953 | { |
1844 | if (enable) |
1954 | if (enable) |
1845 | self->flags |= ix; |
1955 | self->flags |= ix; |
1846 | else |
1956 | else |
… | |
… | |
1862 | get_shrink = F_SHRINK |
1972 | get_shrink = F_SHRINK |
1863 | get_allow_blessed = F_ALLOW_BLESSED |
1973 | get_allow_blessed = F_ALLOW_BLESSED |
1864 | get_convert_blessed = F_CONV_BLESSED |
1974 | get_convert_blessed = F_CONV_BLESSED |
1865 | get_relaxed = F_RELAXED |
1975 | get_relaxed = F_RELAXED |
1866 | get_allow_unknown = F_ALLOW_UNKNOWN |
1976 | get_allow_unknown = F_ALLOW_UNKNOWN |
|
|
1977 | get_allow_tags = F_ALLOW_TAGS |
1867 | PPCODE: |
1978 | PPCODE: |
1868 | XPUSHs (boolSV (self->flags & ix)); |
1979 | XPUSHs (boolSV (self->flags & ix)); |
1869 | |
1980 | |
1870 | void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
1981 | void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
1871 | PPCODE: |
1982 | PPCODE: |