… | |
… | |
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 { |
… | |
… | |
187 | return SvUTF8 (sv) |
192 | return SvUTF8 (sv) |
188 | ? utf8_distance (offset, SvPVX (sv)) |
193 | ? utf8_distance (offset, SvPVX (sv)) |
189 | : offset - SvPVX (sv); |
194 | : offset - SvPVX (sv); |
190 | } |
195 | } |
191 | |
196 | |
|
|
197 | ///////////////////////////////////////////////////////////////////////////// |
|
|
198 | // fp hell |
|
|
199 | |
|
|
200 | // scan a group of digits, and a trailing exponent |
|
|
201 | static void |
|
|
202 | json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth) |
|
|
203 | { |
|
|
204 | UV uaccum = 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; |
|
|
212 | |
|
|
213 | for (;;) |
|
|
214 | { |
|
|
215 | U8 dig = (U8)*s - '0'; |
|
|
216 | |
|
|
217 | if (expect_false (dig >= 10)) |
|
|
218 | { |
|
|
219 | if (dig == (U8)((U8)'.' - (U8)'0')) |
|
|
220 | { |
|
|
221 | ++s; |
|
|
222 | json_atof_scan1 (s, accum, expo, 1, maxdepth); |
|
|
223 | } |
|
|
224 | else if ((dig | ' ') == 'e' - '0') |
|
|
225 | { |
|
|
226 | int exp2 = 0; |
|
|
227 | int neg = 0; |
|
|
228 | |
|
|
229 | ++s; |
|
|
230 | |
|
|
231 | if (*s == '-') |
|
|
232 | { |
|
|
233 | ++s; |
|
|
234 | neg = 1; |
|
|
235 | } |
|
|
236 | else if (*s == '+') |
|
|
237 | ++s; |
|
|
238 | |
|
|
239 | while ((dig = (U8)*s - '0') < 10) |
|
|
240 | exp2 = exp2 * 10 + *s++ - '0'; |
|
|
241 | |
|
|
242 | *expo += neg ? -exp2 : exp2; |
|
|
243 | } |
|
|
244 | |
|
|
245 | break; |
|
|
246 | } |
|
|
247 | |
|
|
248 | ++s; |
|
|
249 | |
|
|
250 | uaccum = uaccum * 10 + dig; |
|
|
251 | ++eaccum; |
|
|
252 | |
|
|
253 | // if we have too many digits, then recurse for more |
|
|
254 | // we actually do this for rather few digits |
|
|
255 | if (uaccum >= (UV_MAX - 9) / 10) |
|
|
256 | { |
|
|
257 | if (postdp) *expo -= eaccum; |
|
|
258 | json_atof_scan1 (s, accum, expo, postdp, maxdepth); |
|
|
259 | if (postdp) *expo += eaccum; |
|
|
260 | |
|
|
261 | break; |
|
|
262 | } |
|
|
263 | } |
|
|
264 | |
|
|
265 | // this relies greatly on the quality of the pow () |
|
|
266 | // implementation of the platform, but a good |
|
|
267 | // implementation is hard to beat. |
|
|
268 | // (IEEE 754 conformant ones are required to be exact) |
|
|
269 | if (postdp) *expo -= eaccum; |
|
|
270 | *accum += uaccum * Perl_pow (10., *expo); |
|
|
271 | *expo += eaccum; |
|
|
272 | } |
|
|
273 | |
|
|
274 | static NV |
|
|
275 | json_atof (const char *s) |
|
|
276 | { |
|
|
277 | NV accum = 0.; |
|
|
278 | int expo = 0; |
|
|
279 | int neg = 0; |
|
|
280 | |
|
|
281 | if (*s == '-') |
|
|
282 | { |
|
|
283 | ++s; |
|
|
284 | neg = 1; |
|
|
285 | } |
|
|
286 | |
|
|
287 | // a recursion depth of ten gives us >>500 bits |
|
|
288 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
|
|
289 | |
|
|
290 | return neg ? -accum : accum; |
|
|
291 | } |
192 | ///////////////////////////////////////////////////////////////////////////// |
292 | ///////////////////////////////////////////////////////////////////////////// |
193 | // encoder |
293 | // encoder |
194 | |
294 | |
195 | // structure used for encoding JSON |
295 | // structure used for encoding JSON |
196 | typedef struct |
296 | typedef struct |
… | |
… | |
383 | |
483 | |
384 | if (enc->indent >= enc->json.max_depth) |
484 | if (enc->indent >= enc->json.max_depth) |
385 | croak (ERR_NESTING_EXCEEDED); |
485 | croak (ERR_NESTING_EXCEEDED); |
386 | |
486 | |
387 | encode_ch (enc, '['); |
487 | encode_ch (enc, '['); |
388 | |
488 | |
389 | if (len >= 0) |
489 | if (len >= 0) |
390 | { |
490 | { |
391 | encode_nl (enc); ++enc->indent; |
491 | encode_nl (enc); ++enc->indent; |
392 | |
492 | |
393 | for (i = 0; i <= len; ++i) |
493 | for (i = 0; i <= len; ++i) |
… | |
… | |
405 | encode_comma (enc); |
505 | encode_comma (enc); |
406 | } |
506 | } |
407 | |
507 | |
408 | encode_nl (enc); --enc->indent; encode_indent (enc); |
508 | encode_nl (enc); --enc->indent; encode_indent (enc); |
409 | } |
509 | } |
410 | |
510 | |
411 | encode_ch (enc, ']'); |
511 | encode_ch (enc, ']'); |
412 | } |
512 | } |
413 | |
513 | |
414 | static void |
514 | static void |
415 | encode_hk (enc_t *enc, HE *he) |
515 | encode_hk (enc_t *enc, HE *he) |
… | |
… | |
419 | if (HeKLEN (he) == HEf_SVKEY) |
519 | if (HeKLEN (he) == HEf_SVKEY) |
420 | { |
520 | { |
421 | SV *sv = HeSVKEY (he); |
521 | SV *sv = HeSVKEY (he); |
422 | STRLEN len; |
522 | STRLEN len; |
423 | char *str; |
523 | char *str; |
424 | |
524 | |
425 | SvGETMAGIC (sv); |
525 | SvGETMAGIC (sv); |
426 | str = SvPV (sv, len); |
526 | str = SvPV (sv, len); |
427 | |
527 | |
428 | encode_str (enc, str, len, SvUTF8 (sv)); |
528 | encode_str (enc, str, len, SvUTF8 (sv)); |
429 | } |
529 | } |
… | |
… | |
495 | } |
595 | } |
496 | |
596 | |
497 | if (count) |
597 | if (count) |
498 | { |
598 | { |
499 | int i, fast = 1; |
599 | int i, fast = 1; |
500 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
600 | HE *hes_stack [STACK_HES]; |
501 | HE **hes = _alloca (count * sizeof (HE)); |
601 | HE **hes = hes_stack; |
502 | #else |
602 | |
503 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
603 | // allocate larger arrays on the heap |
504 | #endif |
604 | if (count > STACK_HES) |
|
|
605 | { |
|
|
606 | SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes))); |
|
|
607 | hes = (HE **)SvPVX (sv); |
|
|
608 | } |
505 | |
609 | |
506 | i = 0; |
610 | i = 0; |
507 | while ((he = hv_iternext (hv))) |
611 | while ((he = hv_iternext (hv))) |
508 | { |
612 | { |
509 | hes [i++] = he; |
613 | hes [i++] = he; |
… | |
… | |
728 | } |
832 | } |
729 | else |
833 | else |
730 | { |
834 | { |
731 | // large integer, use the (rather slow) snprintf way. |
835 | // large integer, use the (rather slow) snprintf way. |
732 | need (enc, IVUV_MAXCHARS); |
836 | need (enc, IVUV_MAXCHARS); |
733 | enc->cur += |
837 | enc->cur += |
734 | SvIsUV(sv) |
838 | SvIsUV(sv) |
735 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
839 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
736 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
840 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
737 | } |
841 | } |
738 | } |
842 | } |
739 | else if (SvROK (sv)) |
843 | else if (SvROK (sv)) |
740 | encode_rv (enc, SvRV (sv)); |
844 | encode_rv (enc, SvRV (sv)); |
741 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
845 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
742 | encode_str (enc, "null", 4, 0); |
846 | encode_str (enc, "null", 4, 0); |
743 | else |
847 | else |
744 | 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", |
745 | SvPV_nolen (sv), SvFLAGS (sv)); |
849 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
746 | } |
850 | } |
747 | |
851 | |
748 | static SV * |
852 | static SV * |
749 | encode_json (SV *scalar, JSON *json) |
853 | encode_json (SV *scalar, JSON *json) |
750 | { |
854 | { |
… | |
… | |
1119 | } |
1223 | } |
1120 | |
1224 | |
1121 | len -= *start == '-' ? 1 : 0; |
1225 | len -= *start == '-' ? 1 : 0; |
1122 | |
1226 | |
1123 | // does not fit into IV or UV, try NV |
1227 | // does not fit into IV or UV, try NV |
1124 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
1228 | if (len <= NV_DIG) |
1125 | #if defined (LDBL_DIG) |
|
|
1126 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1127 | #endif |
|
|
1128 | ) |
|
|
1129 | // fits into NV without loss of precision |
1229 | // fits into NV without loss of precision |
1130 | return newSVnv (Atof (start)); |
1230 | return newSVnv (json_atof (start)); |
1131 | |
1231 | |
1132 | // everything else fails, convert it to a string |
1232 | // everything else fails, convert it to a string |
1133 | return newSVpvn (start, dec->cur - start); |
1233 | return newSVpvn (start, dec->cur - start); |
1134 | } |
1234 | } |
1135 | |
1235 | |
1136 | // loss of precision here |
1236 | // loss of precision here |
1137 | return newSVnv (Atof (start)); |
1237 | return newSVnv (json_atof (start)); |
1138 | |
1238 | |
1139 | fail: |
1239 | fail: |
1140 | return 0; |
1240 | return 0; |
1141 | } |
1241 | } |
1142 | |
1242 | |
… | |
… | |
1166 | if (*dec->cur == ']') |
1266 | if (*dec->cur == ']') |
1167 | { |
1267 | { |
1168 | ++dec->cur; |
1268 | ++dec->cur; |
1169 | break; |
1269 | break; |
1170 | } |
1270 | } |
1171 | |
1271 | |
1172 | if (*dec->cur != ',') |
1272 | if (*dec->cur != ',') |
1173 | ERR (", or ] expected while parsing array"); |
1273 | ERR (", or ] expected while parsing array"); |
1174 | |
1274 | |
1175 | ++dec->cur; |
1275 | ++dec->cur; |
1176 | |
1276 | |
… | |
… | |
1311 | dSP; |
1411 | dSP; |
1312 | int count; |
1412 | int count; |
1313 | |
1413 | |
1314 | ENTER; SAVETMPS; PUSHMARK (SP); |
1414 | ENTER; SAVETMPS; PUSHMARK (SP); |
1315 | XPUSHs (HeVAL (he)); |
1415 | XPUSHs (HeVAL (he)); |
|
|
1416 | sv_2mortal (sv); |
1316 | |
1417 | |
1317 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1418 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1318 | |
1419 | |
1319 | if (count == 1) |
1420 | if (count == 1) |
1320 | { |
1421 | { |
1321 | sv = newSVsv (POPs); |
1422 | sv = newSVsv (POPs); |
1322 | FREETMPS; LEAVE; |
1423 | FREETMPS; LEAVE; |
1323 | return sv; |
1424 | return sv; |
1324 | } |
1425 | } |
1325 | |
1426 | |
|
|
1427 | SvREFCNT_inc (sv); |
1326 | FREETMPS; LEAVE; |
1428 | FREETMPS; LEAVE; |
1327 | } |
1429 | } |
1328 | } |
1430 | } |
1329 | |
1431 | |
1330 | if (dec->json.cb_object) |
1432 | if (dec->json.cb_object) |
… | |
… | |
1362 | { |
1464 | { |
1363 | // the beauty of JSON: you need exactly one character lookahead |
1465 | // the beauty of JSON: you need exactly one character lookahead |
1364 | // to parse everything. |
1466 | // to parse everything. |
1365 | switch (*dec->cur) |
1467 | switch (*dec->cur) |
1366 | { |
1468 | { |
1367 | case '"': ++dec->cur; return decode_str (dec); |
1469 | case '"': ++dec->cur; return decode_str (dec); |
1368 | case '[': ++dec->cur; return decode_av (dec); |
1470 | case '[': ++dec->cur; return decode_av (dec); |
1369 | case '{': ++dec->cur; return decode_hv (dec); |
1471 | case '{': ++dec->cur; return decode_hv (dec); |
1370 | |
1472 | |
1371 | case '-': |
1473 | case '-': |
1372 | case '0': case '1': case '2': case '3': case '4': |
1474 | case '0': case '1': case '2': case '3': case '4': |
1373 | case '5': case '6': case '7': case '8': case '9': |
1475 | case '5': case '6': case '7': case '8': case '9': |
… | |
… | |
1426 | { |
1528 | { |
1427 | dec_t dec; |
1529 | dec_t dec; |
1428 | SV *sv; |
1530 | SV *sv; |
1429 | |
1531 | |
1430 | /* work around bugs in 5.10 where manipulating magic values |
1532 | /* work around bugs in 5.10 where manipulating magic values |
1431 | * 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). |
1432 | */ |
1536 | */ |
1433 | /*SvGETMAGIC (string);*/ |
1537 | /*SvGETMAGIC (string);*/ |
1434 | if (SvMAGICAL (string)) |
1538 | if (SvMAGICAL (string) || !SvPOK (string)) |
1435 | string = sv_2mortal (newSVsv (string)); |
1539 | string = sv_2mortal (newSVsv (string)); |
1436 | |
1540 | |
1437 | SvUPGRADE (string, SVt_PV); |
1541 | SvUPGRADE (string, SVt_PV); |
1438 | |
1542 | |
1439 | /* 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 |
… | |
… | |
1456 | if (offset > json->max_size && json->max_size) |
1560 | if (offset > json->max_size && json->max_size) |
1457 | 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", |
1458 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1562 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1459 | } |
1563 | } |
1460 | |
1564 | |
1461 | if (json->flags & F_UTF8) |
1565 | if (DECODE_WANTS_OCTETS (json)) |
1462 | sv_utf8_downgrade (string, 0); |
1566 | sv_utf8_downgrade (string, 0); |
1463 | else |
1567 | else |
1464 | sv_utf8_upgrade (string); |
1568 | sv_utf8_upgrade (string); |
1465 | |
1569 | |
1466 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1570 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
… | |
… | |
1508 | 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); |
1509 | LEAVE; |
1613 | LEAVE; |
1510 | |
1614 | |
1511 | croak ("%s, at character offset %d (before \"%s\")", |
1615 | croak ("%s, at character offset %d (before \"%s\")", |
1512 | dec.err, |
1616 | dec.err, |
1513 | ptr_to_index (string, dec.cur), |
1617 | (int)ptr_to_index (string, dec.cur), |
1514 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1618 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1515 | } |
1619 | } |
1516 | |
1620 | |
1517 | sv = sv_2mortal (sv); |
1621 | sv = sv_2mortal (sv); |
1518 | |
1622 | |
… | |
… | |
1708 | json_boolean_stash = 0; |
1812 | json_boolean_stash = 0; |
1709 | |
1813 | |
1710 | void new (char *klass) |
1814 | void new (char *klass) |
1711 | PPCODE: |
1815 | PPCODE: |
1712 | { |
1816 | { |
1713 | SV *pv = NEWSV (0, sizeof (JSON)); |
1817 | SV *pv = NEWSV (0, sizeof (JSON)); |
1714 | SvPOK_only (pv); |
1818 | SvPOK_only (pv); |
1715 | json_init ((JSON *)SvPVX (pv)); |
1819 | json_init ((JSON *)SvPVX (pv)); |
1716 | XPUSHs (sv_2mortal (sv_bless ( |
1820 | XPUSHs (sv_2mortal (sv_bless ( |
1717 | newRV_noinc (pv), |
1821 | newRV_noinc (pv), |
1718 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
1822 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
… | |
… | |
1795 | } |
1899 | } |
1796 | |
1900 | |
1797 | 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) |
1798 | PPCODE: |
1902 | PPCODE: |
1799 | { |
1903 | { |
1800 | if (!self->cb_sk_object) |
1904 | if (!self->cb_sk_object) |
1801 | self->cb_sk_object = newHV (); |
1905 | self->cb_sk_object = newHV (); |
1802 | |
1906 | |
1803 | if (SvOK (cb)) |
1907 | if (SvOK (cb)) |
1804 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1908 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1805 | else |
1909 | else |
… | |
… | |
1816 | XPUSHs (ST (0)); |
1920 | XPUSHs (ST (0)); |
1817 | } |
1921 | } |
1818 | |
1922 | |
1819 | void encode (JSON *self, SV *scalar) |
1923 | void encode (JSON *self, SV *scalar) |
1820 | PPCODE: |
1924 | PPCODE: |
1821 | XPUSHs (encode_json (scalar, self)); |
1925 | PUTBACK; scalar = encode_json (scalar, self); SPAGAIN; |
|
|
1926 | XPUSHs (scalar); |
1822 | |
1927 | |
1823 | void decode (JSON *self, SV *jsonstr) |
1928 | void decode (JSON *self, SV *jsonstr) |
1824 | PPCODE: |
1929 | PPCODE: |
1825 | XPUSHs (decode_json (jsonstr, self, 0)); |
1930 | PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN; |
|
|
1931 | XPUSHs (jsonstr); |
1826 | |
1932 | |
1827 | void decode_prefix (JSON *self, SV *jsonstr) |
1933 | void decode_prefix (JSON *self, SV *jsonstr) |
1828 | PPCODE: |
1934 | PPCODE: |
1829 | { |
1935 | { |
|
|
1936 | SV *sv; |
1830 | char *offset; |
1937 | char *offset; |
|
|
1938 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
1831 | EXTEND (SP, 2); |
1939 | EXTEND (SP, 2); |
1832 | PUSHs (decode_json (jsonstr, self, &offset)); |
1940 | PUSHs (sv); |
1833 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1941 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1834 | } |
1942 | } |
1835 | |
1943 | |
1836 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1944 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1837 | PPCODE: |
1945 | PPCODE: |
1838 | { |
1946 | { |
1839 | if (!self->incr_text) |
1947 | if (!self->incr_text) |
1840 | 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 | } |
1841 | |
1968 | |
1842 | // append data, if any |
1969 | // append data, if any |
1843 | if (jsonstr) |
1970 | if (jsonstr) |
1844 | { |
1971 | { |
|
|
1972 | /* make sure both strings have same encoding */ |
|
|
1973 | if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) |
1845 | if (SvUTF8 (jsonstr)) |
1974 | if (SvUTF8 (jsonstr)) |
|
|
1975 | sv_utf8_downgrade (jsonstr, 0); |
1846 | { |
1976 | else |
1847 | if (!SvUTF8 (self->incr_text)) |
|
|
1848 | { |
|
|
1849 | /* utf-8-ness differs, need to upgrade */ |
|
|
1850 | sv_utf8_upgrade (self->incr_text); |
|
|
1851 | |
|
|
1852 | if (self->incr_pos) |
|
|
1853 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1854 | - (U8 *)SvPVX (self->incr_text); |
|
|
1855 | } |
|
|
1856 | } |
|
|
1857 | else if (SvUTF8 (self->incr_text)) |
|
|
1858 | sv_utf8_upgrade (jsonstr); |
1977 | sv_utf8_upgrade (jsonstr); |
1859 | |
1978 | |
|
|
1979 | /* and then just blindly append */ |
1860 | { |
1980 | { |
1861 | STRLEN len; |
1981 | STRLEN len; |
1862 | const char *str = SvPV (jsonstr, len); |
1982 | const char *str = SvPV (jsonstr, len); |
1863 | STRLEN cur = SvCUR (self->incr_text); |
1983 | STRLEN cur = SvCUR (self->incr_text); |
1864 | |
1984 | |
… | |
… | |
1872 | } |
1992 | } |
1873 | |
1993 | |
1874 | if (GIMME_V != G_VOID) |
1994 | if (GIMME_V != G_VOID) |
1875 | do |
1995 | do |
1876 | { |
1996 | { |
|
|
1997 | SV *sv; |
1877 | char *offset; |
1998 | char *offset; |
1878 | |
1999 | |
1879 | if (!INCR_DONE (self)) |
2000 | if (!INCR_DONE (self)) |
1880 | { |
2001 | { |
1881 | incr_parse (self); |
2002 | incr_parse (self); |
… | |
… | |
1883 | if (self->incr_pos > self->max_size && self->max_size) |
2004 | if (self->incr_pos > self->max_size && self->max_size) |
1884 | 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", |
1885 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
2006 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1886 | |
2007 | |
1887 | 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 | |
1888 | break; |
2017 | break; |
|
|
2018 | } |
1889 | } |
2019 | } |
1890 | |
2020 | |
1891 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
2021 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
|
|
2022 | XPUSHs (sv); |
1892 | |
2023 | |
1893 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2024 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1894 | self->incr_nest = 0; |
2025 | self->incr_nest = 0; |
1895 | self->incr_mode = 0; |
2026 | self->incr_mode = 0; |
1896 | |
2027 | |
… | |
… | |
1940 | SvREFCNT_dec (self->incr_text); |
2071 | SvREFCNT_dec (self->incr_text); |
1941 | |
2072 | |
1942 | PROTOTYPES: ENABLE |
2073 | PROTOTYPES: ENABLE |
1943 | |
2074 | |
1944 | void encode_json (SV *scalar) |
2075 | void encode_json (SV *scalar) |
1945 | ALIAS: |
|
|
1946 | to_json_ = 0 |
|
|
1947 | encode_json = F_UTF8 |
|
|
1948 | PPCODE: |
2076 | PPCODE: |
1949 | { |
2077 | { |
1950 | JSON json; |
2078 | JSON json; |
1951 | json_init (&json); |
2079 | json_init (&json); |
1952 | json.flags |= ix; |
2080 | json.flags |= F_UTF8; |
1953 | XPUSHs (encode_json (scalar, &json)); |
2081 | PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; |
|
|
2082 | XPUSHs (scalar); |
1954 | } |
2083 | } |
1955 | |
2084 | |
1956 | void decode_json (SV *jsonstr) |
2085 | void decode_json (SV *jsonstr) |
1957 | ALIAS: |
|
|
1958 | from_json_ = 0 |
|
|
1959 | decode_json = F_UTF8 |
|
|
1960 | PPCODE: |
2086 | PPCODE: |
1961 | { |
2087 | { |
1962 | JSON json; |
2088 | JSON json; |
1963 | json_init (&json); |
2089 | json_init (&json); |
1964 | json.flags |= ix; |
2090 | json.flags |= F_UTF8; |
1965 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2091 | PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; |
|
|
2092 | XPUSHs (jsonstr); |
1966 | } |
2093 | } |
1967 | |
2094 | |
1968 | |
|
|