… | |
… | |
260 | } |
260 | } |
261 | |
261 | |
262 | // this relies greatly on the quality of the pow () |
262 | // this relies greatly on the quality of the pow () |
263 | // implementation of the platform, but a good |
263 | // implementation of the platform, but a good |
264 | // implementation is hard to beat. |
264 | // implementation is hard to beat. |
|
|
265 | // (IEEE 754 conformant ones are required to be exact) |
265 | if (postdp) *expo -= eaccum; |
266 | if (postdp) *expo -= eaccum; |
266 | *accum += uaccum * Perl_pow (10., *expo); |
267 | *accum += uaccum * Perl_pow (10., *expo); |
267 | *expo += eaccum; |
268 | *expo += eaccum; |
268 | } |
269 | } |
269 | |
270 | |
… | |
… | |
824 | } |
825 | } |
825 | else |
826 | else |
826 | { |
827 | { |
827 | // large integer, use the (rather slow) snprintf way. |
828 | // large integer, use the (rather slow) snprintf way. |
828 | need (enc, IVUV_MAXCHARS); |
829 | need (enc, IVUV_MAXCHARS); |
829 | enc->cur += |
830 | enc->cur += |
830 | SvIsUV(sv) |
831 | SvIsUV(sv) |
831 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
832 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
832 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
833 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
833 | } |
834 | } |
834 | } |
835 | } |
… | |
… | |
836 | encode_rv (enc, SvRV (sv)); |
837 | encode_rv (enc, SvRV (sv)); |
837 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
838 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
838 | encode_str (enc, "null", 4, 0); |
839 | encode_str (enc, "null", 4, 0); |
839 | else |
840 | else |
840 | 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", |
841 | SvPV_nolen (sv), SvFLAGS (sv)); |
842 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
842 | } |
843 | } |
843 | |
844 | |
844 | static SV * |
845 | static SV * |
845 | encode_json (SV *scalar, JSON *json) |
846 | encode_json (SV *scalar, JSON *json) |
846 | { |
847 | { |
… | |
… | |
1520 | { |
1521 | { |
1521 | dec_t dec; |
1522 | dec_t dec; |
1522 | SV *sv; |
1523 | SV *sv; |
1523 | |
1524 | |
1524 | /* work around bugs in 5.10 where manipulating magic values |
1525 | /* work around bugs in 5.10 where manipulating magic values |
1525 | * 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). |
1526 | */ |
1529 | */ |
1527 | /*SvGETMAGIC (string);*/ |
1530 | /*SvGETMAGIC (string);*/ |
1528 | if (SvMAGICAL (string)) |
1531 | if (SvMAGICAL (string) || !SvPOK (string)) |
1529 | string = sv_2mortal (newSVsv (string)); |
1532 | string = sv_2mortal (newSVsv (string)); |
1530 | |
1533 | |
1531 | SvUPGRADE (string, SVt_PV); |
1534 | SvUPGRADE (string, SVt_PV); |
1532 | |
1535 | |
1533 | /* 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 |
… | |
… | |
1602 | 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); |
1603 | LEAVE; |
1606 | LEAVE; |
1604 | |
1607 | |
1605 | croak ("%s, at character offset %d (before \"%s\")", |
1608 | croak ("%s, at character offset %d (before \"%s\")", |
1606 | dec.err, |
1609 | dec.err, |
1607 | ptr_to_index (string, dec.cur), |
1610 | (int)ptr_to_index (string, dec.cur), |
1608 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1611 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1609 | } |
1612 | } |
1610 | |
1613 | |
1611 | sv = sv_2mortal (sv); |
1614 | sv = sv_2mortal (sv); |
1612 | |
1615 | |
… | |
… | |
1889 | } |
1892 | } |
1890 | |
1893 | |
1891 | 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) |
1892 | PPCODE: |
1895 | PPCODE: |
1893 | { |
1896 | { |
1894 | if (!self->cb_sk_object) |
1897 | if (!self->cb_sk_object) |
1895 | self->cb_sk_object = newHV (); |
1898 | self->cb_sk_object = newHV (); |
1896 | |
1899 | |
1897 | if (SvOK (cb)) |
1900 | if (SvOK (cb)) |
1898 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1901 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1899 | else |
1902 | else |
… | |
… | |
1910 | XPUSHs (ST (0)); |
1913 | XPUSHs (ST (0)); |
1911 | } |
1914 | } |
1912 | |
1915 | |
1913 | void encode (JSON *self, SV *scalar) |
1916 | void encode (JSON *self, SV *scalar) |
1914 | PPCODE: |
1917 | PPCODE: |
1915 | XPUSHs (encode_json (scalar, self)); |
1918 | PUTBACK; scalar = encode_json (scalar, self); SPAGAIN; |
|
|
1919 | XPUSHs (scalar); |
1916 | |
1920 | |
1917 | void decode (JSON *self, SV *jsonstr) |
1921 | void decode (JSON *self, SV *jsonstr) |
1918 | PPCODE: |
1922 | PPCODE: |
1919 | XPUSHs (decode_json (jsonstr, self, 0)); |
1923 | PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN; |
|
|
1924 | XPUSHs (jsonstr); |
1920 | |
1925 | |
1921 | void decode_prefix (JSON *self, SV *jsonstr) |
1926 | void decode_prefix (JSON *self, SV *jsonstr) |
1922 | PPCODE: |
1927 | PPCODE: |
1923 | { |
1928 | { |
|
|
1929 | SV *sv; |
1924 | char *offset; |
1930 | char *offset; |
|
|
1931 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
1925 | EXTEND (SP, 2); |
1932 | EXTEND (SP, 2); |
1926 | PUSHs (decode_json (jsonstr, self, &offset)); |
1933 | PUSHs (sv); |
1927 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1934 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1928 | } |
1935 | } |
1929 | |
1936 | |
1930 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1937 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1931 | PPCODE: |
1938 | PPCODE: |
… | |
… | |
1978 | } |
1985 | } |
1979 | |
1986 | |
1980 | if (GIMME_V != G_VOID) |
1987 | if (GIMME_V != G_VOID) |
1981 | do |
1988 | do |
1982 | { |
1989 | { |
|
|
1990 | SV *sv; |
1983 | char *offset; |
1991 | char *offset; |
1984 | |
1992 | |
1985 | if (!INCR_DONE (self)) |
1993 | if (!INCR_DONE (self)) |
1986 | { |
1994 | { |
1987 | incr_parse (self); |
1995 | incr_parse (self); |
… | |
… | |
1989 | if (self->incr_pos > self->max_size && self->max_size) |
1997 | if (self->incr_pos > self->max_size && self->max_size) |
1990 | 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", |
1991 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1999 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1992 | |
2000 | |
1993 | 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 | |
1994 | break; |
2010 | break; |
|
|
2011 | } |
1995 | } |
2012 | } |
1996 | |
2013 | |
1997 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
2014 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
|
|
2015 | XPUSHs (sv); |
1998 | |
2016 | |
1999 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2017 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2000 | self->incr_nest = 0; |
2018 | self->incr_nest = 0; |
2001 | self->incr_mode = 0; |
2019 | self->incr_mode = 0; |
2002 | |
2020 | |
… | |
… | |
2054 | PPCODE: |
2072 | PPCODE: |
2055 | { |
2073 | { |
2056 | JSON json; |
2074 | JSON json; |
2057 | json_init (&json); |
2075 | json_init (&json); |
2058 | json.flags |= ix; |
2076 | json.flags |= ix; |
2059 | XPUSHs (encode_json (scalar, &json)); |
2077 | PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; |
|
|
2078 | XPUSHs (scalar); |
2060 | } |
2079 | } |
2061 | |
2080 | |
2062 | void decode_json (SV *jsonstr) |
2081 | void decode_json (SV *jsonstr) |
2063 | ALIAS: |
2082 | ALIAS: |
2064 | from_json_ = 0 |
2083 | from_json_ = 0 |
… | |
… | |
2066 | PPCODE: |
2085 | PPCODE: |
2067 | { |
2086 | { |
2068 | JSON json; |
2087 | JSON json; |
2069 | json_init (&json); |
2088 | json_init (&json); |
2070 | json.flags |= ix; |
2089 | json.flags |= ix; |
2071 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2090 | PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; |
|
|
2091 | XPUSHs (jsonstr); |
2072 | } |
2092 | } |
2073 | |
2093 | |