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.116 by root, Thu May 23 09:31:32 2013 UTC vs.
Revision 1.130 by root, Sat Nov 26 06:09:29 2016 UTC

6#include <string.h> 6#include <string.h>
7#include <stdlib.h> 7#include <stdlib.h>
8#include <stdio.h> 8#include <stdio.h>
9#include <limits.h> 9#include <limits.h>
10#include <float.h> 10#include <float.h>
11#include <inttypes.h>
11 12
12#if defined(__BORLANDC__) || defined(_MSC_VER) 13#if defined(__BORLANDC__) || defined(_MSC_VER)
13# define snprintf _snprintf // C compilers have this in stdio.h 14# define snprintf _snprintf // C compilers have this in stdio.h
14#endif 15#endif
15 16
16// some old perls do not have this, try to make it work, no 17// 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. 18// guarantees, though. if it breaks, you get to keep the pieces.
18#ifndef UTF8_MAXBYTES 19#ifndef UTF8_MAXBYTES
19# define UTF8_MAXBYTES 13 20# define UTF8_MAXBYTES 13
21#endif
22
23// compatibility with perl <5.18
24#ifndef HvNAMELEN_get
25# define HvNAMELEN_get(hv) strlen (HvNAME (hv))
26#endif
27#ifndef HvNAMELEN
28# define HvNAMELEN(hv) HvNAMELEN_get (hv)
29#endif
30#ifndef HvNAMEUTF8
31# define HvNAMEUTF8(hv) 0
20#endif 32#endif
21 33
22// three extra for rounding, sign, and end of string 34// three extra for rounding, sign, and end of string
23#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) 35#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3)
24 36
33#define F_SHRINK 0x00000200UL 45#define F_SHRINK 0x00000200UL
34#define F_ALLOW_BLESSED 0x00000400UL 46#define F_ALLOW_BLESSED 0x00000400UL
35#define F_CONV_BLESSED 0x00000800UL 47#define F_CONV_BLESSED 0x00000800UL
36#define F_RELAXED 0x00001000UL 48#define F_RELAXED 0x00001000UL
37#define F_ALLOW_UNKNOWN 0x00002000UL 49#define F_ALLOW_UNKNOWN 0x00002000UL
50#define F_ALLOW_TAGS 0x00004000UL
38#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing 51#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
39 52
40#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 53#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
41 54
42#define INIT_SIZE 32 // initial scalar size to be allocated 55#define INIT_SIZE 32 // initial scalar size to be allocated
67#define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" 80#define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)"
68 81
69#ifdef USE_ITHREADS 82#ifdef USE_ITHREADS
70# define JSON_SLOW 1 83# define JSON_SLOW 1
71# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) 84# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
85# define BOOL_STASH (bool_stash ? bool_stash : gv_stashpv ("Types::Serialiser::Boolean", 1))
72#else 86#else
73# define JSON_SLOW 0 87# define JSON_SLOW 0
74# define JSON_STASH json_stash 88# define JSON_STASH json_stash
89# define BOOL_STASH bool_stash
75#endif 90#endif
76 91
77// the amount of HEs to allocate on the stack, when sorting keys 92// the amount of HEs to allocate on the stack, when sorting keys
78#define STACK_HES 64 93#define STACK_HES 64
79 94
80static HV *json_stash, *json_boolean_stash; // JSON::XS:: 95static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean::
81static SV *json_true, *json_false; 96static SV *bool_true, *bool_false, *sv_json;
82 97
83enum { 98enum {
84 INCR_M_WS = 0, // initial whitespace skipping, must be 0 99 INCR_M_WS = 0, // initial whitespace skipping, must be 0
85 INCR_M_STR, // inside string 100 INCR_M_STR, // inside string
86 INCR_M_BS, // inside backslash 101 INCR_M_BS, // inside backslash
287 // a recursion depth of ten gives us >>500 bits 302 // a recursion depth of ten gives us >>500 bits
288 json_atof_scan1 (s, &accum, &expo, 0, 10); 303 json_atof_scan1 (s, &accum, &expo, 0, 10);
289 304
290 return neg ? -accum : accum; 305 return neg ? -accum : accum;
291} 306}
307
308// target of scalar reference is bool? -1 == nope, 0 == false, 1 == true
309static int
310ref_bool_type (SV *sv)
311{
312 svtype svt = SvTYPE (sv);
313
314 if (svt < SVt_PVAV)
315 {
316 STRLEN len = 0;
317 char *pv = svt ? SvPV (sv, len) : 0;
318
319 if (len == 1)
320 if (*pv == '1')
321 return 1;
322 else if (*pv == '0')
323 return 0;
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 (SvTYPE (scalar) >= SVt_PVMG)
339 {
340 if (SvSTASH (scalar) == bool_stash)
341 return 1;
342
343 if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0)
344 return 1;
345 }
346
347 return 0;
348}
349
292///////////////////////////////////////////////////////////////////////////// 350/////////////////////////////////////////////////////////////////////////////
293// encoder 351// encoder
294 352
295// structure used for encoding JSON 353// structure used for encoding JSON
296typedef struct 354typedef struct
304} enc_t; 362} enc_t;
305 363
306INLINE void 364INLINE void
307need (enc_t *enc, STRLEN len) 365need (enc_t *enc, STRLEN len)
308{ 366{
309 if (expect_false (enc->cur + len >= enc->end)) 367 if (expect_false ((uintptr_t)(enc->end - enc->cur) < len))
310 { 368 {
311 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); 369 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
312 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 370 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
313 enc->cur = SvPVX (enc->sv) + cur; 371 enc->cur = SvPVX (enc->sv) + cur;
314 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 372 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
483 541
484 if (enc->indent >= enc->json.max_depth) 542 if (enc->indent >= enc->json.max_depth)
485 croak (ERR_NESTING_EXCEEDED); 543 croak (ERR_NESTING_EXCEEDED);
486 544
487 encode_ch (enc, '['); 545 encode_ch (enc, '[');
488 546
489 if (len >= 0) 547 if (len >= 0)
490 { 548 {
491 encode_nl (enc); ++enc->indent; 549 encode_nl (enc); ++enc->indent;
492 550
493 for (i = 0; i <= len; ++i) 551 for (i = 0; i <= len; ++i)
505 encode_comma (enc); 563 encode_comma (enc);
506 } 564 }
507 565
508 encode_nl (enc); --enc->indent; encode_indent (enc); 566 encode_nl (enc); --enc->indent; encode_indent (enc);
509 } 567 }
510 568
511 encode_ch (enc, ']'); 569 encode_ch (enc, ']');
512} 570}
513 571
514static void 572static void
515encode_hk (enc_t *enc, HE *he) 573encode_hk (enc_t *enc, HE *he)
519 if (HeKLEN (he) == HEf_SVKEY) 577 if (HeKLEN (he) == HEf_SVKEY)
520 { 578 {
521 SV *sv = HeSVKEY (he); 579 SV *sv = HeSVKEY (he);
522 STRLEN len; 580 STRLEN len;
523 char *str; 581 char *str;
524 582
525 SvGETMAGIC (sv); 583 SvGETMAGIC (sv);
526 str = SvPV (sv, len); 584 str = SvPV (sv, len);
527 585
528 encode_str (enc, str, len, SvUTF8 (sv)); 586 encode_str (enc, str, len, SvUTF8 (sv));
529 } 587 }
597 if (count) 655 if (count)
598 { 656 {
599 int i, fast = 1; 657 int i, fast = 1;
600 HE *hes_stack [STACK_HES]; 658 HE *hes_stack [STACK_HES];
601 HE **hes = hes_stack; 659 HE **hes = hes_stack;
602 660
603 // allocate larger arrays on the heap 661 // allocate larger arrays on the heap
604 if (count > STACK_HES) 662 if (count > STACK_HES)
605 { 663 {
606 SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes))); 664 SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes)));
607 hes = (HE **)SvPVX (sv); 665 hes = (HE **)SvPVX (sv);
682// encode objects, arrays and special \0=false and \1=true values. 740// encode objects, arrays and special \0=false and \1=true values.
683static void 741static void
684encode_rv (enc_t *enc, SV *sv) 742encode_rv (enc_t *enc, SV *sv)
685{ 743{
686 svtype svt; 744 svtype svt;
745 GV *method;
687 746
688 SvGETMAGIC (sv); 747 SvGETMAGIC (sv);
689 svt = SvTYPE (sv); 748 svt = SvTYPE (sv);
690 749
691 if (expect_false (SvOBJECT (sv))) 750 if (expect_false (SvOBJECT (sv)))
692 { 751 {
693 HV *stash = !JSON_SLOW || json_boolean_stash 752 HV *stash = SvSTASH (sv);
694 ? json_boolean_stash
695 : gv_stashpv ("JSON::XS::Boolean", 1);
696 753
697 if (SvSTASH (sv) == stash) 754 if (stash == bool_stash)
698 { 755 {
699 if (SvIV (sv)) 756 if (SvIV (sv))
700 encode_str (enc, "true", 4, 0); 757 encode_str (enc, "true", 4, 0);
701 else 758 else
702 encode_str (enc, "false", 5, 0); 759 encode_str (enc, "false", 5, 0);
703 } 760 }
761 else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0)))
762 {
763 int count;
764 dSP;
765
766 ENTER; SAVETMPS;
767 SAVESTACK_POS ();
768 PUSHMARK (SP);
769 EXTEND (SP, 2);
770 // we re-bless the reference to get overload and other niceties right
771 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
772 PUSHs (sv_json);
773
774 PUTBACK;
775 count = call_sv ((SV *)GvCV (method), G_ARRAY);
776 SPAGAIN;
777
778 // catch this surprisingly common error
779 if (SvROK (TOPs) && SvRV (TOPs) == sv)
780 croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
781
782 encode_ch (enc, '(');
783 encode_ch (enc, '"');
784 encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash));
785 encode_ch (enc, '"');
786 encode_ch (enc, ')');
787 encode_ch (enc, '[');
788
789 while (count)
790 {
791 encode_sv (enc, SP[1 - count--]);
792
793 if (count)
794 encode_ch (enc, ',');
795 }
796
797 encode_ch (enc, ']');
798
799 FREETMPS; LEAVE;
800 }
801 else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0)))
802 {
803 dSP;
804
805 ENTER; SAVETMPS;
806 PUSHMARK (SP);
807 // we re-bless the reference to get overload and other niceties right
808 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
809
810 // calling with G_SCALAR ensures that we always get a 1 return value
811 PUTBACK;
812 call_sv ((SV *)GvCV (method), G_SCALAR);
813 SPAGAIN;
814
815 // catch this surprisingly common error
816 if (SvROK (TOPs) && SvRV (TOPs) == sv)
817 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
818
819 sv = POPs;
820 PUTBACK;
821
822 encode_sv (enc, sv);
823
824 FREETMPS; LEAVE;
825 }
826 else if (enc->json.flags & F_ALLOW_BLESSED)
827 encode_str (enc, "null", 4, 0);
704 else 828 else
705 {
706#if 0
707 if (0 && sv_derived_from (rv, "JSON::Literal"))
708 {
709 // not yet
710 }
711#endif
712 if (enc->json.flags & F_CONV_BLESSED)
713 {
714 // we re-bless the reference to get overload and other niceties right
715 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
716
717 if (to_json)
718 {
719 dSP;
720
721 ENTER; SAVETMPS; PUSHMARK (SP);
722 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
723
724 // calling with G_SCALAR ensures that we always get a 1 return value
725 PUTBACK;
726 call_sv ((SV *)GvCV (to_json), G_SCALAR);
727 SPAGAIN;
728
729 // catch this surprisingly common error
730 if (SvROK (TOPs) && SvRV (TOPs) == sv)
731 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
732
733 sv = POPs;
734 PUTBACK;
735
736 encode_sv (enc, sv);
737
738 FREETMPS; LEAVE;
739 }
740 else if (enc->json.flags & F_ALLOW_BLESSED)
741 encode_str (enc, "null", 4, 0);
742 else
743 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
744 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
745 }
746 else if (enc->json.flags & F_ALLOW_BLESSED)
747 encode_str (enc, "null", 4, 0);
748 else
749 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", 829 croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)",
750 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 830 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
751 }
752 } 831 }
753 else if (svt == SVt_PVHV) 832 else if (svt == SVt_PVHV)
754 encode_hv (enc, (HV *)sv); 833 encode_hv (enc, (HV *)sv);
755 else if (svt == SVt_PVAV) 834 else if (svt == SVt_PVAV)
756 encode_av (enc, (AV *)sv); 835 encode_av (enc, (AV *)sv);
757 else if (svt < SVt_PVAV) 836 else if (svt < SVt_PVAV)
758 { 837 {
759 STRLEN len = 0; 838 int bool_type = ref_bool_type (sv);
760 char *pv = svt ? SvPV (sv, len) : 0;
761 839
762 if (len == 1 && *pv == '1') 840 if (bool_type == 1)
763 encode_str (enc, "true", 4, 0); 841 encode_str (enc, "true", 4, 0);
764 else if (len == 1 && *pv == '0') 842 else if (bool_type == 0)
765 encode_str (enc, "false", 5, 0); 843 encode_str (enc, "false", 5, 0);
766 else if (enc->json.flags & F_ALLOW_UNKNOWN) 844 else if (enc->json.flags & F_ALLOW_UNKNOWN)
767 encode_str (enc, "null", 4, 0); 845 encode_str (enc, "null", 4, 0);
768 else 846 else
769 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", 847 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
843 else if (SvROK (sv)) 921 else if (SvROK (sv))
844 encode_rv (enc, SvRV (sv)); 922 encode_rv (enc, SvRV (sv));
845 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) 923 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN)
846 encode_str (enc, "null", 4, 0); 924 encode_str (enc, "null", 4, 0);
847 else 925 else
848 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 926 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data",
849 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); 927 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
850} 928}
851 929
852static SV * 930static SV *
853encode_json (SV *scalar, JSON *json) 931encode_json (SV *scalar, JSON *json)
854{ 932{
855 enc_t enc; 933 enc_t enc;
856 934
857 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) 935 if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar))
858 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 936 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
859 937
860 enc.json = *json; 938 enc.json = *json;
861 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 939 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
862 enc.cur = SvPVX (enc.sv); 940 enc.cur = SvPVX (enc.sv);
1069 *cur++ = *dec_cur++; 1147 *cur++ = *dec_cur++;
1070 while (--clen); 1148 while (--clen);
1071 1149
1072 utf8 = 1; 1150 utf8 = 1;
1073 } 1151 }
1152 else if (ch == '\t' && dec->json.flags & F_RELAXED)
1153 *cur++ = ch;
1074 else 1154 else
1075 { 1155 {
1076 --dec_cur; 1156 --dec_cur;
1077 1157
1078 if (!ch) 1158 if (!ch)
1266 if (*dec->cur == ']') 1346 if (*dec->cur == ']')
1267 { 1347 {
1268 ++dec->cur; 1348 ++dec->cur;
1269 break; 1349 break;
1270 } 1350 }
1271 1351
1272 if (*dec->cur != ',') 1352 if (*dec->cur != ',')
1273 ERR (", or ] expected while parsing array"); 1353 ERR (", or ] expected while parsing array");
1274 1354
1275 ++dec->cur; 1355 ++dec->cur;
1276 1356
1400 1480
1401 hv_iterinit (hv); 1481 hv_iterinit (hv);
1402 he = hv_iternext (hv); 1482 he = hv_iternext (hv);
1403 hv_iterinit (hv); 1483 hv_iterinit (hv);
1404 1484
1405 // the next line creates a mortal sv each time its called. 1485 // the next line creates a mortal sv each time it's called.
1406 // might want to optimise this for common cases. 1486 // might want to optimise this for common cases.
1407 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); 1487 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1408 1488
1409 if (cb) 1489 if (cb)
1410 { 1490 {
1411 dSP; 1491 dSP;
1412 int count; 1492 int count;
1413 1493
1414 ENTER; SAVETMPS; PUSHMARK (SP); 1494 ENTER; SAVETMPS;
1495 SAVESTACK_POS ();
1496 PUSHMARK (SP);
1415 XPUSHs (HeVAL (he)); 1497 XPUSHs (HeVAL (he));
1416 sv_2mortal (sv); 1498 sv_2mortal (sv);
1417 1499
1418 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1500 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1419 1501
1432 if (dec->json.cb_object) 1514 if (dec->json.cb_object)
1433 { 1515 {
1434 dSP; 1516 dSP;
1435 int count; 1517 int count;
1436 1518
1437 ENTER; SAVETMPS; PUSHMARK (SP); 1519 ENTER; SAVETMPS;
1520 SAVESTACK_POS ();
1521 PUSHMARK (SP);
1438 XPUSHs (sv_2mortal (sv)); 1522 XPUSHs (sv_2mortal (sv));
1439 1523
1440 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1524 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1441 1525
1442 if (count == 1) 1526 if (count == 1)
1458 DEC_DEC_DEPTH; 1542 DEC_DEC_DEPTH;
1459 return 0; 1543 return 0;
1460} 1544}
1461 1545
1462static SV * 1546static SV *
1547decode_tag (dec_t *dec)
1548{
1549 SV *tag = 0;
1550 SV *val = 0;
1551
1552 if (!(dec->json.flags & F_ALLOW_TAGS))
1553 ERR ("malformed JSON string, neither array, object, number, string or atom");
1554
1555 ++dec->cur;
1556
1557 decode_ws (dec);
1558
1559 tag = decode_sv (dec);
1560 if (!tag)
1561 goto fail;
1562
1563 if (!SvPOK (tag))
1564 ERR ("malformed JSON string, (tag) must be a string");
1565
1566 decode_ws (dec);
1567
1568 if (*dec->cur != ')')
1569 ERR (") expected after tag");
1570
1571 ++dec->cur;
1572
1573 decode_ws (dec);
1574
1575 val = decode_sv (dec);
1576 if (!val)
1577 goto fail;
1578
1579 if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV)
1580 ERR ("malformed JSON string, tag value must be an array");
1581
1582 {
1583 AV *av = (AV *)SvRV (val);
1584 int i, len = av_len (av) + 1;
1585 HV *stash = gv_stashsv (tag, 0);
1586 SV *sv;
1587
1588 if (!stash)
1589 ERR ("cannot decode perl-object (package does not exist)");
1590
1591 GV *method = gv_fetchmethod_autoload (stash, "THAW", 0);
1592
1593 if (!method)
1594 ERR ("cannot decode perl-object (package does not have a THAW method)");
1595
1596 dSP;
1597
1598 ENTER; SAVETMPS;
1599 PUSHMARK (SP);
1600 EXTEND (SP, len + 2);
1601 // we re-bless the reference to get overload and other niceties right
1602 PUSHs (tag);
1603 PUSHs (sv_json);
1604
1605 for (i = 0; i < len; ++i)
1606 PUSHs (*av_fetch (av, i, 1));
1607
1608 PUTBACK;
1609 call_sv ((SV *)GvCV (method), G_SCALAR);
1610 SPAGAIN;
1611
1612 SvREFCNT_dec (tag);
1613 SvREFCNT_dec (val);
1614 sv = SvREFCNT_inc (POPs);
1615
1616 PUTBACK;
1617
1618 FREETMPS; LEAVE;
1619
1620 return sv;
1621 }
1622
1623fail:
1624 SvREFCNT_dec (tag);
1625 SvREFCNT_dec (val);
1626 return 0;
1627}
1628
1629static SV *
1463decode_sv (dec_t *dec) 1630decode_sv (dec_t *dec)
1464{ 1631{
1465 // the beauty of JSON: you need exactly one character lookahead 1632 // the beauty of JSON: you need exactly one character lookahead
1466 // to parse everything. 1633 // to parse everything.
1467 switch (*dec->cur) 1634 switch (*dec->cur)
1468 { 1635 {
1469 case '"': ++dec->cur; return decode_str (dec); 1636 case '"': ++dec->cur; return decode_str (dec);
1470 case '[': ++dec->cur; return decode_av (dec); 1637 case '[': ++dec->cur; return decode_av (dec);
1471 case '{': ++dec->cur; return decode_hv (dec); 1638 case '{': ++dec->cur; return decode_hv (dec);
1639 case '(': return decode_tag (dec);
1472 1640
1473 case '-': 1641 case '-':
1474 case '0': case '1': case '2': case '3': case '4': 1642 case '0': case '1': case '2': case '3': case '4':
1475 case '5': case '6': case '7': case '8': case '9': 1643 case '5': case '6': case '7': case '8': case '9':
1476 return decode_num (dec); 1644 return decode_num (dec);
1478 case 't': 1646 case 't':
1479 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1647 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1480 { 1648 {
1481 dec->cur += 4; 1649 dec->cur += 4;
1482#if JSON_SLOW 1650#if JSON_SLOW
1483 json_true = get_bool ("JSON::XS::true"); 1651 bool_true = get_bool ("Types::Serialiser::true");
1484#endif 1652#endif
1485 return newSVsv (json_true); 1653 return newSVsv (bool_true);
1486 } 1654 }
1487 else 1655 else
1488 ERR ("'true' expected"); 1656 ERR ("'true' expected");
1489 1657
1490 break; 1658 break;
1492 case 'f': 1660 case 'f':
1493 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1661 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1494 { 1662 {
1495 dec->cur += 5; 1663 dec->cur += 5;
1496#if JSON_SLOW 1664#if JSON_SLOW
1497 json_false = get_bool ("JSON::XS::false"); 1665 bool_false = get_bool ("Types::Serialiser::false");
1498#endif 1666#endif
1499 return newSVsv (json_false); 1667 return newSVsv (bool_false);
1500 } 1668 }
1501 else 1669 else
1502 ERR ("'false' expected"); 1670 ERR ("'false' expected");
1503 1671
1504 break; 1672 break;
1513 ERR ("'null' expected"); 1681 ERR ("'null' expected");
1514 1682
1515 break; 1683 break;
1516 1684
1517 default: 1685 default:
1518 ERR ("malformed JSON string, neither array, object, number, string or atom"); 1686 ERR ("malformed JSON string, neither tag, array, object, number, string or atom");
1519 break; 1687 break;
1520 } 1688 }
1521 1689
1522fail: 1690fail:
1523 return 0; 1691 return 0;
1524} 1692}
1525 1693
1526static SV * 1694static SV *
1527decode_json (SV *string, JSON *json, char **offset_return) 1695decode_json (SV *string, JSON *json, STRLEN *offset_return)
1528{ 1696{
1529 dec_t dec; 1697 dec_t dec;
1530 SV *sv; 1698 SV *sv;
1531 1699
1532 /* work around bugs in 5.10 where manipulating magic values 1700 /* work around bugs in 5.10 where manipulating magic values
1533 * makes perl ignore the magic in subsequent accesses. 1701 * makes perl ignore the magic in subsequent accesses.
1534 * also make a copy of non-PV values, to get them into a clean 1702 * 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). 1703 * state (SvPV should do that, but it's buggy, see below).
1704 *
1705 * SvIsCOW_shared_hash works around a bug in perl (possibly 5.16),
1706 * as reported by Reini Urban.
1536 */ 1707 */
1537 /*SvGETMAGIC (string);*/ 1708 /*SvGETMAGIC (string);*/
1538 if (SvMAGICAL (string) || !SvPOK (string)) 1709 if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string))
1539 string = sv_2mortal (newSVsv (string)); 1710 string = sv_2mortal (newSVsv (string));
1540 1711
1541 SvUPGRADE (string, SVt_PV); 1712 SvUPGRADE (string, SVt_PV);
1542 1713
1543 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1714 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1582 1753
1583 decode_ws (&dec); 1754 decode_ws (&dec);
1584 sv = decode_sv (&dec); 1755 sv = decode_sv (&dec);
1585 1756
1586 if (offset_return) 1757 if (offset_return)
1587 *offset_return = dec.cur; 1758 *offset_return = dec.cur - SvPVX (string);
1588 1759 else if (sv)
1589 if (!(offset_return || !sv))
1590 { 1760 {
1591 // check for trailing garbage 1761 // check for trailing garbage
1592 decode_ws (&dec); 1762 decode_ws (&dec);
1593 1763
1594 if (*dec.cur) 1764 if (*dec.cur)
1618 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1788 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1619 } 1789 }
1620 1790
1621 sv = sv_2mortal (sv); 1791 sv = sv_2mortal (sv);
1622 1792
1623 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1793 if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv))
1624 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); 1794 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
1625 1795
1626 return sv; 1796 return sv;
1627} 1797}
1628 1798
1750 self->incr_mode = INCR_M_STR; 1920 self->incr_mode = INCR_M_STR;
1751 goto incr_m_str; 1921 goto incr_m_str;
1752 1922
1753 case '[': 1923 case '[':
1754 case '{': 1924 case '{':
1925 case '(':
1755 if (++self->incr_nest > self->max_depth) 1926 if (++self->incr_nest > self->max_depth)
1756 croak (ERR_NESTING_EXCEEDED); 1927 croak (ERR_NESTING_EXCEEDED);
1757 break; 1928 break;
1758 1929
1759 case ']': 1930 case ']':
1760 case '}': 1931 case '}':
1761 if (--self->incr_nest <= 0) 1932 if (--self->incr_nest <= 0)
1762 goto interrupt; 1933 goto interrupt;
1934 break;
1935
1936 case ')':
1937 --self->incr_nest;
1763 break; 1938 break;
1764 1939
1765 case '#': 1940 case '#':
1766 self->incr_mode = INCR_M_C1; 1941 self->incr_mode = INCR_M_C1;
1767 goto incr_m_c; 1942 goto incr_m_c;
1793 i >= '0' && i <= '9' ? i - '0' 1968 i >= '0' && i <= '9' ? i - '0'
1794 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1969 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1795 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1970 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1796 : -1; 1971 : -1;
1797 1972
1798 json_stash = gv_stashpv ("JSON::XS" , 1); 1973 json_stash = gv_stashpv ("JSON::XS" , 1);
1799 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1974 bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
1800
1801 json_true = get_bool ("JSON::XS::true"); 1975 bool_true = get_bool ("Types::Serialiser::true");
1802 json_false = get_bool ("JSON::XS::false"); 1976 bool_false = get_bool ("Types::Serialiser::false");
1977
1978 sv_json = newSVpv ("JSON", 0);
1979 SvREADONLY_on (sv_json);
1803 1980
1804 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ 1981 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1805} 1982}
1806 1983
1807PROTOTYPES: DISABLE 1984PROTOTYPES: DISABLE
1808 1985
1809void CLONE (...) 1986void CLONE (...)
1810 CODE: 1987 CODE:
1811 json_stash = 0; 1988 json_stash = 0;
1812 json_boolean_stash = 0; 1989 bool_stash = 0;
1813 1990
1814void new (char *klass) 1991void new (char *klass)
1815 PPCODE: 1992 PPCODE:
1816{ 1993{
1817 SV *pv = NEWSV (0, sizeof (JSON)); 1994 SV *pv = NEWSV (0, sizeof (JSON));
1837 shrink = F_SHRINK 2014 shrink = F_SHRINK
1838 allow_blessed = F_ALLOW_BLESSED 2015 allow_blessed = F_ALLOW_BLESSED
1839 convert_blessed = F_CONV_BLESSED 2016 convert_blessed = F_CONV_BLESSED
1840 relaxed = F_RELAXED 2017 relaxed = F_RELAXED
1841 allow_unknown = F_ALLOW_UNKNOWN 2018 allow_unknown = F_ALLOW_UNKNOWN
2019 allow_tags = F_ALLOW_TAGS
1842 PPCODE: 2020 PPCODE:
1843{ 2021{
1844 if (enable) 2022 if (enable)
1845 self->flags |= ix; 2023 self->flags |= ix;
1846 else 2024 else
1862 get_shrink = F_SHRINK 2040 get_shrink = F_SHRINK
1863 get_allow_blessed = F_ALLOW_BLESSED 2041 get_allow_blessed = F_ALLOW_BLESSED
1864 get_convert_blessed = F_CONV_BLESSED 2042 get_convert_blessed = F_CONV_BLESSED
1865 get_relaxed = F_RELAXED 2043 get_relaxed = F_RELAXED
1866 get_allow_unknown = F_ALLOW_UNKNOWN 2044 get_allow_unknown = F_ALLOW_UNKNOWN
2045 get_allow_tags = F_ALLOW_TAGS
1867 PPCODE: 2046 PPCODE:
1868 XPUSHs (boolSV (self->flags & ix)); 2047 XPUSHs (boolSV (self->flags & ix));
1869 2048
1870void max_depth (JSON *self, U32 max_depth = 0x80000000UL) 2049void max_depth (JSON *self, U32 max_depth = 0x80000000UL)
1871 PPCODE: 2050 PPCODE:
1932 2111
1933void decode_prefix (JSON *self, SV *jsonstr) 2112void decode_prefix (JSON *self, SV *jsonstr)
1934 PPCODE: 2113 PPCODE:
1935{ 2114{
1936 SV *sv; 2115 SV *sv;
1937 char *offset; 2116 STRLEN offset;
1938 PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; 2117 PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN;
1939 EXTEND (SP, 2); 2118 EXTEND (SP, 2);
1940 PUSHs (sv); 2119 PUSHs (sv);
1941 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); 2120 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, SvPV_nolen (jsonstr) + offset))));
1942} 2121}
1943 2122
1944void incr_parse (JSON *self, SV *jsonstr = 0) 2123void incr_parse (JSON *self, SV *jsonstr = 0)
1945 PPCODE: 2124 PPCODE:
1946{ 2125{
1993 2172
1994 if (GIMME_V != G_VOID) 2173 if (GIMME_V != G_VOID)
1995 do 2174 do
1996 { 2175 {
1997 SV *sv; 2176 SV *sv;
1998 char *offset; 2177 STRLEN offset;
1999 2178
2000 if (!INCR_DONE (self)) 2179 if (!INCR_DONE (self))
2001 { 2180 {
2002 incr_parse (self); 2181 incr_parse (self);
2003 2182
2019 } 2198 }
2020 2199
2021 PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; 2200 PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN;
2022 XPUSHs (sv); 2201 XPUSHs (sv);
2023 2202
2024 self->incr_pos -= offset - SvPVX (self->incr_text); 2203 self->incr_pos -= offset;
2025 self->incr_nest = 0; 2204 self->incr_nest = 0;
2026 self->incr_mode = 0; 2205 self->incr_mode = 0;
2027 2206
2028 sv_chop (self->incr_text, offset); 2207 sv_chop (self->incr_text, SvPVX (self->incr_text) + offset);
2029 } 2208 }
2030 while (GIMME_V == G_ARRAY); 2209 while (GIMME_V == G_ARRAY);
2031} 2210}
2032 2211
2033SV *incr_text (JSON *self) 2212SV *incr_text (JSON *self)
2071 SvREFCNT_dec (self->incr_text); 2250 SvREFCNT_dec (self->incr_text);
2072 2251
2073PROTOTYPES: ENABLE 2252PROTOTYPES: ENABLE
2074 2253
2075void encode_json (SV *scalar) 2254void encode_json (SV *scalar)
2076 ALIAS:
2077 to_json_ = 0
2078 encode_json = F_UTF8
2079 PPCODE: 2255 PPCODE:
2080{ 2256{
2081 JSON json; 2257 JSON json;
2082 json_init (&json); 2258 json_init (&json);
2083 json.flags |= ix; 2259 json.flags |= F_UTF8;
2084 PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; 2260 PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN;
2085 XPUSHs (scalar); 2261 XPUSHs (scalar);
2086} 2262}
2087 2263
2088void decode_json (SV *jsonstr) 2264void decode_json (SV *jsonstr)
2089 ALIAS:
2090 from_json_ = 0
2091 decode_json = F_UTF8
2092 PPCODE: 2265 PPCODE:
2093{ 2266{
2094 JSON json; 2267 JSON json;
2095 json_init (&json); 2268 json_init (&json);
2096 json.flags |= ix; 2269 json.flags |= F_UTF8;
2097 PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; 2270 PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN;
2098 XPUSHs (jsonstr); 2271 XPUSHs (jsonstr);
2099} 2272}
2100 2273

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines