… | |
… | |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
43 | #define INDENT_STEP 3 // spaces per indentation level |
43 | #define INDENT_STEP 3 // spaces per indentation level |
44 | |
44 | |
45 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
45 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
46 | |
46 | |
|
|
47 | #define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8) |
|
|
48 | |
47 | #define SB do { |
49 | #define SB do { |
48 | #define SE } while (0) |
50 | #define SE } while (0) |
49 | |
51 | |
50 | #if __GNUC__ >= 3 |
52 | #if __GNUC__ >= 3 |
51 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
53 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
… | |
… | |
258 | } |
260 | } |
259 | |
261 | |
260 | // this relies greatly on the quality of the pow () |
262 | // this relies greatly on the quality of the pow () |
261 | // implementation of the platform, but a good |
263 | // implementation of the platform, but a good |
262 | // implementation is hard to beat. |
264 | // implementation is hard to beat. |
|
|
265 | // (IEEE 754 conformant ones are required to be exact) |
263 | if (postdp) *expo -= eaccum; |
266 | if (postdp) *expo -= eaccum; |
264 | *accum += uaccum * Perl_pow (10., *expo); |
267 | *accum += uaccum * Perl_pow (10., *expo); |
265 | *expo += eaccum; |
268 | *expo += eaccum; |
266 | } |
269 | } |
267 | |
270 | |
… | |
… | |
822 | } |
825 | } |
823 | else |
826 | else |
824 | { |
827 | { |
825 | // large integer, use the (rather slow) snprintf way. |
828 | // large integer, use the (rather slow) snprintf way. |
826 | need (enc, IVUV_MAXCHARS); |
829 | need (enc, IVUV_MAXCHARS); |
827 | enc->cur += |
830 | enc->cur += |
828 | SvIsUV(sv) |
831 | SvIsUV(sv) |
829 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
832 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
830 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
833 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
831 | } |
834 | } |
832 | } |
835 | } |
… | |
… | |
834 | encode_rv (enc, SvRV (sv)); |
837 | encode_rv (enc, SvRV (sv)); |
835 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
838 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
836 | encode_str (enc, "null", 4, 0); |
839 | encode_str (enc, "null", 4, 0); |
837 | else |
840 | else |
838 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
841 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
839 | SvPV_nolen (sv), SvFLAGS (sv)); |
842 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
840 | } |
843 | } |
841 | |
844 | |
842 | static SV * |
845 | static SV * |
843 | encode_json (SV *scalar, JSON *json) |
846 | encode_json (SV *scalar, JSON *json) |
844 | { |
847 | { |
… | |
… | |
1401 | dSP; |
1404 | dSP; |
1402 | int count; |
1405 | int count; |
1403 | |
1406 | |
1404 | ENTER; SAVETMPS; PUSHMARK (SP); |
1407 | ENTER; SAVETMPS; PUSHMARK (SP); |
1405 | XPUSHs (HeVAL (he)); |
1408 | XPUSHs (HeVAL (he)); |
|
|
1409 | sv_2mortal (sv); |
1406 | |
1410 | |
1407 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1411 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1408 | |
1412 | |
1409 | if (count == 1) |
1413 | if (count == 1) |
1410 | { |
1414 | { |
1411 | sv = newSVsv (POPs); |
1415 | sv = newSVsv (POPs); |
1412 | FREETMPS; LEAVE; |
1416 | FREETMPS; LEAVE; |
1413 | return sv; |
1417 | return sv; |
1414 | } |
1418 | } |
1415 | |
1419 | |
|
|
1420 | SvREFCNT_inc (sv); |
1416 | FREETMPS; LEAVE; |
1421 | FREETMPS; LEAVE; |
1417 | } |
1422 | } |
1418 | } |
1423 | } |
1419 | |
1424 | |
1420 | if (dec->json.cb_object) |
1425 | if (dec->json.cb_object) |
… | |
… | |
1516 | { |
1521 | { |
1517 | dec_t dec; |
1522 | dec_t dec; |
1518 | SV *sv; |
1523 | SV *sv; |
1519 | |
1524 | |
1520 | /* work around bugs in 5.10 where manipulating magic values |
1525 | /* work around bugs in 5.10 where manipulating magic values |
1521 | * will perl ignore the magic in subsequent accesses |
1526 | * will perl ignore the magic in subsequent accesses. |
|
|
1527 | * also make a copy of non-PV values, to get them into a clean |
|
|
1528 | * state (SvPV should do that, but it's buggy, see below). |
1522 | */ |
1529 | */ |
1523 | /*SvGETMAGIC (string);*/ |
1530 | /*SvGETMAGIC (string);*/ |
1524 | if (SvMAGICAL (string)) |
1531 | if (SvMAGICAL (string) || !SvPOK (string)) |
1525 | string = sv_2mortal (newSVsv (string)); |
1532 | string = sv_2mortal (newSVsv (string)); |
1526 | |
1533 | |
1527 | SvUPGRADE (string, SVt_PV); |
1534 | SvUPGRADE (string, SVt_PV); |
1528 | |
1535 | |
1529 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1536 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
… | |
… | |
1546 | if (offset > json->max_size && json->max_size) |
1553 | if (offset > json->max_size && json->max_size) |
1547 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1554 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1548 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1555 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1549 | } |
1556 | } |
1550 | |
1557 | |
1551 | if (json->flags & F_UTF8) |
1558 | if (DECODE_WANTS_OCTETS (json)) |
1552 | sv_utf8_downgrade (string, 0); |
1559 | sv_utf8_downgrade (string, 0); |
1553 | else |
1560 | else |
1554 | sv_utf8_upgrade (string); |
1561 | sv_utf8_upgrade (string); |
1555 | |
1562 | |
1556 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1563 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
… | |
… | |
1598 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1605 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1599 | LEAVE; |
1606 | LEAVE; |
1600 | |
1607 | |
1601 | croak ("%s, at character offset %d (before \"%s\")", |
1608 | croak ("%s, at character offset %d (before \"%s\")", |
1602 | dec.err, |
1609 | dec.err, |
1603 | ptr_to_index (string, dec.cur), |
1610 | (int)ptr_to_index (string, dec.cur), |
1604 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1611 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1605 | } |
1612 | } |
1606 | |
1613 | |
1607 | sv = sv_2mortal (sv); |
1614 | sv = sv_2mortal (sv); |
1608 | |
1615 | |
… | |
… | |
1885 | } |
1892 | } |
1886 | |
1893 | |
1887 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1894 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1888 | PPCODE: |
1895 | PPCODE: |
1889 | { |
1896 | { |
1890 | if (!self->cb_sk_object) |
1897 | if (!self->cb_sk_object) |
1891 | self->cb_sk_object = newHV (); |
1898 | self->cb_sk_object = newHV (); |
1892 | |
1899 | |
1893 | if (SvOK (cb)) |
1900 | if (SvOK (cb)) |
1894 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1901 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1895 | else |
1902 | else |
… | |
… | |
1906 | XPUSHs (ST (0)); |
1913 | XPUSHs (ST (0)); |
1907 | } |
1914 | } |
1908 | |
1915 | |
1909 | void encode (JSON *self, SV *scalar) |
1916 | void encode (JSON *self, SV *scalar) |
1910 | PPCODE: |
1917 | PPCODE: |
1911 | XPUSHs (encode_json (scalar, self)); |
1918 | PUTBACK; scalar = encode_json (scalar, self); SPAGAIN; |
|
|
1919 | XPUSHs (scalar); |
1912 | |
1920 | |
1913 | void decode (JSON *self, SV *jsonstr) |
1921 | void decode (JSON *self, SV *jsonstr) |
1914 | PPCODE: |
1922 | PPCODE: |
1915 | XPUSHs (decode_json (jsonstr, self, 0)); |
1923 | PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN; |
|
|
1924 | XPUSHs (jsonstr); |
1916 | |
1925 | |
1917 | void decode_prefix (JSON *self, SV *jsonstr) |
1926 | void decode_prefix (JSON *self, SV *jsonstr) |
1918 | PPCODE: |
1927 | PPCODE: |
1919 | { |
1928 | { |
|
|
1929 | SV *sv; |
1920 | char *offset; |
1930 | char *offset; |
|
|
1931 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
1921 | EXTEND (SP, 2); |
1932 | EXTEND (SP, 2); |
1922 | PUSHs (decode_json (jsonstr, self, &offset)); |
1933 | PUSHs (sv); |
1923 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1934 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1924 | } |
1935 | } |
1925 | |
1936 | |
1926 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1937 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1927 | PPCODE: |
1938 | PPCODE: |
1928 | { |
1939 | { |
1929 | if (!self->incr_text) |
1940 | if (!self->incr_text) |
1930 | self->incr_text = newSVpvn ("", 0); |
1941 | self->incr_text = newSVpvn ("", 0); |
|
|
1942 | |
|
|
1943 | /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */ |
|
|
1944 | if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) |
|
|
1945 | if (DECODE_WANTS_OCTETS (self)) |
|
|
1946 | { |
|
|
1947 | if (self->incr_pos) |
|
|
1948 | self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text), |
|
|
1949 | (U8 *)SvPVX (self->incr_text) + self->incr_pos); |
|
|
1950 | |
|
|
1951 | sv_utf8_downgrade (self->incr_text, 0); |
|
|
1952 | } |
|
|
1953 | else |
|
|
1954 | { |
|
|
1955 | sv_utf8_upgrade (self->incr_text); |
|
|
1956 | |
|
|
1957 | if (self->incr_pos) |
|
|
1958 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1959 | - (U8 *)SvPVX (self->incr_text); |
|
|
1960 | } |
1931 | |
1961 | |
1932 | // append data, if any |
1962 | // append data, if any |
1933 | if (jsonstr) |
1963 | if (jsonstr) |
1934 | { |
1964 | { |
|
|
1965 | /* make sure both strings have same encoding */ |
|
|
1966 | if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) |
1935 | if (SvUTF8 (jsonstr)) |
1967 | if (SvUTF8 (jsonstr)) |
|
|
1968 | sv_utf8_downgrade (jsonstr, 0); |
1936 | { |
1969 | else |
1937 | if (!SvUTF8 (self->incr_text)) |
|
|
1938 | { |
|
|
1939 | /* utf-8-ness differs, need to upgrade */ |
|
|
1940 | sv_utf8_upgrade (self->incr_text); |
|
|
1941 | |
|
|
1942 | if (self->incr_pos) |
|
|
1943 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1944 | - (U8 *)SvPVX (self->incr_text); |
|
|
1945 | } |
|
|
1946 | } |
|
|
1947 | else if (SvUTF8 (self->incr_text)) |
|
|
1948 | sv_utf8_upgrade (jsonstr); |
1970 | sv_utf8_upgrade (jsonstr); |
1949 | |
1971 | |
|
|
1972 | /* and then just blindly append */ |
1950 | { |
1973 | { |
1951 | STRLEN len; |
1974 | STRLEN len; |
1952 | const char *str = SvPV (jsonstr, len); |
1975 | const char *str = SvPV (jsonstr, len); |
1953 | STRLEN cur = SvCUR (self->incr_text); |
1976 | STRLEN cur = SvCUR (self->incr_text); |
1954 | |
1977 | |
… | |
… | |
1962 | } |
1985 | } |
1963 | |
1986 | |
1964 | if (GIMME_V != G_VOID) |
1987 | if (GIMME_V != G_VOID) |
1965 | do |
1988 | do |
1966 | { |
1989 | { |
|
|
1990 | SV *sv; |
1967 | char *offset; |
1991 | char *offset; |
1968 | |
1992 | |
1969 | if (!INCR_DONE (self)) |
1993 | if (!INCR_DONE (self)) |
1970 | { |
1994 | { |
1971 | incr_parse (self); |
1995 | incr_parse (self); |
… | |
… | |
1973 | if (self->incr_pos > self->max_size && self->max_size) |
1997 | if (self->incr_pos > self->max_size && self->max_size) |
1974 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1998 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1975 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1999 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1976 | |
2000 | |
1977 | if (!INCR_DONE (self)) |
2001 | if (!INCR_DONE (self)) |
|
|
2002 | { |
|
|
2003 | // as an optimisation, do not accumulate white space in the incr buffer |
|
|
2004 | if (self->incr_mode == INCR_M_WS && self->incr_pos) |
|
|
2005 | { |
|
|
2006 | self->incr_pos = 0; |
|
|
2007 | SvCUR_set (self->incr_text, 0); |
|
|
2008 | } |
|
|
2009 | |
1978 | break; |
2010 | break; |
|
|
2011 | } |
1979 | } |
2012 | } |
1980 | |
2013 | |
1981 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
2014 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
|
|
2015 | XPUSHs (sv); |
1982 | |
2016 | |
1983 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2017 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1984 | self->incr_nest = 0; |
2018 | self->incr_nest = 0; |
1985 | self->incr_mode = 0; |
2019 | self->incr_mode = 0; |
1986 | |
2020 | |
… | |
… | |
2038 | PPCODE: |
2072 | PPCODE: |
2039 | { |
2073 | { |
2040 | JSON json; |
2074 | JSON json; |
2041 | json_init (&json); |
2075 | json_init (&json); |
2042 | json.flags |= ix; |
2076 | json.flags |= ix; |
2043 | XPUSHs (encode_json (scalar, &json)); |
2077 | PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; |
|
|
2078 | XPUSHs (scalar); |
2044 | } |
2079 | } |
2045 | |
2080 | |
2046 | void decode_json (SV *jsonstr) |
2081 | void decode_json (SV *jsonstr) |
2047 | ALIAS: |
2082 | ALIAS: |
2048 | from_json_ = 0 |
2083 | from_json_ = 0 |
… | |
… | |
2050 | PPCODE: |
2085 | PPCODE: |
2051 | { |
2086 | { |
2052 | JSON json; |
2087 | JSON json; |
2053 | json_init (&json); |
2088 | json_init (&json); |
2054 | json.flags |= ix; |
2089 | json.flags |= ix; |
2055 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2090 | PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; |
|
|
2091 | XPUSHs (jsonstr); |
2056 | } |
2092 | } |
2057 | |
2093 | |