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.106 by root, Tue Jan 19 01:36:34 2010 UTC vs.
Revision 1.126 by root, Sun Feb 21 16:18:19 2016 UTC

15 15
16// some old perls do not have this, try to make it work, no 16// some old perls do not have this, try to make it work, no
17// guarantees, though. if it breaks, you get to keep the pieces. 17// guarantees, though. if it breaks, you get to keep the pieces.
18#ifndef UTF8_MAXBYTES 18#ifndef UTF8_MAXBYTES
19# define UTF8_MAXBYTES 13 19# define UTF8_MAXBYTES 13
20#endif
21
22// compatibility with perl <5.18
23#ifndef HvNAMELEN_get
24# define HvNAMELEN_get(hv) strlen (HvNAME (hv))
25#endif
26#ifndef HvNAMELEN
27# define HvNAMELEN(hv) HvNAMELEN_get (hv)
28#endif
29#ifndef HvNAMEUTF8
30# define HvNAMEUTF8(hv) 0
20#endif 31#endif
21 32
22// three extra for rounding, sign, and end of string 33// three extra for rounding, sign, and end of string
23#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) 34#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3)
24 35
33#define F_SHRINK 0x00000200UL 44#define F_SHRINK 0x00000200UL
34#define F_ALLOW_BLESSED 0x00000400UL 45#define F_ALLOW_BLESSED 0x00000400UL
35#define F_CONV_BLESSED 0x00000800UL 46#define F_CONV_BLESSED 0x00000800UL
36#define F_RELAXED 0x00001000UL 47#define F_RELAXED 0x00001000UL
37#define F_ALLOW_UNKNOWN 0x00002000UL 48#define F_ALLOW_UNKNOWN 0x00002000UL
49#define F_ALLOW_TAGS 0x00004000UL
38#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing 50#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
39 51
40#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 52#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
41 53
42#define INIT_SIZE 32 // initial scalar size to be allocated 54#define INIT_SIZE 32 // initial scalar size to be allocated
43#define INDENT_STEP 3 // spaces per indentation level 55#define INDENT_STEP 3 // spaces per indentation level
44 56
45#define SHORT_STRING_LEN 16384 // special-case strings of up to this size 57#define SHORT_STRING_LEN 16384 // special-case strings of up to this size
58
59#define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8)
46 60
47#define SB do { 61#define SB do {
48#define SE } while (0) 62#define SE } while (0)
49 63
50#if __GNUC__ >= 3 64#if __GNUC__ >= 3
65#define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" 79#define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)"
66 80
67#ifdef USE_ITHREADS 81#ifdef USE_ITHREADS
68# define JSON_SLOW 1 82# define JSON_SLOW 1
69# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) 83# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
84# define BOOL_STASH (bool_stash ? bool_stash : gv_stashpv ("Types::Serialiser::Boolean", 1))
70#else 85#else
71# define JSON_SLOW 0 86# define JSON_SLOW 0
72# define JSON_STASH json_stash 87# define JSON_STASH json_stash
88# define BOOL_STASH bool_stash
73#endif 89#endif
74 90
75static HV *json_stash, *json_boolean_stash; // JSON::XS:: 91// the amount of HEs to allocate on the stack, when sorting keys
76static SV *json_true, *json_false; 92#define STACK_HES 64
93
94static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean::
95static SV *bool_true, *bool_false, *sv_json;
77 96
78enum { 97enum {
79 INCR_M_WS = 0, // initial whitespace skipping, must be 0 98 INCR_M_WS = 0, // initial whitespace skipping, must be 0
80 INCR_M_STR, // inside string 99 INCR_M_STR, // inside string
81 INCR_M_BS, // inside backslash 100 INCR_M_BS, // inside backslash
258 } 277 }
259 278
260 // this relies greatly on the quality of the pow () 279 // this relies greatly on the quality of the pow ()
261 // implementation of the platform, but a good 280 // implementation of the platform, but a good
262 // implementation is hard to beat. 281 // implementation is hard to beat.
282 // (IEEE 754 conformant ones are required to be exact)
263 if (postdp) *expo -= eaccum; 283 if (postdp) *expo -= eaccum;
264 *accum += uaccum * Perl_pow (10., *expo); 284 *accum += uaccum * Perl_pow (10., *expo);
265 *expo += eaccum; 285 *expo += eaccum;
266} 286}
267 287
281 // a recursion depth of ten gives us >>500 bits 301 // a recursion depth of ten gives us >>500 bits
282 json_atof_scan1 (s, &accum, &expo, 0, 10); 302 json_atof_scan1 (s, &accum, &expo, 0, 10);
283 303
284 return neg ? -accum : accum; 304 return neg ? -accum : accum;
285} 305}
306
307// target of scalar reference is bool? -1 == nope, 0 == false, 1 == true
308static int
309ref_bool_type (SV *sv)
310{
311 svtype svt = SvTYPE (sv);
312
313 if (svt < SVt_PVAV)
314 {
315 STRLEN len = 0;
316 char *pv = svt ? SvPV (sv, len) : 0;
317
318 if (len == 1)
319 if (*pv == '1')
320 return 1;
321 else if (*pv == '0')
322 return 0;
323
324 }
325
326 return -1;
327}
328
329// returns whether scalar is not a reference in the sense of allow_nonref
330static int
331json_nonref (SV *scalar)
332{
333 if (!SvROK (scalar))
334 return 1;
335
336 scalar = SvRV (scalar);
337
338 if (SvSTASH (scalar) == bool_stash)
339 return 1;
340
341 if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0)
342 return 1;
343
344 return 0;
345}
346
286///////////////////////////////////////////////////////////////////////////// 347/////////////////////////////////////////////////////////////////////////////
287// encoder 348// encoder
288 349
289// structure used for encoding JSON 350// structure used for encoding JSON
290typedef struct 351typedef struct
477 538
478 if (enc->indent >= enc->json.max_depth) 539 if (enc->indent >= enc->json.max_depth)
479 croak (ERR_NESTING_EXCEEDED); 540 croak (ERR_NESTING_EXCEEDED);
480 541
481 encode_ch (enc, '['); 542 encode_ch (enc, '[');
482 543
483 if (len >= 0) 544 if (len >= 0)
484 { 545 {
485 encode_nl (enc); ++enc->indent; 546 encode_nl (enc); ++enc->indent;
486 547
487 for (i = 0; i <= len; ++i) 548 for (i = 0; i <= len; ++i)
499 encode_comma (enc); 560 encode_comma (enc);
500 } 561 }
501 562
502 encode_nl (enc); --enc->indent; encode_indent (enc); 563 encode_nl (enc); --enc->indent; encode_indent (enc);
503 } 564 }
504 565
505 encode_ch (enc, ']'); 566 encode_ch (enc, ']');
506} 567}
507 568
508static void 569static void
509encode_hk (enc_t *enc, HE *he) 570encode_hk (enc_t *enc, HE *he)
513 if (HeKLEN (he) == HEf_SVKEY) 574 if (HeKLEN (he) == HEf_SVKEY)
514 { 575 {
515 SV *sv = HeSVKEY (he); 576 SV *sv = HeSVKEY (he);
516 STRLEN len; 577 STRLEN len;
517 char *str; 578 char *str;
518 579
519 SvGETMAGIC (sv); 580 SvGETMAGIC (sv);
520 str = SvPV (sv, len); 581 str = SvPV (sv, len);
521 582
522 encode_str (enc, str, len, SvUTF8 (sv)); 583 encode_str (enc, str, len, SvUTF8 (sv));
523 } 584 }
589 } 650 }
590 651
591 if (count) 652 if (count)
592 { 653 {
593 int i, fast = 1; 654 int i, fast = 1;
594#if defined(__BORLANDC__) || defined(_MSC_VER) 655 HE *hes_stack [STACK_HES];
595 HE **hes = _alloca (count * sizeof (HE)); 656 HE **hes = hes_stack;
596#else 657
597 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 658 // allocate larger arrays on the heap
598#endif 659 if (count > STACK_HES)
660 {
661 SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes)));
662 hes = (HE **)SvPVX (sv);
663 }
599 664
600 i = 0; 665 i = 0;
601 while ((he = hv_iternext (hv))) 666 while ((he = hv_iternext (hv)))
602 { 667 {
603 hes [i++] = he; 668 hes [i++] = he;
672// encode objects, arrays and special \0=false and \1=true values. 737// encode objects, arrays and special \0=false and \1=true values.
673static void 738static void
674encode_rv (enc_t *enc, SV *sv) 739encode_rv (enc_t *enc, SV *sv)
675{ 740{
676 svtype svt; 741 svtype svt;
742 GV *method;
677 743
678 SvGETMAGIC (sv); 744 SvGETMAGIC (sv);
679 svt = SvTYPE (sv); 745 svt = SvTYPE (sv);
680 746
681 if (expect_false (SvOBJECT (sv))) 747 if (expect_false (SvOBJECT (sv)))
682 { 748 {
683 HV *stash = !JSON_SLOW || json_boolean_stash 749 HV *stash = SvSTASH (sv);
684 ? json_boolean_stash
685 : gv_stashpv ("JSON::XS::Boolean", 1);
686 750
687 if (SvSTASH (sv) == stash) 751 if (stash == bool_stash)
688 { 752 {
689 if (SvIV (sv)) 753 if (SvIV (sv))
690 encode_str (enc, "true", 4, 0); 754 encode_str (enc, "true", 4, 0);
691 else 755 else
692 encode_str (enc, "false", 5, 0); 756 encode_str (enc, "false", 5, 0);
693 } 757 }
758 else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0)))
759 {
760 int count;
761 dSP;
762
763 ENTER; SAVETMPS;
764 SAVESTACK_POS ();
765 PUSHMARK (SP);
766 EXTEND (SP, 2);
767 // we re-bless the reference to get overload and other niceties right
768 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
769 PUSHs (sv_json);
770
771 PUTBACK;
772 count = call_sv ((SV *)GvCV (method), G_ARRAY);
773 SPAGAIN;
774
775 // catch this surprisingly common error
776 if (SvROK (TOPs) && SvRV (TOPs) == sv)
777 croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
778
779 encode_ch (enc, '(');
780 encode_ch (enc, '"');
781 encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash));
782 encode_ch (enc, '"');
783 encode_ch (enc, ')');
784 encode_ch (enc, '[');
785
786 while (count)
787 {
788 encode_sv (enc, SP[1 - count--]);
789
790 if (count)
791 encode_ch (enc, ',');
792 }
793
794 encode_ch (enc, ']');
795
796 FREETMPS; LEAVE;
797 }
798 else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0)))
799 {
800 dSP;
801
802 ENTER; SAVETMPS;
803 PUSHMARK (SP);
804 // we re-bless the reference to get overload and other niceties right
805 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
806
807 // calling with G_SCALAR ensures that we always get a 1 return value
808 PUTBACK;
809 call_sv ((SV *)GvCV (method), G_SCALAR);
810 SPAGAIN;
811
812 // catch this surprisingly common error
813 if (SvROK (TOPs) && SvRV (TOPs) == sv)
814 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
815
816 sv = POPs;
817 PUTBACK;
818
819 encode_sv (enc, sv);
820
821 FREETMPS; LEAVE;
822 }
823 else if (enc->json.flags & F_ALLOW_BLESSED)
824 encode_str (enc, "null", 4, 0);
694 else 825 else
695 {
696#if 0
697 if (0 && sv_derived_from (rv, "JSON::Literal"))
698 {
699 // not yet
700 }
701#endif
702 if (enc->json.flags & F_CONV_BLESSED)
703 {
704 // we re-bless the reference to get overload and other niceties right
705 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
706
707 if (to_json)
708 {
709 dSP;
710
711 ENTER; SAVETMPS; PUSHMARK (SP);
712 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
713
714 // calling with G_SCALAR ensures that we always get a 1 return value
715 PUTBACK;
716 call_sv ((SV *)GvCV (to_json), G_SCALAR);
717 SPAGAIN;
718
719 // catch this surprisingly common error
720 if (SvROK (TOPs) && SvRV (TOPs) == sv)
721 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
722
723 sv = POPs;
724 PUTBACK;
725
726 encode_sv (enc, sv);
727
728 FREETMPS; LEAVE;
729 }
730 else if (enc->json.flags & F_ALLOW_BLESSED)
731 encode_str (enc, "null", 4, 0);
732 else
733 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
734 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
735 }
736 else if (enc->json.flags & F_ALLOW_BLESSED)
737 encode_str (enc, "null", 4, 0);
738 else
739 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", 826 croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)",
740 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 827 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
741 }
742 } 828 }
743 else if (svt == SVt_PVHV) 829 else if (svt == SVt_PVHV)
744 encode_hv (enc, (HV *)sv); 830 encode_hv (enc, (HV *)sv);
745 else if (svt == SVt_PVAV) 831 else if (svt == SVt_PVAV)
746 encode_av (enc, (AV *)sv); 832 encode_av (enc, (AV *)sv);
747 else if (svt < SVt_PVAV) 833 else if (svt < SVt_PVAV)
748 { 834 {
749 STRLEN len = 0; 835 int bool_type = ref_bool_type (sv);
750 char *pv = svt ? SvPV (sv, len) : 0;
751 836
752 if (len == 1 && *pv == '1') 837 if (bool_type == 1)
753 encode_str (enc, "true", 4, 0); 838 encode_str (enc, "true", 4, 0);
754 else if (len == 1 && *pv == '0') 839 else if (bool_type == 0)
755 encode_str (enc, "false", 5, 0); 840 encode_str (enc, "false", 5, 0);
756 else if (enc->json.flags & F_ALLOW_UNKNOWN) 841 else if (enc->json.flags & F_ALLOW_UNKNOWN)
757 encode_str (enc, "null", 4, 0); 842 encode_str (enc, "null", 4, 0);
758 else 843 else
759 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", 844 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
822 } 907 }
823 else 908 else
824 { 909 {
825 // large integer, use the (rather slow) snprintf way. 910 // large integer, use the (rather slow) snprintf way.
826 need (enc, IVUV_MAXCHARS); 911 need (enc, IVUV_MAXCHARS);
827 enc->cur += 912 enc->cur +=
828 SvIsUV(sv) 913 SvIsUV(sv)
829 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) 914 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv))
830 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); 915 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv));
831 } 916 }
832 } 917 }
833 else if (SvROK (sv)) 918 else if (SvROK (sv))
834 encode_rv (enc, SvRV (sv)); 919 encode_rv (enc, SvRV (sv));
835 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) 920 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN)
836 encode_str (enc, "null", 4, 0); 921 encode_str (enc, "null", 4, 0);
837 else 922 else
838 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 923 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data",
839 SvPV_nolen (sv), SvFLAGS (sv)); 924 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
840} 925}
841 926
842static SV * 927static SV *
843encode_json (SV *scalar, JSON *json) 928encode_json (SV *scalar, JSON *json)
844{ 929{
845 enc_t enc; 930 enc_t enc;
846 931
847 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) 932 if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar))
848 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 933 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
849 934
850 enc.json = *json; 935 enc.json = *json;
851 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 936 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
852 enc.cur = SvPVX (enc.sv); 937 enc.cur = SvPVX (enc.sv);
1059 *cur++ = *dec_cur++; 1144 *cur++ = *dec_cur++;
1060 while (--clen); 1145 while (--clen);
1061 1146
1062 utf8 = 1; 1147 utf8 = 1;
1063 } 1148 }
1149 else if (ch == '\t' && dec->json.flags & F_RELAXED)
1150 *cur++ = ch;
1064 else 1151 else
1065 { 1152 {
1066 --dec_cur; 1153 --dec_cur;
1067 1154
1068 if (!ch) 1155 if (!ch)
1256 if (*dec->cur == ']') 1343 if (*dec->cur == ']')
1257 { 1344 {
1258 ++dec->cur; 1345 ++dec->cur;
1259 break; 1346 break;
1260 } 1347 }
1261 1348
1262 if (*dec->cur != ',') 1349 if (*dec->cur != ',')
1263 ERR (", or ] expected while parsing array"); 1350 ERR (", or ] expected while parsing array");
1264 1351
1265 ++dec->cur; 1352 ++dec->cur;
1266 1353
1390 1477
1391 hv_iterinit (hv); 1478 hv_iterinit (hv);
1392 he = hv_iternext (hv); 1479 he = hv_iternext (hv);
1393 hv_iterinit (hv); 1480 hv_iterinit (hv);
1394 1481
1395 // the next line creates a mortal sv each time its called. 1482 // the next line creates a mortal sv each time it's called.
1396 // might want to optimise this for common cases. 1483 // might want to optimise this for common cases.
1397 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); 1484 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1398 1485
1399 if (cb) 1486 if (cb)
1400 { 1487 {
1401 dSP; 1488 dSP;
1402 int count; 1489 int count;
1403 1490
1404 ENTER; SAVETMPS; PUSHMARK (SP); 1491 ENTER; SAVETMPS;
1492 SAVESTACK_POS ();
1493 PUSHMARK (SP);
1405 XPUSHs (HeVAL (he)); 1494 XPUSHs (HeVAL (he));
1495 sv_2mortal (sv);
1406 1496
1407 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1497 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1408 1498
1409 if (count == 1) 1499 if (count == 1)
1410 { 1500 {
1411 sv = newSVsv (POPs); 1501 sv = newSVsv (POPs);
1412 FREETMPS; LEAVE; 1502 FREETMPS; LEAVE;
1413 return sv; 1503 return sv;
1414 } 1504 }
1415 1505
1506 SvREFCNT_inc (sv);
1416 FREETMPS; LEAVE; 1507 FREETMPS; LEAVE;
1417 } 1508 }
1418 } 1509 }
1419 1510
1420 if (dec->json.cb_object) 1511 if (dec->json.cb_object)
1421 { 1512 {
1422 dSP; 1513 dSP;
1423 int count; 1514 int count;
1424 1515
1425 ENTER; SAVETMPS; PUSHMARK (SP); 1516 ENTER; SAVETMPS;
1517 SAVESTACK_POS ();
1518 PUSHMARK (SP);
1426 XPUSHs (sv_2mortal (sv)); 1519 XPUSHs (sv_2mortal (sv));
1427 1520
1428 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1521 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1429 1522
1430 if (count == 1) 1523 if (count == 1)
1446 DEC_DEC_DEPTH; 1539 DEC_DEC_DEPTH;
1447 return 0; 1540 return 0;
1448} 1541}
1449 1542
1450static SV * 1543static SV *
1544decode_tag (dec_t *dec)
1545{
1546 SV *tag = 0;
1547 SV *val = 0;
1548
1549 if (!(dec->json.flags & F_ALLOW_TAGS))
1550 ERR ("malformed JSON string, neither array, object, number, string or atom");
1551
1552 ++dec->cur;
1553
1554 decode_ws (dec);
1555
1556 tag = decode_sv (dec);
1557 if (!tag)
1558 goto fail;
1559
1560 if (!SvPOK (tag))
1561 ERR ("malformed JSON string, (tag) must be a string");
1562
1563 decode_ws (dec);
1564
1565 if (*dec->cur != ')')
1566 ERR (") expected after tag");
1567
1568 ++dec->cur;
1569
1570 decode_ws (dec);
1571
1572 val = decode_sv (dec);
1573 if (!val)
1574 goto fail;
1575
1576 if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV)
1577 ERR ("malformed JSON string, tag value must be an array");
1578
1579 {
1580 AV *av = (AV *)SvRV (val);
1581 int i, len = av_len (av) + 1;
1582 HV *stash = gv_stashsv (tag, 0);
1583 SV *sv;
1584
1585 if (!stash)
1586 ERR ("cannot decode perl-object (package does not exist)");
1587
1588 GV *method = gv_fetchmethod_autoload (stash, "THAW", 0);
1589
1590 if (!method)
1591 ERR ("cannot decode perl-object (package does not have a THAW method)");
1592
1593 dSP;
1594
1595 ENTER; SAVETMPS;
1596 PUSHMARK (SP);
1597 EXTEND (SP, len + 2);
1598 // we re-bless the reference to get overload and other niceties right
1599 PUSHs (tag);
1600 PUSHs (sv_json);
1601
1602 for (i = 0; i < len; ++i)
1603 PUSHs (*av_fetch (av, i, 1));
1604
1605 PUTBACK;
1606 call_sv ((SV *)GvCV (method), G_SCALAR);
1607 SPAGAIN;
1608
1609 SvREFCNT_dec (tag);
1610 SvREFCNT_dec (val);
1611 sv = SvREFCNT_inc (POPs);
1612
1613 PUTBACK;
1614
1615 FREETMPS; LEAVE;
1616
1617 return sv;
1618 }
1619
1620fail:
1621 SvREFCNT_dec (tag);
1622 SvREFCNT_dec (val);
1623 return 0;
1624}
1625
1626static SV *
1451decode_sv (dec_t *dec) 1627decode_sv (dec_t *dec)
1452{ 1628{
1453 // the beauty of JSON: you need exactly one character lookahead 1629 // the beauty of JSON: you need exactly one character lookahead
1454 // to parse everything. 1630 // to parse everything.
1455 switch (*dec->cur) 1631 switch (*dec->cur)
1456 { 1632 {
1457 case '"': ++dec->cur; return decode_str (dec); 1633 case '"': ++dec->cur; return decode_str (dec);
1458 case '[': ++dec->cur; return decode_av (dec); 1634 case '[': ++dec->cur; return decode_av (dec);
1459 case '{': ++dec->cur; return decode_hv (dec); 1635 case '{': ++dec->cur; return decode_hv (dec);
1636 case '(': return decode_tag (dec);
1460 1637
1461 case '-': 1638 case '-':
1462 case '0': case '1': case '2': case '3': case '4': 1639 case '0': case '1': case '2': case '3': case '4':
1463 case '5': case '6': case '7': case '8': case '9': 1640 case '5': case '6': case '7': case '8': case '9':
1464 return decode_num (dec); 1641 return decode_num (dec);
1466 case 't': 1643 case 't':
1467 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1644 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1468 { 1645 {
1469 dec->cur += 4; 1646 dec->cur += 4;
1470#if JSON_SLOW 1647#if JSON_SLOW
1471 json_true = get_bool ("JSON::XS::true"); 1648 bool_true = get_bool ("Types::Serialiser::true");
1472#endif 1649#endif
1473 return newSVsv (json_true); 1650 return newSVsv (bool_true);
1474 } 1651 }
1475 else 1652 else
1476 ERR ("'true' expected"); 1653 ERR ("'true' expected");
1477 1654
1478 break; 1655 break;
1480 case 'f': 1657 case 'f':
1481 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1658 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1482 { 1659 {
1483 dec->cur += 5; 1660 dec->cur += 5;
1484#if JSON_SLOW 1661#if JSON_SLOW
1485 json_false = get_bool ("JSON::XS::false"); 1662 bool_false = get_bool ("Types::Serialiser::false");
1486#endif 1663#endif
1487 return newSVsv (json_false); 1664 return newSVsv (bool_false);
1488 } 1665 }
1489 else 1666 else
1490 ERR ("'false' expected"); 1667 ERR ("'false' expected");
1491 1668
1492 break; 1669 break;
1501 ERR ("'null' expected"); 1678 ERR ("'null' expected");
1502 1679
1503 break; 1680 break;
1504 1681
1505 default: 1682 default:
1506 ERR ("malformed JSON string, neither array, object, number, string or atom"); 1683 ERR ("malformed JSON string, neither tag, array, object, number, string or atom");
1507 break; 1684 break;
1508 } 1685 }
1509 1686
1510fail: 1687fail:
1511 return 0; 1688 return 0;
1516{ 1693{
1517 dec_t dec; 1694 dec_t dec;
1518 SV *sv; 1695 SV *sv;
1519 1696
1520 /* work around bugs in 5.10 where manipulating magic values 1697 /* work around bugs in 5.10 where manipulating magic values
1521 * will perl ignore the magic in subsequent accesses 1698 * makes perl ignore the magic in subsequent accesses.
1699 * also make a copy of non-PV values, to get them into a clean
1700 * state (SvPV should do that, but it's buggy, see below).
1522 */ 1701 */
1523 /*SvGETMAGIC (string);*/ 1702 /*SvGETMAGIC (string);*/
1524 if (SvMAGICAL (string)) 1703 if (SvMAGICAL (string) || !SvPOK (string))
1525 string = sv_2mortal (newSVsv (string)); 1704 string = sv_2mortal (newSVsv (string));
1526 1705
1527 SvUPGRADE (string, SVt_PV); 1706 SvUPGRADE (string, SVt_PV);
1528 1707
1529 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1708 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1546 if (offset > json->max_size && json->max_size) 1725 if (offset > json->max_size && json->max_size)
1547 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1726 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1548 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1727 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1549 } 1728 }
1550 1729
1551 if (json->flags & F_UTF8) 1730 if (DECODE_WANTS_OCTETS (json))
1552 sv_utf8_downgrade (string, 0); 1731 sv_utf8_downgrade (string, 0);
1553 else 1732 else
1554 sv_utf8_upgrade (string); 1733 sv_utf8_upgrade (string);
1555 1734
1556 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1735 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1598 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1777 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1599 LEAVE; 1778 LEAVE;
1600 1779
1601 croak ("%s, at character offset %d (before \"%s\")", 1780 croak ("%s, at character offset %d (before \"%s\")",
1602 dec.err, 1781 dec.err,
1603 ptr_to_index (string, dec.cur), 1782 (int)ptr_to_index (string, dec.cur),
1604 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1783 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1605 } 1784 }
1606 1785
1607 sv = sv_2mortal (sv); 1786 sv = sv_2mortal (sv);
1608 1787
1609 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1788 if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv))
1610 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); 1789 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
1611 1790
1612 return sv; 1791 return sv;
1613} 1792}
1614 1793
1736 self->incr_mode = INCR_M_STR; 1915 self->incr_mode = INCR_M_STR;
1737 goto incr_m_str; 1916 goto incr_m_str;
1738 1917
1739 case '[': 1918 case '[':
1740 case '{': 1919 case '{':
1920 case '(':
1741 if (++self->incr_nest > self->max_depth) 1921 if (++self->incr_nest > self->max_depth)
1742 croak (ERR_NESTING_EXCEEDED); 1922 croak (ERR_NESTING_EXCEEDED);
1743 break; 1923 break;
1744 1924
1745 case ']': 1925 case ']':
1746 case '}': 1926 case '}':
1747 if (--self->incr_nest <= 0) 1927 if (--self->incr_nest <= 0)
1748 goto interrupt; 1928 goto interrupt;
1929 break;
1930
1931 case ')':
1932 --self->incr_nest;
1749 break; 1933 break;
1750 1934
1751 case '#': 1935 case '#':
1752 self->incr_mode = INCR_M_C1; 1936 self->incr_mode = INCR_M_C1;
1753 goto incr_m_c; 1937 goto incr_m_c;
1779 i >= '0' && i <= '9' ? i - '0' 1963 i >= '0' && i <= '9' ? i - '0'
1780 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1964 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1781 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1965 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1782 : -1; 1966 : -1;
1783 1967
1784 json_stash = gv_stashpv ("JSON::XS" , 1); 1968 json_stash = gv_stashpv ("JSON::XS" , 1);
1785 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1969 bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
1786
1787 json_true = get_bool ("JSON::XS::true"); 1970 bool_true = get_bool ("Types::Serialiser::true");
1788 json_false = get_bool ("JSON::XS::false"); 1971 bool_false = get_bool ("Types::Serialiser::false");
1972
1973 sv_json = newSVpv ("JSON", 0);
1974 SvREADONLY_on (sv_json);
1789 1975
1790 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ 1976 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1791} 1977}
1792 1978
1793PROTOTYPES: DISABLE 1979PROTOTYPES: DISABLE
1794 1980
1795void CLONE (...) 1981void CLONE (...)
1796 CODE: 1982 CODE:
1797 json_stash = 0; 1983 json_stash = 0;
1798 json_boolean_stash = 0; 1984 bool_stash = 0;
1799 1985
1800void new (char *klass) 1986void new (char *klass)
1801 PPCODE: 1987 PPCODE:
1802{ 1988{
1803 SV *pv = NEWSV (0, sizeof (JSON)); 1989 SV *pv = NEWSV (0, sizeof (JSON));
1804 SvPOK_only (pv); 1990 SvPOK_only (pv);
1805 json_init ((JSON *)SvPVX (pv)); 1991 json_init ((JSON *)SvPVX (pv));
1806 XPUSHs (sv_2mortal (sv_bless ( 1992 XPUSHs (sv_2mortal (sv_bless (
1807 newRV_noinc (pv), 1993 newRV_noinc (pv),
1808 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) 1994 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1823 shrink = F_SHRINK 2009 shrink = F_SHRINK
1824 allow_blessed = F_ALLOW_BLESSED 2010 allow_blessed = F_ALLOW_BLESSED
1825 convert_blessed = F_CONV_BLESSED 2011 convert_blessed = F_CONV_BLESSED
1826 relaxed = F_RELAXED 2012 relaxed = F_RELAXED
1827 allow_unknown = F_ALLOW_UNKNOWN 2013 allow_unknown = F_ALLOW_UNKNOWN
2014 allow_tags = F_ALLOW_TAGS
1828 PPCODE: 2015 PPCODE:
1829{ 2016{
1830 if (enable) 2017 if (enable)
1831 self->flags |= ix; 2018 self->flags |= ix;
1832 else 2019 else
1848 get_shrink = F_SHRINK 2035 get_shrink = F_SHRINK
1849 get_allow_blessed = F_ALLOW_BLESSED 2036 get_allow_blessed = F_ALLOW_BLESSED
1850 get_convert_blessed = F_CONV_BLESSED 2037 get_convert_blessed = F_CONV_BLESSED
1851 get_relaxed = F_RELAXED 2038 get_relaxed = F_RELAXED
1852 get_allow_unknown = F_ALLOW_UNKNOWN 2039 get_allow_unknown = F_ALLOW_UNKNOWN
2040 get_allow_tags = F_ALLOW_TAGS
1853 PPCODE: 2041 PPCODE:
1854 XPUSHs (boolSV (self->flags & ix)); 2042 XPUSHs (boolSV (self->flags & ix));
1855 2043
1856void max_depth (JSON *self, U32 max_depth = 0x80000000UL) 2044void max_depth (JSON *self, U32 max_depth = 0x80000000UL)
1857 PPCODE: 2045 PPCODE:
1885} 2073}
1886 2074
1887void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) 2075void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef)
1888 PPCODE: 2076 PPCODE:
1889{ 2077{
1890 if (!self->cb_sk_object) 2078 if (!self->cb_sk_object)
1891 self->cb_sk_object = newHV (); 2079 self->cb_sk_object = newHV ();
1892 2080
1893 if (SvOK (cb)) 2081 if (SvOK (cb))
1894 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); 2082 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1895 else 2083 else
1906 XPUSHs (ST (0)); 2094 XPUSHs (ST (0));
1907} 2095}
1908 2096
1909void encode (JSON *self, SV *scalar) 2097void encode (JSON *self, SV *scalar)
1910 PPCODE: 2098 PPCODE:
1911 XPUSHs (encode_json (scalar, self)); 2099 PUTBACK; scalar = encode_json (scalar, self); SPAGAIN;
2100 XPUSHs (scalar);
1912 2101
1913void decode (JSON *self, SV *jsonstr) 2102void decode (JSON *self, SV *jsonstr)
1914 PPCODE: 2103 PPCODE:
1915 XPUSHs (decode_json (jsonstr, self, 0)); 2104 PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN;
2105 XPUSHs (jsonstr);
1916 2106
1917void decode_prefix (JSON *self, SV *jsonstr) 2107void decode_prefix (JSON *self, SV *jsonstr)
1918 PPCODE: 2108 PPCODE:
1919{ 2109{
2110 SV *sv;
1920 char *offset; 2111 char *offset;
2112 PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN;
1921 EXTEND (SP, 2); 2113 EXTEND (SP, 2);
1922 PUSHs (decode_json (jsonstr, self, &offset)); 2114 PUSHs (sv);
1923 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); 2115 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1924} 2116}
1925 2117
1926void incr_parse (JSON *self, SV *jsonstr = 0) 2118void incr_parse (JSON *self, SV *jsonstr = 0)
1927 PPCODE: 2119 PPCODE:
1928{ 2120{
1929 if (!self->incr_text) 2121 if (!self->incr_text)
1930 self->incr_text = newSVpvn ("", 0); 2122 self->incr_text = newSVpvn ("", 0);
2123
2124 /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */
2125 if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text))
2126 if (DECODE_WANTS_OCTETS (self))
2127 {
2128 if (self->incr_pos)
2129 self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text),
2130 (U8 *)SvPVX (self->incr_text) + self->incr_pos);
2131
2132 sv_utf8_downgrade (self->incr_text, 0);
2133 }
2134 else
2135 {
2136 sv_utf8_upgrade (self->incr_text);
2137
2138 if (self->incr_pos)
2139 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
2140 - (U8 *)SvPVX (self->incr_text);
2141 }
1931 2142
1932 // append data, if any 2143 // append data, if any
1933 if (jsonstr) 2144 if (jsonstr)
1934 { 2145 {
2146 /* make sure both strings have same encoding */
2147 if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text))
1935 if (SvUTF8 (jsonstr)) 2148 if (SvUTF8 (jsonstr))
2149 sv_utf8_downgrade (jsonstr, 0);
1936 { 2150 else
1937 if (!SvUTF8 (self->incr_text))
1938 {
1939 /* utf-8-ness differs, need to upgrade */
1940 sv_utf8_upgrade (self->incr_text);
1941
1942 if (self->incr_pos)
1943 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1944 - (U8 *)SvPVX (self->incr_text);
1945 }
1946 }
1947 else if (SvUTF8 (self->incr_text))
1948 sv_utf8_upgrade (jsonstr); 2151 sv_utf8_upgrade (jsonstr);
1949 2152
2153 /* and then just blindly append */
1950 { 2154 {
1951 STRLEN len; 2155 STRLEN len;
1952 const char *str = SvPV (jsonstr, len); 2156 const char *str = SvPV (jsonstr, len);
1953 STRLEN cur = SvCUR (self->incr_text); 2157 STRLEN cur = SvCUR (self->incr_text);
1954 2158
1962 } 2166 }
1963 2167
1964 if (GIMME_V != G_VOID) 2168 if (GIMME_V != G_VOID)
1965 do 2169 do
1966 { 2170 {
2171 SV *sv;
1967 char *offset; 2172 char *offset;
1968 2173
1969 if (!INCR_DONE (self)) 2174 if (!INCR_DONE (self))
1970 { 2175 {
1971 incr_parse (self); 2176 incr_parse (self);
1973 if (self->incr_pos > self->max_size && self->max_size) 2178 if (self->incr_pos > self->max_size && self->max_size)
1974 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 2179 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1975 (unsigned long)self->incr_pos, (unsigned long)self->max_size); 2180 (unsigned long)self->incr_pos, (unsigned long)self->max_size);
1976 2181
1977 if (!INCR_DONE (self)) 2182 if (!INCR_DONE (self))
2183 {
2184 // as an optimisation, do not accumulate white space in the incr buffer
2185 if (self->incr_mode == INCR_M_WS && self->incr_pos)
2186 {
2187 self->incr_pos = 0;
2188 SvCUR_set (self->incr_text, 0);
2189 }
2190
1978 break; 2191 break;
2192 }
1979 } 2193 }
1980 2194
1981 XPUSHs (decode_json (self->incr_text, self, &offset)); 2195 PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN;
2196 XPUSHs (sv);
1982 2197
1983 self->incr_pos -= offset - SvPVX (self->incr_text); 2198 self->incr_pos -= offset - SvPVX (self->incr_text);
1984 self->incr_nest = 0; 2199 self->incr_nest = 0;
1985 self->incr_mode = 0; 2200 self->incr_mode = 0;
1986 2201
2030 SvREFCNT_dec (self->incr_text); 2245 SvREFCNT_dec (self->incr_text);
2031 2246
2032PROTOTYPES: ENABLE 2247PROTOTYPES: ENABLE
2033 2248
2034void encode_json (SV *scalar) 2249void encode_json (SV *scalar)
2035 ALIAS:
2036 to_json_ = 0
2037 encode_json = F_UTF8
2038 PPCODE: 2250 PPCODE:
2039{ 2251{
2040 JSON json; 2252 JSON json;
2041 json_init (&json); 2253 json_init (&json);
2042 json.flags |= ix; 2254 json.flags |= F_UTF8;
2043 XPUSHs (encode_json (scalar, &json)); 2255 PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN;
2256 XPUSHs (scalar);
2044} 2257}
2045 2258
2046void decode_json (SV *jsonstr) 2259void decode_json (SV *jsonstr)
2047 ALIAS:
2048 from_json_ = 0
2049 decode_json = F_UTF8
2050 PPCODE: 2260 PPCODE:
2051{ 2261{
2052 JSON json; 2262 JSON json;
2053 json_init (&json); 2263 json_init (&json);
2054 json.flags |= ix; 2264 json.flags |= F_UTF8;
2055 XPUSHs (decode_json (jsonstr, &json, 0)); 2265 PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN;
2266 XPUSHs (jsonstr);
2056} 2267}
2057 2268

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines