… | |
… | |
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 |
… | |
… | |
125 | } JSON; |
127 | } JSON; |
126 | |
128 | |
127 | INLINE void |
129 | INLINE void |
128 | json_init (JSON *json) |
130 | json_init (JSON *json) |
129 | { |
131 | { |
130 | Zero (json, 1, JSON); |
132 | static const JSON init = { F_ALLOW_NONREF, 512 }; |
131 | json->max_depth = 512; |
133 | |
|
|
134 | *json = init; |
132 | } |
135 | } |
133 | |
136 | |
134 | ///////////////////////////////////////////////////////////////////////////// |
137 | ///////////////////////////////////////////////////////////////////////////// |
135 | // utility functions |
138 | // utility functions |
136 | |
139 | |
… | |
… | |
1501 | |
1504 | |
1502 | DEC_DEC_DEPTH; |
1505 | DEC_DEC_DEPTH; |
1503 | sv = newRV_noinc ((SV *)hv); |
1506 | sv = newRV_noinc ((SV *)hv); |
1504 | |
1507 | |
1505 | // check filter callbacks |
1508 | // check filter callbacks |
1506 | if (dec->json.flags & F_HOOK) |
1509 | if (expect_false (dec->json.flags & F_HOOK)) |
1507 | { |
1510 | { |
1508 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
1511 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
1509 | { |
1512 | { |
1510 | HE *cb, *he; |
1513 | HE *cb, *he; |
1511 | |
1514 | |
… | |
… | |
1553 | XPUSHs (sv_2mortal (sv)); |
1556 | XPUSHs (sv_2mortal (sv)); |
1554 | |
1557 | |
1555 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1558 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1556 | |
1559 | |
1557 | if (count == 1) |
1560 | if (count == 1) |
1558 | { |
|
|
1559 | sv = newSVsv (POPs); |
1561 | sv = newSVsv (POPs); |
1560 | FREETMPS; LEAVE; |
|
|
1561 | return sv; |
|
|
1562 | } |
|
|
1563 | else if (count) |
1562 | else if (count == 0) |
|
|
1563 | SvREFCNT_inc (sv); |
|
|
1564 | else |
1564 | croak ("filter_json_object callbacks must not return more than one scalar"); |
1565 | croak ("filter_json_object callbacks must not return more than one scalar"); |
1565 | |
1566 | |
1566 | SvREFCNT_inc (sv); |
|
|
1567 | FREETMPS; LEAVE; |
1567 | FREETMPS; LEAVE; |
1568 | } |
1568 | } |
1569 | } |
1569 | } |
1570 | |
1570 | |
1571 | return sv; |
1571 | return sv; |
… | |
… | |
1794 | else if (sv) |
1794 | else if (sv) |
1795 | { |
1795 | { |
1796 | // check for trailing garbage |
1796 | // check for trailing garbage |
1797 | decode_ws (&dec); |
1797 | decode_ws (&dec); |
1798 | |
1798 | |
1799 | if (*dec.cur) |
1799 | if (dec.cur != dec.end) |
1800 | { |
1800 | { |
1801 | dec.err = "garbage after JSON object"; |
1801 | dec.err = "garbage after JSON object"; |
1802 | SvREFCNT_dec (sv); |
1802 | SvREFCNT_dec (sv); |
1803 | sv = 0; |
1803 | sv = 0; |
1804 | } |
1804 | } |
… | |
… | |
1842 | // the state machine here is a bit convoluted and could be simplified a lot |
1842 | // the state machine here is a bit convoluted and could be simplified a lot |
1843 | // but this would make it slower, so... |
1843 | // but this would make it slower, so... |
1844 | |
1844 | |
1845 | for (;;) |
1845 | for (;;) |
1846 | { |
1846 | { |
1847 | //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 |
|
|
1848 | switch (self->incr_mode) |
1847 | switch (self->incr_mode) |
1849 | { |
1848 | { |
|
|
1849 | // reached end of a scalar, see if we are inside a nested structure or not |
|
|
1850 | end_of_scalar: |
|
|
1851 | self->incr_mode = INCR_M_JSON; |
|
|
1852 | |
|
|
1853 | if (self->incr_nest) // end of a scalar inside array, object or tag |
|
|
1854 | goto incr_m_json; |
|
|
1855 | else // end of scalar outside structure, json text ends here |
|
|
1856 | goto interrupt; |
|
|
1857 | |
1850 | // only used for initial whitespace skipping |
1858 | // only used for initial whitespace skipping |
1851 | case INCR_M_WS: |
1859 | case INCR_M_WS: |
1852 | for (;;) |
1860 | for (;;) |
1853 | { |
1861 | { |
1854 | if (*p > 0x20) |
1862 | if (*p > 0x20) |
… | |
… | |
1896 | ++p; |
1904 | ++p; |
1897 | } |
1905 | } |
1898 | |
1906 | |
1899 | break; |
1907 | break; |
1900 | |
1908 | |
|
|
1909 | // inside true/false/null |
|
|
1910 | case INCR_M_TFN: |
|
|
1911 | incr_m_tfn: |
|
|
1912 | for (;;) |
|
|
1913 | switch (*p++) |
|
|
1914 | { |
|
|
1915 | case 'r': case 'u': case 'e': // tRUE, falsE, nUll |
|
|
1916 | case 'a': case 'l': case 's': // fALSe, nuLL |
|
|
1917 | // allowed |
|
|
1918 | break; |
|
|
1919 | |
|
|
1920 | default: |
|
|
1921 | --p; |
|
|
1922 | goto end_of_scalar; |
|
|
1923 | } |
|
|
1924 | |
|
|
1925 | // inside a number |
|
|
1926 | case INCR_M_NUM: |
|
|
1927 | incr_m_num: |
|
|
1928 | for (;;) |
|
|
1929 | switch (*p++) |
|
|
1930 | { |
|
|
1931 | case 'e': case 'E': case '.': case '+': |
|
|
1932 | case '-': |
|
|
1933 | case '0': case '1': case '2': case '3': case '4': |
|
|
1934 | case '5': case '6': case '7': case '8': case '9': |
|
|
1935 | // allowed |
|
|
1936 | break; |
|
|
1937 | |
|
|
1938 | default: |
|
|
1939 | --p; |
|
|
1940 | goto end_of_scalar; |
|
|
1941 | } |
|
|
1942 | |
1901 | // inside a string |
1943 | // inside a string |
1902 | case INCR_M_STR: |
1944 | case INCR_M_STR: |
1903 | incr_m_str: |
1945 | incr_m_str: |
1904 | for (;;) |
1946 | for (;;) |
1905 | { |
1947 | { |
1906 | if (*p == '"') |
1948 | if (*p == '"') |
1907 | { |
1949 | { |
1908 | ++p; |
1950 | ++p; |
1909 | self->incr_mode = INCR_M_JSON; |
|
|
1910 | |
|
|
1911 | if (!self->incr_nest) |
|
|
1912 | goto interrupt; |
|
|
1913 | |
|
|
1914 | goto incr_m_json; |
1951 | goto end_of_scalar; |
1915 | } |
1952 | } |
1916 | else if (*p == '\\') |
1953 | else if (*p == '\\') |
1917 | { |
1954 | { |
1918 | ++p; // "virtually" consumes character after \ |
1955 | ++p; // "virtually" consumes character after \ |
1919 | |
1956 | |
… | |
… | |
1948 | { |
1985 | { |
1949 | --p; // do not eat the whitespace, let the next round do it |
1986 | --p; // do not eat the whitespace, let the next round do it |
1950 | goto interrupt; |
1987 | goto interrupt; |
1951 | } |
1988 | } |
1952 | break; |
1989 | break; |
|
|
1990 | |
|
|
1991 | // the following three blocks handle scalars. this makes the parser |
|
|
1992 | // more strict than required inside arrays or objects, and could |
|
|
1993 | // be moved to a special case on the toplevel (except strings) |
|
|
1994 | case 't': |
|
|
1995 | case 'f': |
|
|
1996 | case 'n': |
|
|
1997 | self->incr_mode = INCR_M_TFN; |
|
|
1998 | goto incr_m_tfn; |
|
|
1999 | |
|
|
2000 | case '-': |
|
|
2001 | case '0': case '1': case '2': case '3': case '4': |
|
|
2002 | case '5': case '6': case '7': case '8': case '9': |
|
|
2003 | self->incr_mode = INCR_M_NUM; |
|
|
2004 | goto incr_m_num; |
1953 | |
2005 | |
1954 | case '"': |
2006 | case '"': |
1955 | self->incr_mode = INCR_M_STR; |
2007 | self->incr_mode = INCR_M_STR; |
1956 | goto incr_m_str; |
2008 | goto incr_m_str; |
1957 | |
2009 | |
… | |
… | |
2154 | XPUSHs (ST (0)); |
2206 | XPUSHs (ST (0)); |
2155 | } |
2207 | } |
2156 | |
2208 | |
2157 | void encode (JSON *self, SV *scalar) |
2209 | void encode (JSON *self, SV *scalar) |
2158 | PPCODE: |
2210 | PPCODE: |
2159 | PUTBACK; scalar = encode_json (scalar, self); SPAGAIN; |
2211 | PUTBACK; XPUSHs (encode_json (scalar, self)); |
2160 | XPUSHs (scalar); |
|
|
2161 | |
2212 | |
2162 | void decode (JSON *self, SV *jsonstr) |
2213 | void decode (JSON *self, SV *jsonstr) |
2163 | PPCODE: |
2214 | PPCODE: |
2164 | PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN; |
2215 | PUTBACK; XPUSHs (decode_json (jsonstr, self, 0)); |
2165 | XPUSHs (jsonstr); |
|
|
2166 | |
2216 | |
2167 | void decode_prefix (JSON *self, SV *jsonstr) |
2217 | void decode_prefix (JSON *self, SV *jsonstr) |
2168 | PPCODE: |
2218 | PPCODE: |
2169 | { |
2219 | { |
2170 | SV *sv; |
2220 | SV *sv; |
2171 | STRLEN offset; |
2221 | STRLEN offset; |
2172 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
2222 | PUTBACK; sv = decode_json (jsonstr, self, &offset); |
2173 | EXTEND (SP, 2); |
2223 | EXTEND (SP, 2); |
2174 | PUSHs (sv); |
2224 | PUSHs (sv); |
2175 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, SvPV_nolen (jsonstr) + offset)))); |
2225 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, SvPV_nolen (jsonstr) + offset)))); |
2176 | } |
2226 | } |
2177 | |
2227 | |
… | |
… | |
2250 | |
2300 | |
2251 | break; |
2301 | break; |
2252 | } |
2302 | } |
2253 | } |
2303 | } |
2254 | |
2304 | |
2255 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
2305 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); |
2256 | XPUSHs (sv); |
2306 | XPUSHs (sv); |
2257 | |
2307 | |
2258 | self->incr_pos -= offset; |
2308 | self->incr_pos -= offset; |
2259 | self->incr_nest = 0; |
2309 | self->incr_nest = 0; |
2260 | self->incr_mode = 0; |
2310 | self->incr_mode = 0; |
… | |
… | |
2312 | PPCODE: |
2362 | PPCODE: |
2313 | { |
2363 | { |
2314 | JSON json; |
2364 | JSON json; |
2315 | json_init (&json); |
2365 | json_init (&json); |
2316 | json.flags |= F_UTF8; |
2366 | json.flags |= F_UTF8; |
2317 | PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; |
2367 | PUTBACK; XPUSHs (encode_json (scalar, &json)); |
2318 | XPUSHs (scalar); |
|
|
2319 | } |
2368 | } |
2320 | |
2369 | |
2321 | void decode_json (SV *jsonstr) |
2370 | void decode_json (SV *jsonstr) |
2322 | PPCODE: |
2371 | PPCODE: |
2323 | { |
2372 | { |
2324 | JSON json; |
2373 | JSON json; |
2325 | json_init (&json); |
2374 | json_init (&json); |
2326 | json.flags |= F_UTF8; |
2375 | json.flags |= F_UTF8; |
2327 | PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; |
2376 | PUTBACK; XPUSHs (decode_json (jsonstr, &json, 0)); |
2328 | XPUSHs (jsonstr); |
|
|
2329 | } |
2377 | } |
2330 | |
2378 | |