… | |
… | |
96 | static SV *bool_false, *bool_true; |
96 | static SV *bool_false, *bool_true; |
97 | static SV *sv_json; |
97 | static SV *sv_json; |
98 | |
98 | |
99 | enum { |
99 | enum { |
100 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
100 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
|
|
101 | INCR_M_TFN, // inside true/false/null |
|
|
102 | INCR_M_NUM, // inside number |
101 | INCR_M_STR, // inside string |
103 | INCR_M_STR, // inside string |
102 | INCR_M_BS, // inside backslash |
104 | INCR_M_BS, // inside backslash |
103 | INCR_M_C0, // inside comment in initial whitespace sequence |
105 | INCR_M_C0, // inside comment in initial whitespace sequence |
104 | INCR_M_C1, // inside comment in other places |
106 | INCR_M_C1, // inside comment in other places |
105 | INCR_M_JSON // outside anything, count nesting |
107 | INCR_M_JSON // outside anything, count nesting |
… | |
… | |
824 | |
826 | |
825 | encode_sv (enc, TOPs); |
827 | encode_sv (enc, TOPs); |
826 | SP -= count; |
828 | SP -= count; |
827 | } |
829 | } |
828 | |
830 | |
|
|
831 | PUTBACK; |
|
|
832 | |
829 | encode_ch (enc, ']'); |
833 | encode_ch (enc, ']'); |
830 | |
834 | |
831 | FREETMPS; LEAVE; |
835 | FREETMPS; LEAVE; |
832 | } |
836 | } |
833 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
837 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
… | |
… | |
1502 | |
1506 | |
1503 | DEC_DEC_DEPTH; |
1507 | DEC_DEC_DEPTH; |
1504 | sv = newRV_noinc ((SV *)hv); |
1508 | sv = newRV_noinc ((SV *)hv); |
1505 | |
1509 | |
1506 | // check filter callbacks |
1510 | // check filter callbacks |
1507 | if (dec->json.flags & F_HOOK) |
1511 | if (expect_false (dec->json.flags & F_HOOK)) |
1508 | { |
1512 | { |
1509 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
1513 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
1510 | { |
1514 | { |
1511 | HE *cb, *he; |
1515 | HE *cb, *he; |
1512 | |
1516 | |
… | |
… | |
1531 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1535 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1532 | |
1536 | |
1533 | if (count == 1) |
1537 | if (count == 1) |
1534 | { |
1538 | { |
1535 | sv = newSVsv (POPs); |
1539 | sv = newSVsv (POPs); |
|
|
1540 | PUTBACK; |
1536 | FREETMPS; LEAVE; |
1541 | FREETMPS; LEAVE; |
1537 | return sv; |
1542 | return sv; |
1538 | } |
1543 | } |
1539 | else if (count) |
1544 | else if (count) |
1540 | croak ("filter_json_single_key_object callbacks must not return more than one scalar"); |
1545 | croak ("filter_json_single_key_object callbacks must not return more than one scalar"); |
1541 | |
1546 | |
|
|
1547 | PUTBACK; |
|
|
1548 | |
1542 | SvREFCNT_inc (sv); |
1549 | SvREFCNT_inc (sv); |
|
|
1550 | |
1543 | FREETMPS; LEAVE; |
1551 | FREETMPS; LEAVE; |
1544 | } |
1552 | } |
1545 | } |
1553 | } |
1546 | |
1554 | |
1547 | if (dec->json.cb_object) |
1555 | if (dec->json.cb_object) |
… | |
… | |
1554 | XPUSHs (sv_2mortal (sv)); |
1562 | XPUSHs (sv_2mortal (sv)); |
1555 | |
1563 | |
1556 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1564 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1557 | |
1565 | |
1558 | if (count == 1) |
1566 | if (count == 1) |
1559 | { |
|
|
1560 | sv = newSVsv (POPs); |
1567 | sv = newSVsv (POPs); |
1561 | FREETMPS; LEAVE; |
|
|
1562 | return sv; |
|
|
1563 | } |
|
|
1564 | else if (count) |
1568 | else if (count == 0) |
|
|
1569 | SvREFCNT_inc (sv); |
|
|
1570 | else |
1565 | croak ("filter_json_object callbacks must not return more than one scalar"); |
1571 | croak ("filter_json_object callbacks must not return more than one scalar"); |
1566 | |
1572 | |
1567 | SvREFCNT_inc (sv); |
1573 | PUTBACK; |
|
|
1574 | |
1568 | FREETMPS; LEAVE; |
1575 | FREETMPS; LEAVE; |
1569 | } |
1576 | } |
1570 | } |
1577 | } |
1571 | |
1578 | |
1572 | return sv; |
1579 | return sv; |
… | |
… | |
1843 | // the state machine here is a bit convoluted and could be simplified a lot |
1850 | // the state machine here is a bit convoluted and could be simplified a lot |
1844 | // but this would make it slower, so... |
1851 | // but this would make it slower, so... |
1845 | |
1852 | |
1846 | for (;;) |
1853 | for (;;) |
1847 | { |
1854 | { |
1848 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
|
|
1849 | switch (self->incr_mode) |
1855 | switch (self->incr_mode) |
1850 | { |
1856 | { |
|
|
1857 | // reached end of a scalar, see if we are inside a nested structure or not |
|
|
1858 | end_of_scalar: |
|
|
1859 | self->incr_mode = INCR_M_JSON; |
|
|
1860 | |
|
|
1861 | if (self->incr_nest) // end of a scalar inside array, object or tag |
|
|
1862 | goto incr_m_json; |
|
|
1863 | else // end of scalar outside structure, json text ends here |
|
|
1864 | goto interrupt; |
|
|
1865 | |
1851 | // only used for initial whitespace skipping |
1866 | // only used for initial whitespace skipping |
1852 | case INCR_M_WS: |
1867 | case INCR_M_WS: |
1853 | for (;;) |
1868 | for (;;) |
1854 | { |
1869 | { |
1855 | if (*p > 0x20) |
1870 | if (*p > 0x20) |
… | |
… | |
1897 | ++p; |
1912 | ++p; |
1898 | } |
1913 | } |
1899 | |
1914 | |
1900 | break; |
1915 | break; |
1901 | |
1916 | |
|
|
1917 | // inside true/false/null |
|
|
1918 | case INCR_M_TFN: |
|
|
1919 | incr_m_tfn: |
|
|
1920 | for (;;) |
|
|
1921 | switch (*p++) |
|
|
1922 | { |
|
|
1923 | case 'r': case 'u': case 'e': // tRUE, falsE, nUll |
|
|
1924 | case 'a': case 'l': case 's': // fALSe, nuLL |
|
|
1925 | // allowed |
|
|
1926 | break; |
|
|
1927 | |
|
|
1928 | default: |
|
|
1929 | --p; |
|
|
1930 | goto end_of_scalar; |
|
|
1931 | } |
|
|
1932 | |
|
|
1933 | // inside a number |
|
|
1934 | case INCR_M_NUM: |
|
|
1935 | incr_m_num: |
|
|
1936 | for (;;) |
|
|
1937 | switch (*p++) |
|
|
1938 | { |
|
|
1939 | case 'e': case 'E': case '.': case '+': |
|
|
1940 | case '-': |
|
|
1941 | case '0': case '1': case '2': case '3': case '4': |
|
|
1942 | case '5': case '6': case '7': case '8': case '9': |
|
|
1943 | // allowed |
|
|
1944 | break; |
|
|
1945 | |
|
|
1946 | default: |
|
|
1947 | --p; |
|
|
1948 | goto end_of_scalar; |
|
|
1949 | } |
|
|
1950 | |
1902 | // inside a string |
1951 | // inside a string |
1903 | case INCR_M_STR: |
1952 | case INCR_M_STR: |
1904 | incr_m_str: |
1953 | incr_m_str: |
1905 | for (;;) |
1954 | for (;;) |
1906 | { |
1955 | { |
1907 | if (*p == '"') |
1956 | if (*p == '"') |
1908 | { |
1957 | { |
1909 | ++p; |
1958 | ++p; |
1910 | self->incr_mode = INCR_M_JSON; |
|
|
1911 | |
|
|
1912 | if (!self->incr_nest) |
|
|
1913 | goto interrupt; |
|
|
1914 | |
|
|
1915 | goto incr_m_json; |
1959 | goto end_of_scalar; |
1916 | } |
1960 | } |
1917 | else if (*p == '\\') |
1961 | else if (*p == '\\') |
1918 | { |
1962 | { |
1919 | ++p; // "virtually" consumes character after \ |
1963 | ++p; // "virtually" consumes character after \ |
1920 | |
1964 | |
… | |
… | |
1949 | { |
1993 | { |
1950 | --p; // do not eat the whitespace, let the next round do it |
1994 | --p; // do not eat the whitespace, let the next round do it |
1951 | goto interrupt; |
1995 | goto interrupt; |
1952 | } |
1996 | } |
1953 | break; |
1997 | break; |
|
|
1998 | |
|
|
1999 | // the following three blocks handle scalars. this makes the parser |
|
|
2000 | // more strict than required inside arrays or objects, and could |
|
|
2001 | // be moved to a special case on the toplevel (except strings) |
|
|
2002 | case 't': |
|
|
2003 | case 'f': |
|
|
2004 | case 'n': |
|
|
2005 | self->incr_mode = INCR_M_TFN; |
|
|
2006 | goto incr_m_tfn; |
|
|
2007 | |
|
|
2008 | case '-': |
|
|
2009 | case '0': case '1': case '2': case '3': case '4': |
|
|
2010 | case '5': case '6': case '7': case '8': case '9': |
|
|
2011 | self->incr_mode = INCR_M_NUM; |
|
|
2012 | goto incr_m_num; |
1954 | |
2013 | |
1955 | case '"': |
2014 | case '"': |
1956 | self->incr_mode = INCR_M_STR; |
2015 | self->incr_mode = INCR_M_STR; |
1957 | goto incr_m_str; |
2016 | goto incr_m_str; |
1958 | |
2017 | |