ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.109 by root, Tue Mar 8 10:18:50 2011 UTC vs.
Revision 1.118 by root, Fri Oct 25 20:27:57 2013 UTC

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 76
77// the amount of HEs to allocate on the stack, when sorting keys
78#define STACK_HES 64
79
77static HV *json_stash, *json_boolean_stash; // JSON::XS:: 80static HV *json_stash, *json_boolean_stash; // JSON::XS::
78static SV *json_true, *json_false; 81static SV *json_true, *json_false;
79 82
80enum { 83enum {
81 INCR_M_WS = 0, // initial whitespace skipping, must be 0 84 INCR_M_WS = 0, // initial whitespace skipping, must be 0
260 } 263 }
261 264
262 // this relies greatly on the quality of the pow () 265 // this relies greatly on the quality of the pow ()
263 // implementation of the platform, but a good 266 // implementation of the platform, but a good
264 // implementation is hard to beat. 267 // implementation is hard to beat.
268 // (IEEE 754 conformant ones are required to be exact)
265 if (postdp) *expo -= eaccum; 269 if (postdp) *expo -= eaccum;
266 *accum += uaccum * Perl_pow (10., *expo); 270 *accum += uaccum * Perl_pow (10., *expo);
267 *expo += eaccum; 271 *expo += eaccum;
268} 272}
269 273
479 483
480 if (enc->indent >= enc->json.max_depth) 484 if (enc->indent >= enc->json.max_depth)
481 croak (ERR_NESTING_EXCEEDED); 485 croak (ERR_NESTING_EXCEEDED);
482 486
483 encode_ch (enc, '['); 487 encode_ch (enc, '[');
484 488
485 if (len >= 0) 489 if (len >= 0)
486 { 490 {
487 encode_nl (enc); ++enc->indent; 491 encode_nl (enc); ++enc->indent;
488 492
489 for (i = 0; i <= len; ++i) 493 for (i = 0; i <= len; ++i)
501 encode_comma (enc); 505 encode_comma (enc);
502 } 506 }
503 507
504 encode_nl (enc); --enc->indent; encode_indent (enc); 508 encode_nl (enc); --enc->indent; encode_indent (enc);
505 } 509 }
506 510
507 encode_ch (enc, ']'); 511 encode_ch (enc, ']');
508} 512}
509 513
510static void 514static void
511encode_hk (enc_t *enc, HE *he) 515encode_hk (enc_t *enc, HE *he)
515 if (HeKLEN (he) == HEf_SVKEY) 519 if (HeKLEN (he) == HEf_SVKEY)
516 { 520 {
517 SV *sv = HeSVKEY (he); 521 SV *sv = HeSVKEY (he);
518 STRLEN len; 522 STRLEN len;
519 char *str; 523 char *str;
520 524
521 SvGETMAGIC (sv); 525 SvGETMAGIC (sv);
522 str = SvPV (sv, len); 526 str = SvPV (sv, len);
523 527
524 encode_str (enc, str, len, SvUTF8 (sv)); 528 encode_str (enc, str, len, SvUTF8 (sv));
525 } 529 }
591 } 595 }
592 596
593 if (count) 597 if (count)
594 { 598 {
595 int i, fast = 1; 599 int i, fast = 1;
596#if defined(__BORLANDC__) || defined(_MSC_VER) 600 HE *hes_stack [STACK_HES];
597 HE **hes = _alloca (count * sizeof (HE)); 601 HE **hes = hes_stack;
598#else 602
599 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 603 // allocate larger arrays on the heap
600#endif 604 if (count > STACK_HES)
605 {
606 SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes)));
607 hes = (HE **)SvPVX (sv);
608 }
601 609
602 i = 0; 610 i = 0;
603 while ((he = hv_iternext (hv))) 611 while ((he = hv_iternext (hv)))
604 { 612 {
605 hes [i++] = he; 613 hes [i++] = he;
824 } 832 }
825 else 833 else
826 { 834 {
827 // large integer, use the (rather slow) snprintf way. 835 // large integer, use the (rather slow) snprintf way.
828 need (enc, IVUV_MAXCHARS); 836 need (enc, IVUV_MAXCHARS);
829 enc->cur += 837 enc->cur +=
830 SvIsUV(sv) 838 SvIsUV(sv)
831 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) 839 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv))
832 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); 840 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv));
833 } 841 }
834 } 842 }
835 else if (SvROK (sv)) 843 else if (SvROK (sv))
836 encode_rv (enc, SvRV (sv)); 844 encode_rv (enc, SvRV (sv));
837 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) 845 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN)
838 encode_str (enc, "null", 4, 0); 846 encode_str (enc, "null", 4, 0);
839 else 847 else
840 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 848 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data",
841 SvPV_nolen (sv), SvFLAGS (sv)); 849 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
842} 850}
843 851
844static SV * 852static SV *
845encode_json (SV *scalar, JSON *json) 853encode_json (SV *scalar, JSON *json)
846{ 854{
1258 if (*dec->cur == ']') 1266 if (*dec->cur == ']')
1259 { 1267 {
1260 ++dec->cur; 1268 ++dec->cur;
1261 break; 1269 break;
1262 } 1270 }
1263 1271
1264 if (*dec->cur != ',') 1272 if (*dec->cur != ',')
1265 ERR (", or ] expected while parsing array"); 1273 ERR (", or ] expected while parsing array");
1266 1274
1267 ++dec->cur; 1275 ++dec->cur;
1268 1276
1456{ 1464{
1457 // the beauty of JSON: you need exactly one character lookahead 1465 // the beauty of JSON: you need exactly one character lookahead
1458 // to parse everything. 1466 // to parse everything.
1459 switch (*dec->cur) 1467 switch (*dec->cur)
1460 { 1468 {
1461 case '"': ++dec->cur; return decode_str (dec); 1469 case '"': ++dec->cur; return decode_str (dec);
1462 case '[': ++dec->cur; return decode_av (dec); 1470 case '[': ++dec->cur; return decode_av (dec);
1463 case '{': ++dec->cur; return decode_hv (dec); 1471 case '{': ++dec->cur; return decode_hv (dec);
1464 1472
1465 case '-': 1473 case '-':
1466 case '0': case '1': case '2': case '3': case '4': 1474 case '0': case '1': case '2': case '3': case '4':
1467 case '5': case '6': case '7': case '8': case '9': 1475 case '5': case '6': case '7': case '8': case '9':
1520{ 1528{
1521 dec_t dec; 1529 dec_t dec;
1522 SV *sv; 1530 SV *sv;
1523 1531
1524 /* work around bugs in 5.10 where manipulating magic values 1532 /* work around bugs in 5.10 where manipulating magic values
1525 * will perl ignore the magic in subsequent accesses. 1533 * makes perl ignore the magic in subsequent accesses.
1526 * 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
1527 * state (SvPV should do that, but it's buggy, see below). 1535 * state (SvPV should do that, but it's buggy, see below).
1528 */ 1536 */
1529 /*SvGETMAGIC (string);*/ 1537 /*SvGETMAGIC (string);*/
1530 if (SvMAGICAL (string) || !SvPOK (string)) 1538 if (SvMAGICAL (string) || !SvPOK (string))
1604 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1612 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1605 LEAVE; 1613 LEAVE;
1606 1614
1607 croak ("%s, at character offset %d (before \"%s\")", 1615 croak ("%s, at character offset %d (before \"%s\")",
1608 dec.err, 1616 dec.err,
1609 ptr_to_index (string, dec.cur), 1617 (int)ptr_to_index (string, dec.cur),
1610 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1618 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1611 } 1619 }
1612 1620
1613 sv = sv_2mortal (sv); 1621 sv = sv_2mortal (sv);
1614 1622
1804 json_boolean_stash = 0; 1812 json_boolean_stash = 0;
1805 1813
1806void new (char *klass) 1814void new (char *klass)
1807 PPCODE: 1815 PPCODE:
1808{ 1816{
1809 SV *pv = NEWSV (0, sizeof (JSON)); 1817 SV *pv = NEWSV (0, sizeof (JSON));
1810 SvPOK_only (pv); 1818 SvPOK_only (pv);
1811 json_init ((JSON *)SvPVX (pv)); 1819 json_init ((JSON *)SvPVX (pv));
1812 XPUSHs (sv_2mortal (sv_bless ( 1820 XPUSHs (sv_2mortal (sv_bless (
1813 newRV_noinc (pv), 1821 newRV_noinc (pv),
1814 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) 1822 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1891} 1899}
1892 1900
1893void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) 1901void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef)
1894 PPCODE: 1902 PPCODE:
1895{ 1903{
1896 if (!self->cb_sk_object) 1904 if (!self->cb_sk_object)
1897 self->cb_sk_object = newHV (); 1905 self->cb_sk_object = newHV ();
1898 1906
1899 if (SvOK (cb)) 1907 if (SvOK (cb))
1900 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); 1908 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1901 else 1909 else
1912 XPUSHs (ST (0)); 1920 XPUSHs (ST (0));
1913} 1921}
1914 1922
1915void encode (JSON *self, SV *scalar) 1923void encode (JSON *self, SV *scalar)
1916 PPCODE: 1924 PPCODE:
1917 XPUSHs (encode_json (scalar, self)); 1925 PUTBACK; scalar = encode_json (scalar, self); SPAGAIN;
1926 XPUSHs (scalar);
1918 1927
1919void decode (JSON *self, SV *jsonstr) 1928void decode (JSON *self, SV *jsonstr)
1920 PPCODE: 1929 PPCODE:
1921 XPUSHs (decode_json (jsonstr, self, 0)); 1930 PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN;
1931 XPUSHs (jsonstr);
1922 1932
1923void decode_prefix (JSON *self, SV *jsonstr) 1933void decode_prefix (JSON *self, SV *jsonstr)
1924 PPCODE: 1934 PPCODE:
1925{ 1935{
1936 SV *sv;
1926 char *offset; 1937 char *offset;
1938 PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN;
1927 EXTEND (SP, 2); 1939 EXTEND (SP, 2);
1928 PUSHs (decode_json (jsonstr, self, &offset)); 1940 PUSHs (sv);
1929 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); 1941 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1930} 1942}
1931 1943
1932void incr_parse (JSON *self, SV *jsonstr = 0) 1944void incr_parse (JSON *self, SV *jsonstr = 0)
1933 PPCODE: 1945 PPCODE:
1980 } 1992 }
1981 1993
1982 if (GIMME_V != G_VOID) 1994 if (GIMME_V != G_VOID)
1983 do 1995 do
1984 { 1996 {
1997 SV *sv;
1985 char *offset; 1998 char *offset;
1986 1999
1987 if (!INCR_DONE (self)) 2000 if (!INCR_DONE (self))
1988 { 2001 {
1989 incr_parse (self); 2002 incr_parse (self);
1991 if (self->incr_pos > self->max_size && self->max_size) 2004 if (self->incr_pos > self->max_size && self->max_size)
1992 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 2005 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1993 (unsigned long)self->incr_pos, (unsigned long)self->max_size); 2006 (unsigned long)self->incr_pos, (unsigned long)self->max_size);
1994 2007
1995 if (!INCR_DONE (self)) 2008 if (!INCR_DONE (self))
2009 {
2010 // as an optimisation, do not accumulate white space in the incr buffer
2011 if (self->incr_mode == INCR_M_WS && self->incr_pos)
2012 {
2013 self->incr_pos = 0;
2014 SvCUR_set (self->incr_text, 0);
2015 }
2016
1996 break; 2017 break;
2018 }
1997 } 2019 }
1998 2020
1999 XPUSHs (decode_json (self->incr_text, self, &offset)); 2021 PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN;
2022 XPUSHs (sv);
2000 2023
2001 self->incr_pos -= offset - SvPVX (self->incr_text); 2024 self->incr_pos -= offset - SvPVX (self->incr_text);
2002 self->incr_nest = 0; 2025 self->incr_nest = 0;
2003 self->incr_mode = 0; 2026 self->incr_mode = 0;
2004 2027
2048 SvREFCNT_dec (self->incr_text); 2071 SvREFCNT_dec (self->incr_text);
2049 2072
2050PROTOTYPES: ENABLE 2073PROTOTYPES: ENABLE
2051 2074
2052void encode_json (SV *scalar) 2075void encode_json (SV *scalar)
2053 ALIAS:
2054 to_json_ = 0
2055 encode_json = F_UTF8
2056 PPCODE: 2076 PPCODE:
2057{ 2077{
2058 JSON json; 2078 JSON json;
2059 json_init (&json); 2079 json_init (&json);
2060 json.flags |= ix; 2080 json.flags |= F_UTF8;
2061 XPUSHs (encode_json (scalar, &json)); 2081 PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN;
2082 XPUSHs (scalar);
2062} 2083}
2063 2084
2064void decode_json (SV *jsonstr) 2085void decode_json (SV *jsonstr)
2065 ALIAS:
2066 from_json_ = 0
2067 decode_json = F_UTF8
2068 PPCODE: 2086 PPCODE:
2069{ 2087{
2070 JSON json; 2088 JSON json;
2071 json_init (&json); 2089 json_init (&json);
2072 json.flags |= ix; 2090 json.flags |= F_UTF8;
2073 XPUSHs (decode_json (jsonstr, &json, 0)); 2091 PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN;
2092 XPUSHs (jsonstr);
2074} 2093}
2075 2094

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines