… | |
… | |
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)) |
… | |
… | |
69 | # 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)) |
70 | #else |
72 | #else |
71 | # define JSON_SLOW 0 |
73 | # define JSON_SLOW 0 |
72 | # define JSON_STASH json_stash |
74 | # define JSON_STASH json_stash |
73 | #endif |
75 | #endif |
|
|
76 | |
|
|
77 | // the amount of HEs to allocate on the stack, when sorting keys |
|
|
78 | #define STACK_HES 64 |
74 | |
79 | |
75 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
80 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
76 | static SV *json_true, *json_false; |
81 | static SV *json_true, *json_false; |
77 | |
82 | |
78 | enum { |
83 | enum { |
… | |
… | |
192 | ///////////////////////////////////////////////////////////////////////////// |
197 | ///////////////////////////////////////////////////////////////////////////// |
193 | // fp hell |
198 | // fp hell |
194 | |
199 | |
195 | // scan a group of digits, and a trailing exponent |
200 | // scan a group of digits, and a trailing exponent |
196 | static void |
201 | static void |
197 | json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp) |
202 | json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth) |
198 | { |
203 | { |
199 | UV uaccum = 0; |
204 | UV uaccum = 0; |
200 | int eaccum = 0; |
205 | int eaccum = 0; |
|
|
206 | |
|
|
207 | // if we recurse too deep, skip all remaining digits |
|
|
208 | // to avoid a stack overflow attack |
|
|
209 | if (expect_false (--maxdepth <= 0)) |
|
|
210 | while (((U8)*s - '0') < 10) |
|
|
211 | ++s; |
201 | |
212 | |
202 | for (;;) |
213 | for (;;) |
203 | { |
214 | { |
204 | U8 dig = (U8)*s - '0'; |
215 | U8 dig = (U8)*s - '0'; |
205 | |
216 | |
206 | if (expect_false (dig >= 10)) |
217 | if (expect_false (dig >= 10)) |
207 | { |
218 | { |
208 | if (dig == (U8)((U8)'.' - (U8)'0')) |
219 | if (dig == (U8)((U8)'.' - (U8)'0')) |
209 | { |
220 | { |
210 | ++s; |
221 | ++s; |
211 | json_atof_scan1 (s, accum, expo, 1); |
222 | json_atof_scan1 (s, accum, expo, 1, maxdepth); |
212 | } |
223 | } |
213 | else if ((dig | ' ') == 'e' - '0') |
224 | else if ((dig | ' ') == 'e' - '0') |
214 | { |
225 | { |
215 | int exp2 = 0; |
226 | int exp2 = 0; |
216 | int neg = 0; |
227 | int neg = 0; |
… | |
… | |
242 | // if we have too many digits, then recurse for more |
253 | // if we have too many digits, then recurse for more |
243 | // we actually do this for rather few digits |
254 | // we actually do this for rather few digits |
244 | if (uaccum >= (UV_MAX - 9) / 10) |
255 | if (uaccum >= (UV_MAX - 9) / 10) |
245 | { |
256 | { |
246 | if (postdp) *expo -= eaccum; |
257 | if (postdp) *expo -= eaccum; |
247 | json_atof_scan1 (s, accum, expo, postdp); |
258 | json_atof_scan1 (s, accum, expo, postdp, maxdepth); |
248 | if (postdp) *expo += eaccum; |
259 | if (postdp) *expo += eaccum; |
249 | |
260 | |
250 | break; |
261 | break; |
251 | } |
262 | } |
252 | } |
263 | } |
253 | |
264 | |
254 | // this relies greatly on the quality of the pow () |
265 | // this relies greatly on the quality of the pow () |
255 | // implementation of the platform, but a good |
266 | // implementation of the platform, but a good |
256 | // implementation is hard to beat. |
267 | // implementation is hard to beat. |
|
|
268 | // (IEEE 754 conformant ones are required to be exact) |
257 | if (postdp) *expo -= eaccum; |
269 | if (postdp) *expo -= eaccum; |
258 | *accum += uaccum * Perl_pow (10., *expo); |
270 | *accum += uaccum * Perl_pow (10., *expo); |
259 | *expo += eaccum; |
271 | *expo += eaccum; |
260 | } |
272 | } |
261 | |
273 | |
… | |
… | |
270 | { |
282 | { |
271 | ++s; |
283 | ++s; |
272 | neg = 1; |
284 | neg = 1; |
273 | } |
285 | } |
274 | |
286 | |
|
|
287 | // a recursion depth of ten gives us >>500 bits |
275 | json_atof_scan1 (s, &accum, &expo, 0); |
288 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
276 | |
289 | |
277 | return neg ? -accum : accum; |
290 | return neg ? -accum : accum; |
278 | } |
291 | } |
279 | ///////////////////////////////////////////////////////////////////////////// |
292 | ///////////////////////////////////////////////////////////////////////////// |
280 | // encoder |
293 | // encoder |
… | |
… | |
470 | |
483 | |
471 | if (enc->indent >= enc->json.max_depth) |
484 | if (enc->indent >= enc->json.max_depth) |
472 | croak (ERR_NESTING_EXCEEDED); |
485 | croak (ERR_NESTING_EXCEEDED); |
473 | |
486 | |
474 | encode_ch (enc, '['); |
487 | encode_ch (enc, '['); |
475 | |
488 | |
476 | if (len >= 0) |
489 | if (len >= 0) |
477 | { |
490 | { |
478 | encode_nl (enc); ++enc->indent; |
491 | encode_nl (enc); ++enc->indent; |
479 | |
492 | |
480 | for (i = 0; i <= len; ++i) |
493 | for (i = 0; i <= len; ++i) |
… | |
… | |
492 | encode_comma (enc); |
505 | encode_comma (enc); |
493 | } |
506 | } |
494 | |
507 | |
495 | encode_nl (enc); --enc->indent; encode_indent (enc); |
508 | encode_nl (enc); --enc->indent; encode_indent (enc); |
496 | } |
509 | } |
497 | |
510 | |
498 | encode_ch (enc, ']'); |
511 | encode_ch (enc, ']'); |
499 | } |
512 | } |
500 | |
513 | |
501 | static void |
514 | static void |
502 | encode_hk (enc_t *enc, HE *he) |
515 | encode_hk (enc_t *enc, HE *he) |
… | |
… | |
506 | if (HeKLEN (he) == HEf_SVKEY) |
519 | if (HeKLEN (he) == HEf_SVKEY) |
507 | { |
520 | { |
508 | SV *sv = HeSVKEY (he); |
521 | SV *sv = HeSVKEY (he); |
509 | STRLEN len; |
522 | STRLEN len; |
510 | char *str; |
523 | char *str; |
511 | |
524 | |
512 | SvGETMAGIC (sv); |
525 | SvGETMAGIC (sv); |
513 | str = SvPV (sv, len); |
526 | str = SvPV (sv, len); |
514 | |
527 | |
515 | encode_str (enc, str, len, SvUTF8 (sv)); |
528 | encode_str (enc, str, len, SvUTF8 (sv)); |
516 | } |
529 | } |
… | |
… | |
582 | } |
595 | } |
583 | |
596 | |
584 | if (count) |
597 | if (count) |
585 | { |
598 | { |
586 | int i, fast = 1; |
599 | int i, fast = 1; |
587 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
600 | HE *hes_stack [STACK_HES]; |
588 | HE **hes = _alloca (count * sizeof (HE)); |
601 | HE **hes = hes_stack; |
589 | #else |
602 | |
590 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
603 | // allocate larger arrays on the heap |
591 | #endif |
604 | if (count > STACK_HES) |
|
|
605 | { |
|
|
606 | SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes))); |
|
|
607 | hes = (HE **)SvPVX (sv); |
|
|
608 | } |
592 | |
609 | |
593 | i = 0; |
610 | i = 0; |
594 | while ((he = hv_iternext (hv))) |
611 | while ((he = hv_iternext (hv))) |
595 | { |
612 | { |
596 | hes [i++] = he; |
613 | hes [i++] = he; |
… | |
… | |
815 | } |
832 | } |
816 | else |
833 | else |
817 | { |
834 | { |
818 | // large integer, use the (rather slow) snprintf way. |
835 | // large integer, use the (rather slow) snprintf way. |
819 | need (enc, IVUV_MAXCHARS); |
836 | need (enc, IVUV_MAXCHARS); |
820 | enc->cur += |
837 | enc->cur += |
821 | SvIsUV(sv) |
838 | SvIsUV(sv) |
822 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
839 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
823 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
840 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
824 | } |
841 | } |
825 | } |
842 | } |
826 | else if (SvROK (sv)) |
843 | else if (SvROK (sv)) |
827 | encode_rv (enc, SvRV (sv)); |
844 | encode_rv (enc, SvRV (sv)); |
828 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
845 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
829 | encode_str (enc, "null", 4, 0); |
846 | encode_str (enc, "null", 4, 0); |
830 | else |
847 | else |
831 | 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", |
832 | SvPV_nolen (sv), SvFLAGS (sv)); |
849 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
833 | } |
850 | } |
834 | |
851 | |
835 | static SV * |
852 | static SV * |
836 | encode_json (SV *scalar, JSON *json) |
853 | encode_json (SV *scalar, JSON *json) |
837 | { |
854 | { |
… | |
… | |
1249 | if (*dec->cur == ']') |
1266 | if (*dec->cur == ']') |
1250 | { |
1267 | { |
1251 | ++dec->cur; |
1268 | ++dec->cur; |
1252 | break; |
1269 | break; |
1253 | } |
1270 | } |
1254 | |
1271 | |
1255 | if (*dec->cur != ',') |
1272 | if (*dec->cur != ',') |
1256 | ERR (", or ] expected while parsing array"); |
1273 | ERR (", or ] expected while parsing array"); |
1257 | |
1274 | |
1258 | ++dec->cur; |
1275 | ++dec->cur; |
1259 | |
1276 | |
… | |
… | |
1394 | dSP; |
1411 | dSP; |
1395 | int count; |
1412 | int count; |
1396 | |
1413 | |
1397 | ENTER; SAVETMPS; PUSHMARK (SP); |
1414 | ENTER; SAVETMPS; PUSHMARK (SP); |
1398 | XPUSHs (HeVAL (he)); |
1415 | XPUSHs (HeVAL (he)); |
|
|
1416 | sv_2mortal (sv); |
1399 | |
1417 | |
1400 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1418 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1401 | |
1419 | |
1402 | if (count == 1) |
1420 | if (count == 1) |
1403 | { |
1421 | { |
1404 | sv = newSVsv (POPs); |
1422 | sv = newSVsv (POPs); |
1405 | FREETMPS; LEAVE; |
1423 | FREETMPS; LEAVE; |
1406 | return sv; |
1424 | return sv; |
1407 | } |
1425 | } |
1408 | |
1426 | |
|
|
1427 | SvREFCNT_inc (sv); |
1409 | FREETMPS; LEAVE; |
1428 | FREETMPS; LEAVE; |
1410 | } |
1429 | } |
1411 | } |
1430 | } |
1412 | |
1431 | |
1413 | if (dec->json.cb_object) |
1432 | if (dec->json.cb_object) |
… | |
… | |
1445 | { |
1464 | { |
1446 | // the beauty of JSON: you need exactly one character lookahead |
1465 | // the beauty of JSON: you need exactly one character lookahead |
1447 | // to parse everything. |
1466 | // to parse everything. |
1448 | switch (*dec->cur) |
1467 | switch (*dec->cur) |
1449 | { |
1468 | { |
1450 | case '"': ++dec->cur; return decode_str (dec); |
1469 | case '"': ++dec->cur; return decode_str (dec); |
1451 | case '[': ++dec->cur; return decode_av (dec); |
1470 | case '[': ++dec->cur; return decode_av (dec); |
1452 | case '{': ++dec->cur; return decode_hv (dec); |
1471 | case '{': ++dec->cur; return decode_hv (dec); |
1453 | |
1472 | |
1454 | case '-': |
1473 | case '-': |
1455 | case '0': case '1': case '2': case '3': case '4': |
1474 | case '0': case '1': case '2': case '3': case '4': |
1456 | case '5': case '6': case '7': case '8': case '9': |
1475 | case '5': case '6': case '7': case '8': case '9': |
… | |
… | |
1509 | { |
1528 | { |
1510 | dec_t dec; |
1529 | dec_t dec; |
1511 | SV *sv; |
1530 | SV *sv; |
1512 | |
1531 | |
1513 | /* work around bugs in 5.10 where manipulating magic values |
1532 | /* work around bugs in 5.10 where manipulating magic values |
1514 | * will perl ignore the magic in subsequent accesses |
1533 | * makes perl ignore the magic in subsequent accesses. |
|
|
1534 | * also make a copy of non-PV values, to get them into a clean |
|
|
1535 | * state (SvPV should do that, but it's buggy, see below). |
1515 | */ |
1536 | */ |
1516 | /*SvGETMAGIC (string);*/ |
1537 | /*SvGETMAGIC (string);*/ |
1517 | if (SvMAGICAL (string)) |
1538 | if (SvMAGICAL (string) || !SvPOK (string)) |
1518 | string = sv_2mortal (newSVsv (string)); |
1539 | string = sv_2mortal (newSVsv (string)); |
1519 | |
1540 | |
1520 | SvUPGRADE (string, SVt_PV); |
1541 | SvUPGRADE (string, SVt_PV); |
1521 | |
1542 | |
1522 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1543 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
… | |
… | |
1539 | if (offset > json->max_size && json->max_size) |
1560 | if (offset > json->max_size && json->max_size) |
1540 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1561 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1541 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1562 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1542 | } |
1563 | } |
1543 | |
1564 | |
1544 | if (json->flags & F_UTF8) |
1565 | if (DECODE_WANTS_OCTETS (json)) |
1545 | sv_utf8_downgrade (string, 0); |
1566 | sv_utf8_downgrade (string, 0); |
1546 | else |
1567 | else |
1547 | sv_utf8_upgrade (string); |
1568 | sv_utf8_upgrade (string); |
1548 | |
1569 | |
1549 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1570 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
… | |
… | |
1591 | 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); |
1592 | LEAVE; |
1613 | LEAVE; |
1593 | |
1614 | |
1594 | croak ("%s, at character offset %d (before \"%s\")", |
1615 | croak ("%s, at character offset %d (before \"%s\")", |
1595 | dec.err, |
1616 | dec.err, |
1596 | ptr_to_index (string, dec.cur), |
1617 | (int)ptr_to_index (string, dec.cur), |
1597 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1618 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1598 | } |
1619 | } |
1599 | |
1620 | |
1600 | sv = sv_2mortal (sv); |
1621 | sv = sv_2mortal (sv); |
1601 | |
1622 | |
… | |
… | |
1791 | json_boolean_stash = 0; |
1812 | json_boolean_stash = 0; |
1792 | |
1813 | |
1793 | void new (char *klass) |
1814 | void new (char *klass) |
1794 | PPCODE: |
1815 | PPCODE: |
1795 | { |
1816 | { |
1796 | SV *pv = NEWSV (0, sizeof (JSON)); |
1817 | SV *pv = NEWSV (0, sizeof (JSON)); |
1797 | SvPOK_only (pv); |
1818 | SvPOK_only (pv); |
1798 | json_init ((JSON *)SvPVX (pv)); |
1819 | json_init ((JSON *)SvPVX (pv)); |
1799 | XPUSHs (sv_2mortal (sv_bless ( |
1820 | XPUSHs (sv_2mortal (sv_bless ( |
1800 | newRV_noinc (pv), |
1821 | newRV_noinc (pv), |
1801 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
1822 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
… | |
… | |
1878 | } |
1899 | } |
1879 | |
1900 | |
1880 | 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) |
1881 | PPCODE: |
1902 | PPCODE: |
1882 | { |
1903 | { |
1883 | if (!self->cb_sk_object) |
1904 | if (!self->cb_sk_object) |
1884 | self->cb_sk_object = newHV (); |
1905 | self->cb_sk_object = newHV (); |
1885 | |
1906 | |
1886 | if (SvOK (cb)) |
1907 | if (SvOK (cb)) |
1887 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1908 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1888 | else |
1909 | else |
… | |
… | |
1899 | XPUSHs (ST (0)); |
1920 | XPUSHs (ST (0)); |
1900 | } |
1921 | } |
1901 | |
1922 | |
1902 | void encode (JSON *self, SV *scalar) |
1923 | void encode (JSON *self, SV *scalar) |
1903 | PPCODE: |
1924 | PPCODE: |
1904 | XPUSHs (encode_json (scalar, self)); |
1925 | PUTBACK; scalar = encode_json (scalar, self); SPAGAIN; |
|
|
1926 | XPUSHs (scalar); |
1905 | |
1927 | |
1906 | void decode (JSON *self, SV *jsonstr) |
1928 | void decode (JSON *self, SV *jsonstr) |
1907 | PPCODE: |
1929 | PPCODE: |
1908 | XPUSHs (decode_json (jsonstr, self, 0)); |
1930 | PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN; |
|
|
1931 | XPUSHs (jsonstr); |
1909 | |
1932 | |
1910 | void decode_prefix (JSON *self, SV *jsonstr) |
1933 | void decode_prefix (JSON *self, SV *jsonstr) |
1911 | PPCODE: |
1934 | PPCODE: |
1912 | { |
1935 | { |
|
|
1936 | SV *sv; |
1913 | char *offset; |
1937 | char *offset; |
|
|
1938 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
1914 | EXTEND (SP, 2); |
1939 | EXTEND (SP, 2); |
1915 | PUSHs (decode_json (jsonstr, self, &offset)); |
1940 | PUSHs (sv); |
1916 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1941 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1917 | } |
1942 | } |
1918 | |
1943 | |
1919 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1944 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1920 | PPCODE: |
1945 | PPCODE: |
1921 | { |
1946 | { |
1922 | if (!self->incr_text) |
1947 | if (!self->incr_text) |
1923 | self->incr_text = newSVpvn ("", 0); |
1948 | self->incr_text = newSVpvn ("", 0); |
|
|
1949 | |
|
|
1950 | /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */ |
|
|
1951 | if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) |
|
|
1952 | if (DECODE_WANTS_OCTETS (self)) |
|
|
1953 | { |
|
|
1954 | if (self->incr_pos) |
|
|
1955 | self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text), |
|
|
1956 | (U8 *)SvPVX (self->incr_text) + self->incr_pos); |
|
|
1957 | |
|
|
1958 | sv_utf8_downgrade (self->incr_text, 0); |
|
|
1959 | } |
|
|
1960 | else |
|
|
1961 | { |
|
|
1962 | sv_utf8_upgrade (self->incr_text); |
|
|
1963 | |
|
|
1964 | if (self->incr_pos) |
|
|
1965 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1966 | - (U8 *)SvPVX (self->incr_text); |
|
|
1967 | } |
1924 | |
1968 | |
1925 | // append data, if any |
1969 | // append data, if any |
1926 | if (jsonstr) |
1970 | if (jsonstr) |
1927 | { |
1971 | { |
|
|
1972 | /* make sure both strings have same encoding */ |
|
|
1973 | if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) |
1928 | if (SvUTF8 (jsonstr)) |
1974 | if (SvUTF8 (jsonstr)) |
|
|
1975 | sv_utf8_downgrade (jsonstr, 0); |
1929 | { |
1976 | else |
1930 | if (!SvUTF8 (self->incr_text)) |
|
|
1931 | { |
|
|
1932 | /* utf-8-ness differs, need to upgrade */ |
|
|
1933 | sv_utf8_upgrade (self->incr_text); |
|
|
1934 | |
|
|
1935 | if (self->incr_pos) |
|
|
1936 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1937 | - (U8 *)SvPVX (self->incr_text); |
|
|
1938 | } |
|
|
1939 | } |
|
|
1940 | else if (SvUTF8 (self->incr_text)) |
|
|
1941 | sv_utf8_upgrade (jsonstr); |
1977 | sv_utf8_upgrade (jsonstr); |
1942 | |
1978 | |
|
|
1979 | /* and then just blindly append */ |
1943 | { |
1980 | { |
1944 | STRLEN len; |
1981 | STRLEN len; |
1945 | const char *str = SvPV (jsonstr, len); |
1982 | const char *str = SvPV (jsonstr, len); |
1946 | STRLEN cur = SvCUR (self->incr_text); |
1983 | STRLEN cur = SvCUR (self->incr_text); |
1947 | |
1984 | |
… | |
… | |
1955 | } |
1992 | } |
1956 | |
1993 | |
1957 | if (GIMME_V != G_VOID) |
1994 | if (GIMME_V != G_VOID) |
1958 | do |
1995 | do |
1959 | { |
1996 | { |
|
|
1997 | SV *sv; |
1960 | char *offset; |
1998 | char *offset; |
1961 | |
1999 | |
1962 | if (!INCR_DONE (self)) |
2000 | if (!INCR_DONE (self)) |
1963 | { |
2001 | { |
1964 | incr_parse (self); |
2002 | incr_parse (self); |
… | |
… | |
1966 | if (self->incr_pos > self->max_size && self->max_size) |
2004 | if (self->incr_pos > self->max_size && self->max_size) |
1967 | 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", |
1968 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
2006 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1969 | |
2007 | |
1970 | 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 | |
1971 | break; |
2017 | break; |
|
|
2018 | } |
1972 | } |
2019 | } |
1973 | |
2020 | |
1974 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
2021 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
|
|
2022 | XPUSHs (sv); |
1975 | |
2023 | |
1976 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2024 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1977 | self->incr_nest = 0; |
2025 | self->incr_nest = 0; |
1978 | self->incr_mode = 0; |
2026 | self->incr_mode = 0; |
1979 | |
2027 | |
… | |
… | |
2023 | SvREFCNT_dec (self->incr_text); |
2071 | SvREFCNT_dec (self->incr_text); |
2024 | |
2072 | |
2025 | PROTOTYPES: ENABLE |
2073 | PROTOTYPES: ENABLE |
2026 | |
2074 | |
2027 | void encode_json (SV *scalar) |
2075 | void encode_json (SV *scalar) |
2028 | ALIAS: |
|
|
2029 | to_json_ = 0 |
|
|
2030 | encode_json = F_UTF8 |
|
|
2031 | PPCODE: |
2076 | PPCODE: |
2032 | { |
2077 | { |
2033 | JSON json; |
2078 | JSON json; |
2034 | json_init (&json); |
2079 | json_init (&json); |
2035 | json.flags |= ix; |
2080 | json.flags |= F_UTF8; |
2036 | XPUSHs (encode_json (scalar, &json)); |
2081 | PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; |
|
|
2082 | XPUSHs (scalar); |
2037 | } |
2083 | } |
2038 | |
2084 | |
2039 | void decode_json (SV *jsonstr) |
2085 | void decode_json (SV *jsonstr) |
2040 | ALIAS: |
|
|
2041 | from_json_ = 0 |
|
|
2042 | decode_json = F_UTF8 |
|
|
2043 | PPCODE: |
2086 | PPCODE: |
2044 | { |
2087 | { |
2045 | JSON json; |
2088 | JSON json; |
2046 | json_init (&json); |
2089 | json_init (&json); |
2047 | json.flags |= ix; |
2090 | json.flags |= F_UTF8; |
2048 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2091 | PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; |
|
|
2092 | XPUSHs (jsonstr); |
2049 | } |
2093 | } |
2050 | |
2094 | |