… | |
… | |
71 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
71 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
72 | #else |
72 | #else |
73 | # define JSON_SLOW 0 |
73 | # define JSON_SLOW 0 |
74 | # define JSON_STASH json_stash |
74 | # define JSON_STASH json_stash |
75 | #endif |
75 | #endif |
|
|
76 | |
|
|
77 | // the amount of HEs to allocate on the stack, when sorting keys |
|
|
78 | #define STACK_HES 64 |
76 | |
79 | |
77 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
80 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
78 | static SV *json_true, *json_false; |
81 | static SV *json_true, *json_false; |
79 | |
82 | |
80 | enum { |
83 | enum { |
… | |
… | |
592 | } |
595 | } |
593 | |
596 | |
594 | if (count) |
597 | if (count) |
595 | { |
598 | { |
596 | int i, fast = 1; |
599 | int i, fast = 1; |
597 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
600 | HE *hes_stack [STACK_HES]; |
598 | HE **hes = _alloca (count * sizeof (HE)); |
601 | HE **hes = hes_stack; |
599 | #else |
602 | |
600 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
603 | // allocate larger arrays on the heap |
601 | #endif |
604 | if (count > STACK_HES) |
|
|
605 | { |
|
|
606 | SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes))); |
|
|
607 | hes = (HE **)SvPVX (sv); |
|
|
608 | } |
602 | |
609 | |
603 | i = 0; |
610 | i = 0; |
604 | while ((he = hv_iternext (hv))) |
611 | while ((he = hv_iternext (hv))) |
605 | { |
612 | { |
606 | hes [i++] = he; |
613 | hes [i++] = he; |
… | |
… | |
825 | } |
832 | } |
826 | else |
833 | else |
827 | { |
834 | { |
828 | // large integer, use the (rather slow) snprintf way. |
835 | // large integer, use the (rather slow) snprintf way. |
829 | need (enc, IVUV_MAXCHARS); |
836 | need (enc, IVUV_MAXCHARS); |
830 | enc->cur += |
837 | enc->cur += |
831 | SvIsUV(sv) |
838 | SvIsUV(sv) |
832 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
839 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
833 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
840 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
834 | } |
841 | } |
835 | } |
842 | } |
… | |
… | |
1521 | { |
1528 | { |
1522 | dec_t dec; |
1529 | dec_t dec; |
1523 | SV *sv; |
1530 | SV *sv; |
1524 | |
1531 | |
1525 | /* work around bugs in 5.10 where manipulating magic values |
1532 | /* work around bugs in 5.10 where manipulating magic values |
1526 | * will perl ignore the magic in subsequent accesses. |
1533 | * makes perl ignore the magic in subsequent accesses. |
1527 | * also make a copy of non-PV values, to get them into a clean |
1534 | * 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). |
1535 | * state (SvPV should do that, but it's buggy, see below). |
1529 | */ |
1536 | */ |
1530 | /*SvGETMAGIC (string);*/ |
1537 | /*SvGETMAGIC (string);*/ |
1531 | if (SvMAGICAL (string) || !SvPOK (string)) |
1538 | if (SvMAGICAL (string) || !SvPOK (string)) |
… | |
… | |
1805 | json_boolean_stash = 0; |
1812 | json_boolean_stash = 0; |
1806 | |
1813 | |
1807 | void new (char *klass) |
1814 | void new (char *klass) |
1808 | PPCODE: |
1815 | PPCODE: |
1809 | { |
1816 | { |
1810 | SV *pv = NEWSV (0, sizeof (JSON)); |
1817 | SV *pv = NEWSV (0, sizeof (JSON)); |
1811 | SvPOK_only (pv); |
1818 | SvPOK_only (pv); |
1812 | json_init ((JSON *)SvPVX (pv)); |
1819 | json_init ((JSON *)SvPVX (pv)); |
1813 | XPUSHs (sv_2mortal (sv_bless ( |
1820 | XPUSHs (sv_2mortal (sv_bless ( |
1814 | newRV_noinc (pv), |
1821 | newRV_noinc (pv), |
1815 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
1822 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
… | |
… | |
1892 | } |
1899 | } |
1893 | |
1900 | |
1894 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1901 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1895 | PPCODE: |
1902 | PPCODE: |
1896 | { |
1903 | { |
1897 | if (!self->cb_sk_object) |
1904 | if (!self->cb_sk_object) |
1898 | self->cb_sk_object = newHV (); |
1905 | self->cb_sk_object = newHV (); |
1899 | |
1906 | |
1900 | if (SvOK (cb)) |
1907 | if (SvOK (cb)) |
1901 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1908 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1902 | else |
1909 | else |
… | |
… | |
1913 | XPUSHs (ST (0)); |
1920 | XPUSHs (ST (0)); |
1914 | } |
1921 | } |
1915 | |
1922 | |
1916 | void encode (JSON *self, SV *scalar) |
1923 | void encode (JSON *self, SV *scalar) |
1917 | PPCODE: |
1924 | PPCODE: |
1918 | XPUSHs (encode_json (scalar, self)); |
1925 | PUTBACK; scalar = encode_json (scalar, self); SPAGAIN; |
|
|
1926 | XPUSHs (scalar); |
1919 | |
1927 | |
1920 | void decode (JSON *self, SV *jsonstr) |
1928 | void decode (JSON *self, SV *jsonstr) |
1921 | PPCODE: |
1929 | PPCODE: |
1922 | XPUSHs (decode_json (jsonstr, self, 0)); |
1930 | PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN; |
|
|
1931 | XPUSHs (jsonstr); |
1923 | |
1932 | |
1924 | void decode_prefix (JSON *self, SV *jsonstr) |
1933 | void decode_prefix (JSON *self, SV *jsonstr) |
1925 | PPCODE: |
1934 | PPCODE: |
1926 | { |
1935 | { |
|
|
1936 | SV *sv; |
1927 | char *offset; |
1937 | char *offset; |
|
|
1938 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
1928 | EXTEND (SP, 2); |
1939 | EXTEND (SP, 2); |
1929 | PUSHs (decode_json (jsonstr, self, &offset)); |
1940 | PUSHs (sv); |
1930 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1941 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1931 | } |
1942 | } |
1932 | |
1943 | |
1933 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1944 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1934 | PPCODE: |
1945 | PPCODE: |
… | |
… | |
1981 | } |
1992 | } |
1982 | |
1993 | |
1983 | if (GIMME_V != G_VOID) |
1994 | if (GIMME_V != G_VOID) |
1984 | do |
1995 | do |
1985 | { |
1996 | { |
|
|
1997 | SV *sv; |
1986 | char *offset; |
1998 | char *offset; |
1987 | |
1999 | |
1988 | if (!INCR_DONE (self)) |
2000 | if (!INCR_DONE (self)) |
1989 | { |
2001 | { |
1990 | incr_parse (self); |
2002 | incr_parse (self); |
… | |
… | |
2004 | |
2016 | |
2005 | break; |
2017 | break; |
2006 | } |
2018 | } |
2007 | } |
2019 | } |
2008 | |
2020 | |
2009 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
2021 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
|
|
2022 | XPUSHs (sv); |
2010 | |
2023 | |
2011 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2024 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2012 | self->incr_nest = 0; |
2025 | self->incr_nest = 0; |
2013 | self->incr_mode = 0; |
2026 | self->incr_mode = 0; |
2014 | |
2027 | |
… | |
… | |
2066 | PPCODE: |
2079 | PPCODE: |
2067 | { |
2080 | { |
2068 | JSON json; |
2081 | JSON json; |
2069 | json_init (&json); |
2082 | json_init (&json); |
2070 | json.flags |= ix; |
2083 | json.flags |= ix; |
2071 | XPUSHs (encode_json (scalar, &json)); |
2084 | PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; |
|
|
2085 | XPUSHs (scalar); |
2072 | } |
2086 | } |
2073 | |
2087 | |
2074 | void decode_json (SV *jsonstr) |
2088 | void decode_json (SV *jsonstr) |
2075 | ALIAS: |
2089 | ALIAS: |
2076 | from_json_ = 0 |
2090 | from_json_ = 0 |
… | |
… | |
2078 | PPCODE: |
2092 | PPCODE: |
2079 | { |
2093 | { |
2080 | JSON json; |
2094 | JSON json; |
2081 | json_init (&json); |
2095 | json_init (&json); |
2082 | json.flags |= ix; |
2096 | json.flags |= ix; |
2083 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2097 | PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; |
|
|
2098 | XPUSHs (jsonstr); |
2084 | } |
2099 | } |
2085 | |
2100 | |