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.104 by root, Tue Jan 19 00:31:13 2010 UTC vs.
Revision 1.120 by root, Mon Oct 28 23:23:10 2013 UTC

33#define F_SHRINK 0x00000200UL 33#define F_SHRINK 0x00000200UL
34#define F_ALLOW_BLESSED 0x00000400UL 34#define F_ALLOW_BLESSED 0x00000400UL
35#define F_CONV_BLESSED 0x00000800UL 35#define F_CONV_BLESSED 0x00000800UL
36#define F_RELAXED 0x00001000UL 36#define F_RELAXED 0x00001000UL
37#define F_ALLOW_UNKNOWN 0x00002000UL 37#define F_ALLOW_UNKNOWN 0x00002000UL
38#define F_ALLOW_TAGS 0x00004000UL
38#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing 39#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
39 40
40#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 41#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
41 42
42#define INIT_SIZE 32 // initial scalar size to be allocated 43#define INIT_SIZE 32 // initial scalar size to be allocated
43#define INDENT_STEP 3 // spaces per indentation level 44#define INDENT_STEP 3 // spaces per indentation level
44 45
45#define SHORT_STRING_LEN 16384 // special-case strings of up to this size 46#define SHORT_STRING_LEN 16384 // special-case strings of up to this size
47
48#define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8)
46 49
47#define SB do { 50#define SB do {
48#define SE } while (0) 51#define SE } while (0)
49 52
50#if __GNUC__ >= 3 53#if __GNUC__ >= 3
70#else 73#else
71# define JSON_SLOW 0 74# define JSON_SLOW 0
72# define JSON_STASH json_stash 75# define JSON_STASH json_stash
73#endif 76#endif
74 77
78// the amount of HEs to allocate on the stack, when sorting keys
79#define STACK_HES 64
80
75static HV *json_stash, *json_boolean_stash; // JSON::XS:: 81static HV *json_stash, *types_boolean_stash; // JSON::XS::
76static SV *json_true, *json_false; 82static SV *types_true, *types_false, *sv_json;
77 83
78enum { 84enum {
79 INCR_M_WS = 0, // initial whitespace skipping, must be 0 85 INCR_M_WS = 0, // initial whitespace skipping, must be 0
80 INCR_M_STR, // inside string 86 INCR_M_STR, // inside string
81 INCR_M_BS, // inside backslash 87 INCR_M_BS, // inside backslash
192///////////////////////////////////////////////////////////////////////////// 198/////////////////////////////////////////////////////////////////////////////
193// fp hell 199// fp hell
194 200
195// scan a group of digits, and a trailing exponent 201// scan a group of digits, and a trailing exponent
196static void 202static void
197json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp) 203json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth)
198{ 204{
199 UV uaccum = 0; 205 UV uaccum = 0;
200 int eaccum = 0; 206 int eaccum = 0;
207
208 // if we recurse too deep, skip all remaining digits
209 // to avoid a stack overflow attack
210 if (expect_false (--maxdepth <= 0))
211 while (((U8)*s - '0') < 10)
212 ++s;
201 213
202 for (;;) 214 for (;;)
203 { 215 {
204 U8 dig = (U8)*s - '0'; 216 U8 dig = (U8)*s - '0';
205 217
206 if (expect_false (dig >= 10)) 218 if (expect_false (dig >= 10))
207 { 219 {
208 if (dig == (U8)((U8)'.' - (U8)'0')) 220 if (dig == (U8)((U8)'.' - (U8)'0'))
209 { 221 {
210 ++s; 222 ++s;
211 json_atof_scan1 (s, accum, expo, 1); 223 json_atof_scan1 (s, accum, expo, 1, maxdepth);
212 } 224 }
213 else if ((dig | ' ') == 'e' - '0') 225 else if ((dig | ' ') == 'e' - '0')
214 { 226 {
215 int exp2 = 0; 227 int exp2 = 0;
216 int neg = 0; 228 int neg = 0;
242 // if we have too many digits, then recurse for more 254 // if we have too many digits, then recurse for more
243 // we actually do this for rather few digits 255 // we actually do this for rather few digits
244 if (uaccum >= (UV_MAX - 9) / 10) 256 if (uaccum >= (UV_MAX - 9) / 10)
245 { 257 {
246 if (postdp) *expo -= eaccum; 258 if (postdp) *expo -= eaccum;
247 json_atof_scan1 (s, accum, expo, postdp); 259 json_atof_scan1 (s, accum, expo, postdp, maxdepth);
248 if (postdp) *expo += eaccum; 260 if (postdp) *expo += eaccum;
249 261
250 break; 262 break;
251 } 263 }
252 } 264 }
253 265
266 // this relies greatly on the quality of the pow ()
267 // implementation of the platform, but a good
268 // implementation is hard to beat.
269 // (IEEE 754 conformant ones are required to be exact)
254 if (postdp) *expo -= eaccum; 270 if (postdp) *expo -= eaccum;
255 *accum += uaccum * pow (10., *expo); 271 *accum += uaccum * Perl_pow (10., *expo);
256 *expo += eaccum; 272 *expo += eaccum;
257} 273}
258 274
259static NV 275static NV
260json_atof (const char *s) 276json_atof (const char *s)
267 { 283 {
268 ++s; 284 ++s;
269 neg = 1; 285 neg = 1;
270 } 286 }
271 287
288 // a recursion depth of ten gives us >>500 bits
272 json_atof_scan1 (s, &accum, &expo, 0); 289 json_atof_scan1 (s, &accum, &expo, 0, 10);
273 290
274 return neg ? -accum : accum; 291 return neg ? -accum : accum;
275} 292}
276///////////////////////////////////////////////////////////////////////////// 293/////////////////////////////////////////////////////////////////////////////
277// encoder 294// encoder
467 484
468 if (enc->indent >= enc->json.max_depth) 485 if (enc->indent >= enc->json.max_depth)
469 croak (ERR_NESTING_EXCEEDED); 486 croak (ERR_NESTING_EXCEEDED);
470 487
471 encode_ch (enc, '['); 488 encode_ch (enc, '[');
472 489
473 if (len >= 0) 490 if (len >= 0)
474 { 491 {
475 encode_nl (enc); ++enc->indent; 492 encode_nl (enc); ++enc->indent;
476 493
477 for (i = 0; i <= len; ++i) 494 for (i = 0; i <= len; ++i)
489 encode_comma (enc); 506 encode_comma (enc);
490 } 507 }
491 508
492 encode_nl (enc); --enc->indent; encode_indent (enc); 509 encode_nl (enc); --enc->indent; encode_indent (enc);
493 } 510 }
494 511
495 encode_ch (enc, ']'); 512 encode_ch (enc, ']');
496} 513}
497 514
498static void 515static void
499encode_hk (enc_t *enc, HE *he) 516encode_hk (enc_t *enc, HE *he)
503 if (HeKLEN (he) == HEf_SVKEY) 520 if (HeKLEN (he) == HEf_SVKEY)
504 { 521 {
505 SV *sv = HeSVKEY (he); 522 SV *sv = HeSVKEY (he);
506 STRLEN len; 523 STRLEN len;
507 char *str; 524 char *str;
508 525
509 SvGETMAGIC (sv); 526 SvGETMAGIC (sv);
510 str = SvPV (sv, len); 527 str = SvPV (sv, len);
511 528
512 encode_str (enc, str, len, SvUTF8 (sv)); 529 encode_str (enc, str, len, SvUTF8 (sv));
513 } 530 }
579 } 596 }
580 597
581 if (count) 598 if (count)
582 { 599 {
583 int i, fast = 1; 600 int i, fast = 1;
584#if defined(__BORLANDC__) || defined(_MSC_VER) 601 HE *hes_stack [STACK_HES];
585 HE **hes = _alloca (count * sizeof (HE)); 602 HE **hes = hes_stack;
586#else 603
587 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 604 // allocate larger arrays on the heap
588#endif 605 if (count > STACK_HES)
606 {
607 SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes)));
608 hes = (HE **)SvPVX (sv);
609 }
589 610
590 i = 0; 611 i = 0;
591 while ((he = hv_iternext (hv))) 612 while ((he = hv_iternext (hv)))
592 { 613 {
593 hes [i++] = he; 614 hes [i++] = he;
662// encode objects, arrays and special \0=false and \1=true values. 683// encode objects, arrays and special \0=false and \1=true values.
663static void 684static void
664encode_rv (enc_t *enc, SV *sv) 685encode_rv (enc_t *enc, SV *sv)
665{ 686{
666 svtype svt; 687 svtype svt;
688 GV *method;
667 689
668 SvGETMAGIC (sv); 690 SvGETMAGIC (sv);
669 svt = SvTYPE (sv); 691 svt = SvTYPE (sv);
670 692
671 if (expect_false (SvOBJECT (sv))) 693 if (expect_false (SvOBJECT (sv)))
672 { 694 {
673 HV *stash = !JSON_SLOW || json_boolean_stash 695 HV *boolean_stash = !JSON_SLOW || types_boolean_stash
674 ? json_boolean_stash 696 ? types_boolean_stash
675 : gv_stashpv ("JSON::XS::Boolean", 1); 697 : gv_stashpv ("Types::Serialiser::Boolean", 1);
698 HV *stash = SvSTASH (sv);
676 699
677 if (SvSTASH (sv) == stash) 700 if (stash == boolean_stash)
678 { 701 {
679 if (SvIV (sv)) 702 if (SvIV (sv))
680 encode_str (enc, "true", 4, 0); 703 encode_str (enc, "true", 4, 0);
681 else 704 else
682 encode_str (enc, "false", 5, 0); 705 encode_str (enc, "false", 5, 0);
683 } 706 }
707 else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0)))
708 {
709 int count;
710 dSP;
711
712 ENTER; SAVETMPS; PUSHMARK (SP);
713 EXTEND (SP, 2);
714 // we re-bless the reference to get overload and other niceties right
715 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
716 PUSHs (sv_json);
717
718 PUTBACK;
719 count = call_sv ((SV *)GvCV (method), G_ARRAY);
720 SPAGAIN;
721
722 // catch this surprisingly common error
723 if (SvROK (TOPs) && SvRV (TOPs) == sv)
724 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
725
726 encode_ch (enc, '(');
727 encode_ch (enc, '"');
728 encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash));
729 encode_ch (enc, '"');
730 encode_ch (enc, ')');
731 encode_ch (enc, '[');
732
733 while (count)
734 {
735 encode_sv (enc, SP[1 - count--]);
736
737 if (count)
738 encode_ch (enc, ',');
739 }
740
741 encode_ch (enc, ']');
742
743 PUTBACK;
744
745 FREETMPS; LEAVE;
746 }
747 else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0)))
748 {
749 dSP;
750
751 ENTER; SAVETMPS; PUSHMARK (SP);
752 // we re-bless the reference to get overload and other niceties right
753 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
754
755 // calling with G_SCALAR ensures that we always get a 1 return value
756 PUTBACK;
757 call_sv ((SV *)GvCV (method), G_SCALAR);
758 SPAGAIN;
759
760 // catch this surprisingly common error
761 if (SvROK (TOPs) && SvRV (TOPs) == sv)
762 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
763
764 sv = POPs;
765 PUTBACK;
766
767 encode_sv (enc, sv);
768
769 FREETMPS; LEAVE;
770 }
771 else if (enc->json.flags & F_ALLOW_BLESSED)
772 encode_str (enc, "null", 4, 0);
684 else 773 else
685 {
686#if 0
687 if (0 && sv_derived_from (rv, "JSON::Literal"))
688 {
689 // not yet
690 }
691#endif
692 if (enc->json.flags & F_CONV_BLESSED)
693 {
694 // we re-bless the reference to get overload and other niceties right
695 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
696
697 if (to_json)
698 {
699 dSP;
700
701 ENTER; SAVETMPS; PUSHMARK (SP);
702 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
703
704 // calling with G_SCALAR ensures that we always get a 1 return value
705 PUTBACK;
706 call_sv ((SV *)GvCV (to_json), G_SCALAR);
707 SPAGAIN;
708
709 // catch this surprisingly common error
710 if (SvROK (TOPs) && SvRV (TOPs) == sv)
711 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
712
713 sv = POPs;
714 PUTBACK;
715
716 encode_sv (enc, sv);
717
718 FREETMPS; LEAVE;
719 }
720 else if (enc->json.flags & F_ALLOW_BLESSED)
721 encode_str (enc, "null", 4, 0);
722 else
723 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
724 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
725 }
726 else if (enc->json.flags & F_ALLOW_BLESSED)
727 encode_str (enc, "null", 4, 0);
728 else
729 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", 774 croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)",
730 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 775 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
731 }
732 } 776 }
733 else if (svt == SVt_PVHV) 777 else if (svt == SVt_PVHV)
734 encode_hv (enc, (HV *)sv); 778 encode_hv (enc, (HV *)sv);
735 else if (svt == SVt_PVAV) 779 else if (svt == SVt_PVAV)
736 encode_av (enc, (AV *)sv); 780 encode_av (enc, (AV *)sv);
812 } 856 }
813 else 857 else
814 { 858 {
815 // large integer, use the (rather slow) snprintf way. 859 // large integer, use the (rather slow) snprintf way.
816 need (enc, IVUV_MAXCHARS); 860 need (enc, IVUV_MAXCHARS);
817 enc->cur += 861 enc->cur +=
818 SvIsUV(sv) 862 SvIsUV(sv)
819 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) 863 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv))
820 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); 864 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv));
821 } 865 }
822 } 866 }
823 else if (SvROK (sv)) 867 else if (SvROK (sv))
824 encode_rv (enc, SvRV (sv)); 868 encode_rv (enc, SvRV (sv));
825 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) 869 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN)
826 encode_str (enc, "null", 4, 0); 870 encode_str (enc, "null", 4, 0);
827 else 871 else
828 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 872 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data",
829 SvPV_nolen (sv), SvFLAGS (sv)); 873 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
830} 874}
831 875
832static SV * 876static SV *
833encode_json (SV *scalar, JSON *json) 877encode_json (SV *scalar, JSON *json)
834{ 878{
1246 if (*dec->cur == ']') 1290 if (*dec->cur == ']')
1247 { 1291 {
1248 ++dec->cur; 1292 ++dec->cur;
1249 break; 1293 break;
1250 } 1294 }
1251 1295
1252 if (*dec->cur != ',') 1296 if (*dec->cur != ',')
1253 ERR (", or ] expected while parsing array"); 1297 ERR (", or ] expected while parsing array");
1254 1298
1255 ++dec->cur; 1299 ++dec->cur;
1256 1300
1391 dSP; 1435 dSP;
1392 int count; 1436 int count;
1393 1437
1394 ENTER; SAVETMPS; PUSHMARK (SP); 1438 ENTER; SAVETMPS; PUSHMARK (SP);
1395 XPUSHs (HeVAL (he)); 1439 XPUSHs (HeVAL (he));
1440 sv_2mortal (sv);
1396 1441
1397 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1442 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1398 1443
1399 if (count == 1) 1444 if (count == 1)
1400 { 1445 {
1401 sv = newSVsv (POPs); 1446 sv = newSVsv (POPs);
1402 FREETMPS; LEAVE; 1447 FREETMPS; LEAVE;
1403 return sv; 1448 return sv;
1404 } 1449 }
1405 1450
1451 SvREFCNT_inc (sv);
1406 FREETMPS; LEAVE; 1452 FREETMPS; LEAVE;
1407 } 1453 }
1408 } 1454 }
1409 1455
1410 if (dec->json.cb_object) 1456 if (dec->json.cb_object)
1436 DEC_DEC_DEPTH; 1482 DEC_DEC_DEPTH;
1437 return 0; 1483 return 0;
1438} 1484}
1439 1485
1440static SV * 1486static SV *
1487decode_tag (dec_t *dec)
1488{
1489 SV *tag = 0;
1490 SV *val = 0;
1491
1492 if (!(dec->json.flags & F_ALLOW_TAGS))
1493 ERR ("malformed JSON string, neither array, object, number, string or atom");
1494
1495 ++dec->cur;
1496
1497 tag = decode_sv (dec);
1498 if (!tag)
1499 goto fail;
1500
1501 if (!SvPOK (tag))
1502 ERR ("malformed JSON string, (tag) must be a string");
1503
1504 if (*dec->cur != ')')
1505 ERR (") expected after tag");
1506
1507 ++dec->cur;
1508
1509 val = decode_sv (dec);
1510 if (!val)
1511 goto fail;
1512
1513 if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV)
1514 ERR ("malformed JSON string, tag value must be an array");
1515
1516 {
1517 AV *av = (AV *)SvRV (val);
1518 int i, len = av_len (av) + 1;
1519 HV *stash = gv_stashsv (tag, 0);
1520 SV *sv;
1521
1522 if (!stash)
1523 ERR ("cannot decode perl-object (package does not exist)");
1524
1525 GV *method = gv_fetchmethod_autoload (stash, "THAW", 0);
1526
1527 if (!method)
1528 ERR ("cannot decode perl-object (package does not have a THAW method)");
1529
1530 dSP;
1531
1532 ENTER; SAVETMPS; PUSHMARK (SP);
1533 EXTEND (SP, len + 2);
1534 // we re-bless the reference to get overload and other niceties right
1535 PUSHs (tag);
1536 PUSHs (sv_json);
1537
1538 for (i = 0; i < len; ++i)
1539 PUSHs (*av_fetch (av, i, 1));
1540
1541 PUTBACK;
1542 call_sv ((SV *)GvCV (method), G_SCALAR);
1543 SPAGAIN;
1544
1545 SvREFCNT_dec (tag);
1546 SvREFCNT_dec (val);
1547 sv = SvREFCNT_inc (POPs);
1548
1549 PUTBACK;
1550
1551 FREETMPS; LEAVE;
1552
1553 return sv;
1554 }
1555
1556fail:
1557 SvREFCNT_dec (tag);
1558 SvREFCNT_dec (val);
1559 return 0;
1560}
1561
1562static SV *
1441decode_sv (dec_t *dec) 1563decode_sv (dec_t *dec)
1442{ 1564{
1443 // the beauty of JSON: you need exactly one character lookahead 1565 // the beauty of JSON: you need exactly one character lookahead
1444 // to parse everything. 1566 // to parse everything.
1445 switch (*dec->cur) 1567 switch (*dec->cur)
1446 { 1568 {
1447 case '"': ++dec->cur; return decode_str (dec); 1569 case '"': ++dec->cur; return decode_str (dec);
1448 case '[': ++dec->cur; return decode_av (dec); 1570 case '[': ++dec->cur; return decode_av (dec);
1449 case '{': ++dec->cur; return decode_hv (dec); 1571 case '{': ++dec->cur; return decode_hv (dec);
1572 case '(': return decode_tag (dec);
1450 1573
1451 case '-': 1574 case '-':
1452 case '0': case '1': case '2': case '3': case '4': 1575 case '0': case '1': case '2': case '3': case '4':
1453 case '5': case '6': case '7': case '8': case '9': 1576 case '5': case '6': case '7': case '8': case '9':
1454 return decode_num (dec); 1577 return decode_num (dec);
1456 case 't': 1579 case 't':
1457 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1580 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1458 { 1581 {
1459 dec->cur += 4; 1582 dec->cur += 4;
1460#if JSON_SLOW 1583#if JSON_SLOW
1461 json_true = get_bool ("JSON::XS::true"); 1584 types_true = get_bool ("Types::Serialiser::true");
1462#endif 1585#endif
1463 return newSVsv (json_true); 1586 return newSVsv (types_true);
1464 } 1587 }
1465 else 1588 else
1466 ERR ("'true' expected"); 1589 ERR ("'true' expected");
1467 1590
1468 break; 1591 break;
1470 case 'f': 1593 case 'f':
1471 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1594 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1472 { 1595 {
1473 dec->cur += 5; 1596 dec->cur += 5;
1474#if JSON_SLOW 1597#if JSON_SLOW
1475 json_false = get_bool ("JSON::XS::false"); 1598 types_false = get_bool ("Types::Serialiser::false");
1476#endif 1599#endif
1477 return newSVsv (json_false); 1600 return newSVsv (types_false);
1478 } 1601 }
1479 else 1602 else
1480 ERR ("'false' expected"); 1603 ERR ("'false' expected");
1481 1604
1482 break; 1605 break;
1491 ERR ("'null' expected"); 1614 ERR ("'null' expected");
1492 1615
1493 break; 1616 break;
1494 1617
1495 default: 1618 default:
1496 ERR ("malformed JSON string, neither array, object, number, string or atom"); 1619 ERR ("malformed JSON string, neither tag, array, object, number, string or atom");
1497 break; 1620 break;
1498 } 1621 }
1499 1622
1500fail: 1623fail:
1501 return 0; 1624 return 0;
1506{ 1629{
1507 dec_t dec; 1630 dec_t dec;
1508 SV *sv; 1631 SV *sv;
1509 1632
1510 /* work around bugs in 5.10 where manipulating magic values 1633 /* work around bugs in 5.10 where manipulating magic values
1511 * will perl ignore the magic in subsequent accesses 1634 * makes perl ignore the magic in subsequent accesses.
1635 * also make a copy of non-PV values, to get them into a clean
1636 * state (SvPV should do that, but it's buggy, see below).
1512 */ 1637 */
1513 /*SvGETMAGIC (string);*/ 1638 /*SvGETMAGIC (string);*/
1514 if (SvMAGICAL (string)) 1639 if (SvMAGICAL (string) || !SvPOK (string))
1515 string = sv_2mortal (newSVsv (string)); 1640 string = sv_2mortal (newSVsv (string));
1516 1641
1517 SvUPGRADE (string, SVt_PV); 1642 SvUPGRADE (string, SVt_PV);
1518 1643
1519 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1644 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1536 if (offset > json->max_size && json->max_size) 1661 if (offset > json->max_size && json->max_size)
1537 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1662 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1538 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1663 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1539 } 1664 }
1540 1665
1541 if (json->flags & F_UTF8) 1666 if (DECODE_WANTS_OCTETS (json))
1542 sv_utf8_downgrade (string, 0); 1667 sv_utf8_downgrade (string, 0);
1543 else 1668 else
1544 sv_utf8_upgrade (string); 1669 sv_utf8_upgrade (string);
1545 1670
1546 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1671 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1588 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1713 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1589 LEAVE; 1714 LEAVE;
1590 1715
1591 croak ("%s, at character offset %d (before \"%s\")", 1716 croak ("%s, at character offset %d (before \"%s\")",
1592 dec.err, 1717 dec.err,
1593 ptr_to_index (string, dec.cur), 1718 (int)ptr_to_index (string, dec.cur),
1594 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1719 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1595 } 1720 }
1596 1721
1597 sv = sv_2mortal (sv); 1722 sv = sv_2mortal (sv);
1598 1723
1726 self->incr_mode = INCR_M_STR; 1851 self->incr_mode = INCR_M_STR;
1727 goto incr_m_str; 1852 goto incr_m_str;
1728 1853
1729 case '[': 1854 case '[':
1730 case '{': 1855 case '{':
1856 case '(':
1731 if (++self->incr_nest > self->max_depth) 1857 if (++self->incr_nest > self->max_depth)
1732 croak (ERR_NESTING_EXCEEDED); 1858 croak (ERR_NESTING_EXCEEDED);
1733 break; 1859 break;
1734 1860
1735 case ']': 1861 case ']':
1736 case '}': 1862 case '}':
1737 if (--self->incr_nest <= 0) 1863 if (--self->incr_nest <= 0)
1738 goto interrupt; 1864 goto interrupt;
1865 break;
1866
1867 case ')':
1868 --self->incr_nest;
1739 break; 1869 break;
1740 1870
1741 case '#': 1871 case '#':
1742 self->incr_mode = INCR_M_C1; 1872 self->incr_mode = INCR_M_C1;
1743 goto incr_m_c; 1873 goto incr_m_c;
1769 i >= '0' && i <= '9' ? i - '0' 1899 i >= '0' && i <= '9' ? i - '0'
1770 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1900 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1771 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1901 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1772 : -1; 1902 : -1;
1773 1903
1774 json_stash = gv_stashpv ("JSON::XS" , 1); 1904 json_stash = gv_stashpv ("JSON::XS" , 1);
1775 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1905 types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
1776 1906
1777 json_true = get_bool ("JSON::XS::true"); 1907 types_true = get_bool ("Types::Serialiser::true");
1778 json_false = get_bool ("JSON::XS::false"); 1908 types_false = get_bool ("Types::Serialiser::false");
1909
1910 sv_json = newSVpv ("JSON", 0);
1911 SvREADONLY_on (sv_json);
1779 1912
1780 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ 1913 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1781} 1914}
1782 1915
1783PROTOTYPES: DISABLE 1916PROTOTYPES: DISABLE
1784 1917
1785void CLONE (...) 1918void CLONE (...)
1786 CODE: 1919 CODE:
1787 json_stash = 0; 1920 json_stash = 0;
1788 json_boolean_stash = 0; 1921 types_boolean_stash = 0;
1789 1922
1790void new (char *klass) 1923void new (char *klass)
1791 PPCODE: 1924 PPCODE:
1792{ 1925{
1793 SV *pv = NEWSV (0, sizeof (JSON)); 1926 SV *pv = NEWSV (0, sizeof (JSON));
1794 SvPOK_only (pv); 1927 SvPOK_only (pv);
1795 json_init ((JSON *)SvPVX (pv)); 1928 json_init ((JSON *)SvPVX (pv));
1796 XPUSHs (sv_2mortal (sv_bless ( 1929 XPUSHs (sv_2mortal (sv_bless (
1797 newRV_noinc (pv), 1930 newRV_noinc (pv),
1798 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) 1931 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1813 shrink = F_SHRINK 1946 shrink = F_SHRINK
1814 allow_blessed = F_ALLOW_BLESSED 1947 allow_blessed = F_ALLOW_BLESSED
1815 convert_blessed = F_CONV_BLESSED 1948 convert_blessed = F_CONV_BLESSED
1816 relaxed = F_RELAXED 1949 relaxed = F_RELAXED
1817 allow_unknown = F_ALLOW_UNKNOWN 1950 allow_unknown = F_ALLOW_UNKNOWN
1951 allow_tags = F_ALLOW_TAGS
1818 PPCODE: 1952 PPCODE:
1819{ 1953{
1820 if (enable) 1954 if (enable)
1821 self->flags |= ix; 1955 self->flags |= ix;
1822 else 1956 else
1838 get_shrink = F_SHRINK 1972 get_shrink = F_SHRINK
1839 get_allow_blessed = F_ALLOW_BLESSED 1973 get_allow_blessed = F_ALLOW_BLESSED
1840 get_convert_blessed = F_CONV_BLESSED 1974 get_convert_blessed = F_CONV_BLESSED
1841 get_relaxed = F_RELAXED 1975 get_relaxed = F_RELAXED
1842 get_allow_unknown = F_ALLOW_UNKNOWN 1976 get_allow_unknown = F_ALLOW_UNKNOWN
1977 get_allow_tags = F_ALLOW_TAGS
1843 PPCODE: 1978 PPCODE:
1844 XPUSHs (boolSV (self->flags & ix)); 1979 XPUSHs (boolSV (self->flags & ix));
1845 1980
1846void max_depth (JSON *self, U32 max_depth = 0x80000000UL) 1981void max_depth (JSON *self, U32 max_depth = 0x80000000UL)
1847 PPCODE: 1982 PPCODE:
1875} 2010}
1876 2011
1877void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) 2012void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef)
1878 PPCODE: 2013 PPCODE:
1879{ 2014{
1880 if (!self->cb_sk_object) 2015 if (!self->cb_sk_object)
1881 self->cb_sk_object = newHV (); 2016 self->cb_sk_object = newHV ();
1882 2017
1883 if (SvOK (cb)) 2018 if (SvOK (cb))
1884 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); 2019 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1885 else 2020 else
1896 XPUSHs (ST (0)); 2031 XPUSHs (ST (0));
1897} 2032}
1898 2033
1899void encode (JSON *self, SV *scalar) 2034void encode (JSON *self, SV *scalar)
1900 PPCODE: 2035 PPCODE:
1901 XPUSHs (encode_json (scalar, self)); 2036 PUTBACK; scalar = encode_json (scalar, self); SPAGAIN;
2037 XPUSHs (scalar);
1902 2038
1903void decode (JSON *self, SV *jsonstr) 2039void decode (JSON *self, SV *jsonstr)
1904 PPCODE: 2040 PPCODE:
1905 XPUSHs (decode_json (jsonstr, self, 0)); 2041 PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN;
2042 XPUSHs (jsonstr);
1906 2043
1907void decode_prefix (JSON *self, SV *jsonstr) 2044void decode_prefix (JSON *self, SV *jsonstr)
1908 PPCODE: 2045 PPCODE:
1909{ 2046{
2047 SV *sv;
1910 char *offset; 2048 char *offset;
2049 PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN;
1911 EXTEND (SP, 2); 2050 EXTEND (SP, 2);
1912 PUSHs (decode_json (jsonstr, self, &offset)); 2051 PUSHs (sv);
1913 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); 2052 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1914} 2053}
1915 2054
1916void incr_parse (JSON *self, SV *jsonstr = 0) 2055void incr_parse (JSON *self, SV *jsonstr = 0)
1917 PPCODE: 2056 PPCODE:
1918{ 2057{
1919 if (!self->incr_text) 2058 if (!self->incr_text)
1920 self->incr_text = newSVpvn ("", 0); 2059 self->incr_text = newSVpvn ("", 0);
2060
2061 /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */
2062 if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text))
2063 if (DECODE_WANTS_OCTETS (self))
2064 {
2065 if (self->incr_pos)
2066 self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text),
2067 (U8 *)SvPVX (self->incr_text) + self->incr_pos);
2068
2069 sv_utf8_downgrade (self->incr_text, 0);
2070 }
2071 else
2072 {
2073 sv_utf8_upgrade (self->incr_text);
2074
2075 if (self->incr_pos)
2076 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
2077 - (U8 *)SvPVX (self->incr_text);
2078 }
1921 2079
1922 // append data, if any 2080 // append data, if any
1923 if (jsonstr) 2081 if (jsonstr)
1924 { 2082 {
2083 /* make sure both strings have same encoding */
2084 if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text))
1925 if (SvUTF8 (jsonstr)) 2085 if (SvUTF8 (jsonstr))
2086 sv_utf8_downgrade (jsonstr, 0);
1926 { 2087 else
1927 if (!SvUTF8 (self->incr_text))
1928 {
1929 /* utf-8-ness differs, need to upgrade */
1930 sv_utf8_upgrade (self->incr_text);
1931
1932 if (self->incr_pos)
1933 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1934 - (U8 *)SvPVX (self->incr_text);
1935 }
1936 }
1937 else if (SvUTF8 (self->incr_text))
1938 sv_utf8_upgrade (jsonstr); 2088 sv_utf8_upgrade (jsonstr);
1939 2089
2090 /* and then just blindly append */
1940 { 2091 {
1941 STRLEN len; 2092 STRLEN len;
1942 const char *str = SvPV (jsonstr, len); 2093 const char *str = SvPV (jsonstr, len);
1943 STRLEN cur = SvCUR (self->incr_text); 2094 STRLEN cur = SvCUR (self->incr_text);
1944 2095
1952 } 2103 }
1953 2104
1954 if (GIMME_V != G_VOID) 2105 if (GIMME_V != G_VOID)
1955 do 2106 do
1956 { 2107 {
2108 SV *sv;
1957 char *offset; 2109 char *offset;
1958 2110
1959 if (!INCR_DONE (self)) 2111 if (!INCR_DONE (self))
1960 { 2112 {
1961 incr_parse (self); 2113 incr_parse (self);
1963 if (self->incr_pos > self->max_size && self->max_size) 2115 if (self->incr_pos > self->max_size && self->max_size)
1964 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 2116 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1965 (unsigned long)self->incr_pos, (unsigned long)self->max_size); 2117 (unsigned long)self->incr_pos, (unsigned long)self->max_size);
1966 2118
1967 if (!INCR_DONE (self)) 2119 if (!INCR_DONE (self))
2120 {
2121 // as an optimisation, do not accumulate white space in the incr buffer
2122 if (self->incr_mode == INCR_M_WS && self->incr_pos)
2123 {
2124 self->incr_pos = 0;
2125 SvCUR_set (self->incr_text, 0);
2126 }
2127
1968 break; 2128 break;
2129 }
1969 } 2130 }
1970 2131
1971 XPUSHs (decode_json (self->incr_text, self, &offset)); 2132 PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN;
2133 XPUSHs (sv);
1972 2134
1973 self->incr_pos -= offset - SvPVX (self->incr_text); 2135 self->incr_pos -= offset - SvPVX (self->incr_text);
1974 self->incr_nest = 0; 2136 self->incr_nest = 0;
1975 self->incr_mode = 0; 2137 self->incr_mode = 0;
1976 2138
2020 SvREFCNT_dec (self->incr_text); 2182 SvREFCNT_dec (self->incr_text);
2021 2183
2022PROTOTYPES: ENABLE 2184PROTOTYPES: ENABLE
2023 2185
2024void encode_json (SV *scalar) 2186void encode_json (SV *scalar)
2025 ALIAS:
2026 to_json_ = 0
2027 encode_json = F_UTF8
2028 PPCODE: 2187 PPCODE:
2029{ 2188{
2030 JSON json; 2189 JSON json;
2031 json_init (&json); 2190 json_init (&json);
2032 json.flags |= ix; 2191 json.flags |= F_UTF8;
2033 XPUSHs (encode_json (scalar, &json)); 2192 PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN;
2193 XPUSHs (scalar);
2034} 2194}
2035 2195
2036void decode_json (SV *jsonstr) 2196void decode_json (SV *jsonstr)
2037 ALIAS:
2038 from_json_ = 0
2039 decode_json = F_UTF8
2040 PPCODE: 2197 PPCODE:
2041{ 2198{
2042 JSON json; 2199 JSON json;
2043 json_init (&json); 2200 json_init (&json);
2044 json.flags |= ix; 2201 json.flags |= F_UTF8;
2045 XPUSHs (decode_json (jsonstr, &json, 0)); 2202 PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN;
2203 XPUSHs (jsonstr);
2046} 2204}
2047 2205

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines