… | |
… | |
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 | |
… | |
… | |
68 | #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?)" |
69 | |
81 | |
70 | #ifdef USE_ITHREADS |
82 | #ifdef USE_ITHREADS |
71 | # define JSON_SLOW 1 |
83 | # define JSON_SLOW 1 |
72 | # 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)) |
73 | #else |
86 | #else |
74 | # define JSON_SLOW 0 |
87 | # define JSON_SLOW 0 |
75 | # define JSON_STASH json_stash |
88 | # define JSON_STASH json_stash |
|
|
89 | # define BOOL_STASH bool_stash |
76 | #endif |
90 | #endif |
77 | |
91 | |
78 | // 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 |
79 | #define STACK_HES 64 |
93 | #define STACK_HES 64 |
80 | |
94 | |
81 | static HV *json_stash, *types_boolean_stash; // JSON::XS:: |
95 | static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean:: |
82 | static SV *types_true, *types_false, *sv_json; |
96 | static SV *bool_true, *bool_false, *sv_json; |
83 | |
97 | |
84 | enum { |
98 | enum { |
85 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
99 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
86 | INCR_M_STR, // inside string |
100 | INCR_M_STR, // inside string |
87 | INCR_M_BS, // inside backslash |
101 | INCR_M_BS, // inside backslash |
… | |
… | |
288 | // a recursion depth of ten gives us >>500 bits |
302 | // a recursion depth of ten gives us >>500 bits |
289 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
303 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
290 | |
304 | |
291 | return neg ? -accum : accum; |
305 | return neg ? -accum : accum; |
292 | } |
306 | } |
|
|
307 | |
|
|
308 | // target of scalar reference is bool? -1 == nope, 0 == false, 1 == true |
|
|
309 | static int |
|
|
310 | ref_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 |
|
|
330 | static int |
|
|
331 | json_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 | |
293 | ///////////////////////////////////////////////////////////////////////////// |
350 | ///////////////////////////////////////////////////////////////////////////// |
294 | // encoder |
351 | // encoder |
295 | |
352 | |
296 | // structure used for encoding JSON |
353 | // structure used for encoding JSON |
297 | typedef struct |
354 | typedef struct |
… | |
… | |
305 | } enc_t; |
362 | } enc_t; |
306 | |
363 | |
307 | INLINE void |
364 | INLINE void |
308 | need (enc_t *enc, STRLEN len) |
365 | need (enc_t *enc, STRLEN len) |
309 | { |
366 | { |
310 | if (expect_false (enc->cur + len >= enc->end)) |
367 | if (expect_false ((uintptr_t)(enc->end - enc->cur) < len)) |
311 | { |
368 | { |
312 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
369 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
313 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
370 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
314 | enc->cur = SvPVX (enc->sv) + cur; |
371 | enc->cur = SvPVX (enc->sv) + cur; |
315 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
372 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
… | |
… | |
683 | // encode objects, arrays and special \0=false and \1=true values. |
740 | // encode objects, arrays and special \0=false and \1=true values. |
684 | static void |
741 | static void |
685 | encode_rv (enc_t *enc, SV *sv) |
742 | encode_rv (enc_t *enc, SV *sv) |
686 | { |
743 | { |
687 | svtype svt; |
744 | svtype svt; |
|
|
745 | GV *method; |
688 | |
746 | |
689 | SvGETMAGIC (sv); |
747 | SvGETMAGIC (sv); |
690 | svt = SvTYPE (sv); |
748 | svt = SvTYPE (sv); |
691 | |
749 | |
692 | if (expect_false (SvOBJECT (sv))) |
750 | if (expect_false (SvOBJECT (sv))) |
693 | { |
751 | { |
694 | HV *boolean_stash = !JSON_SLOW || types_boolean_stash |
|
|
695 | ? types_boolean_stash |
|
|
696 | : gv_stashpv ("Types::Serialiser::Boolean", 1); |
|
|
697 | HV *stash = SvSTASH (sv); |
752 | HV *stash = SvSTASH (sv); |
698 | |
753 | |
699 | if (stash == boolean_stash) |
754 | if (stash == bool_stash) |
700 | { |
755 | { |
701 | if (SvIV (sv)) |
756 | if (SvIV (sv)) |
702 | encode_str (enc, "true", 4, 0); |
757 | encode_str (enc, "true", 4, 0); |
703 | else |
758 | else |
704 | encode_str (enc, "false", 5, 0); |
759 | encode_str (enc, "false", 5, 0); |
705 | } |
760 | } |
706 | else if (enc->json.flags & F_CONV_BLESSED) |
761 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
707 | { |
762 | { |
708 | GV *to_json = gv_fetchmethod_autoload (stash, "TO_JSON", 0); |
763 | int count; |
|
|
764 | dSP; |
709 | |
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)); |
710 | if (to_json) |
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) |
711 | { |
790 | { |
712 | dSP; |
791 | encode_sv (enc, SP[1 - count--]); |
713 | |
792 | |
714 | ENTER; SAVETMPS; PUSHMARK (SP); |
793 | if (count) |
715 | // we re-bless the reference to get overload and other niceties right |
|
|
716 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
717 | |
|
|
718 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
719 | PUTBACK; |
|
|
720 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
|
|
721 | SPAGAIN; |
|
|
722 | |
|
|
723 | // catch this surprisingly common error |
|
|
724 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
725 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
726 | |
|
|
727 | sv = POPs; |
|
|
728 | PUTBACK; |
|
|
729 | |
|
|
730 | encode_sv (enc, sv); |
794 | encode_ch (enc, ','); |
731 | |
|
|
732 | FREETMPS; LEAVE; |
|
|
733 | } |
795 | } |
734 | else if (enc->json.flags & F_ALLOW_BLESSED) |
796 | |
735 | encode_str (enc, "null", 4, 0); |
797 | encode_ch (enc, ']'); |
736 | else |
798 | |
737 | croak ("encountered object '%s' when conv_object is enabled, but TO_JSON is missing and allow_blessed disabled", |
799 | FREETMPS; LEAVE; |
738 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
739 | } |
800 | } |
740 | else if (enc->json.flags & F_ALLOW_TAGS) |
801 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
741 | { |
802 | { |
742 | GV *method = gv_fetchmethod_autoload (stash, "FREEZE", 0); |
|
|
743 | |
|
|
744 | if (method) |
|
|
745 | { |
|
|
746 | int count; |
|
|
747 | dSP; |
803 | dSP; |
748 | |
804 | |
749 | ENTER; SAVETMPS; PUSHMARK (SP); |
805 | ENTER; SAVETMPS; |
750 | EXTEND (SP, 2); |
806 | PUSHMARK (SP); |
751 | // we re-bless the reference to get overload and other niceties right |
807 | // we re-bless the reference to get overload and other niceties right |
752 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
808 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
753 | PUSHs (sv_json); |
|
|
754 | |
809 | |
|
|
810 | // calling with G_SCALAR ensures that we always get a 1 return value |
755 | PUTBACK; |
811 | PUTBACK; |
756 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
812 | call_sv ((SV *)GvCV (method), G_SCALAR); |
757 | SPAGAIN; |
813 | SPAGAIN; |
758 | |
814 | |
759 | // catch this surprisingly common error |
815 | // catch this surprisingly common error |
760 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
816 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
761 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
817 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
762 | |
818 | |
763 | encode_ch (enc, '('); |
819 | sv = POPs; |
764 | encode_ch (enc, '"'); |
|
|
765 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
|
|
766 | encode_ch (enc, '"'); |
|
|
767 | encode_ch (enc, ')'); |
|
|
768 | encode_ch (enc, '['); |
|
|
769 | |
|
|
770 | while (count) |
|
|
771 | { |
|
|
772 | encode_sv (enc, SP[1 - count--]); |
|
|
773 | |
|
|
774 | if (count) |
|
|
775 | encode_ch (enc, ','); |
|
|
776 | } |
|
|
777 | |
|
|
778 | encode_ch (enc, ']'); |
|
|
779 | |
|
|
780 | PUTBACK; |
820 | PUTBACK; |
781 | |
821 | |
|
|
822 | encode_sv (enc, sv); |
|
|
823 | |
782 | FREETMPS; LEAVE; |
824 | FREETMPS; LEAVE; |
783 | } |
|
|
784 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
785 | encode_str (enc, "null", 4, 0); |
|
|
786 | else |
|
|
787 | croak ("encountered object '%s' when allow_tags is enabled, but FREEZE is missing and allow_blessed disabled", |
|
|
788 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
789 | } |
825 | } |
790 | else if (enc->json.flags & F_ALLOW_BLESSED) |
826 | else if (enc->json.flags & F_ALLOW_BLESSED) |
791 | encode_str (enc, "null", 4, 0); |
827 | encode_str (enc, "null", 4, 0); |
792 | else |
828 | else |
793 | croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags 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)", |
794 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
830 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
795 | } |
831 | } |
796 | else if (svt == SVt_PVHV) |
832 | else if (svt == SVt_PVHV) |
797 | encode_hv (enc, (HV *)sv); |
833 | encode_hv (enc, (HV *)sv); |
798 | else if (svt == SVt_PVAV) |
834 | else if (svt == SVt_PVAV) |
799 | encode_av (enc, (AV *)sv); |
835 | encode_av (enc, (AV *)sv); |
800 | else if (svt < SVt_PVAV) |
836 | else if (svt < SVt_PVAV) |
801 | { |
837 | { |
802 | STRLEN len = 0; |
838 | int bool_type = ref_bool_type (sv); |
803 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
804 | |
839 | |
805 | if (len == 1 && *pv == '1') |
840 | if (bool_type == 1) |
806 | encode_str (enc, "true", 4, 0); |
841 | encode_str (enc, "true", 4, 0); |
807 | else if (len == 1 && *pv == '0') |
842 | else if (bool_type == 0) |
808 | encode_str (enc, "false", 5, 0); |
843 | encode_str (enc, "false", 5, 0); |
809 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
844 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
810 | encode_str (enc, "null", 4, 0); |
845 | encode_str (enc, "null", 4, 0); |
811 | else |
846 | else |
812 | 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", |
… | |
… | |
895 | static SV * |
930 | static SV * |
896 | encode_json (SV *scalar, JSON *json) |
931 | encode_json (SV *scalar, JSON *json) |
897 | { |
932 | { |
898 | enc_t enc; |
933 | enc_t enc; |
899 | |
934 | |
900 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
935 | if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar)) |
901 | 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)"); |
902 | |
937 | |
903 | enc.json = *json; |
938 | enc.json = *json; |
904 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
939 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
905 | enc.cur = SvPVX (enc.sv); |
940 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
1112 | *cur++ = *dec_cur++; |
1147 | *cur++ = *dec_cur++; |
1113 | while (--clen); |
1148 | while (--clen); |
1114 | |
1149 | |
1115 | utf8 = 1; |
1150 | utf8 = 1; |
1116 | } |
1151 | } |
|
|
1152 | else if (ch == '\t' && dec->json.flags & F_RELAXED) |
|
|
1153 | *cur++ = ch; |
1117 | else |
1154 | else |
1118 | { |
1155 | { |
1119 | --dec_cur; |
1156 | --dec_cur; |
1120 | |
1157 | |
1121 | if (!ch) |
1158 | if (!ch) |
… | |
… | |
1443 | |
1480 | |
1444 | hv_iterinit (hv); |
1481 | hv_iterinit (hv); |
1445 | he = hv_iternext (hv); |
1482 | he = hv_iternext (hv); |
1446 | hv_iterinit (hv); |
1483 | hv_iterinit (hv); |
1447 | |
1484 | |
1448 | // the next line creates a mortal sv each time its called. |
1485 | // the next line creates a mortal sv each time it's called. |
1449 | // might want to optimise this for common cases. |
1486 | // might want to optimise this for common cases. |
1450 | 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); |
1451 | |
1488 | |
1452 | if (cb) |
1489 | if (cb) |
1453 | { |
1490 | { |
1454 | dSP; |
1491 | dSP; |
1455 | int count; |
1492 | int count; |
1456 | |
1493 | |
1457 | ENTER; SAVETMPS; PUSHMARK (SP); |
1494 | ENTER; SAVETMPS; |
|
|
1495 | SAVESTACK_POS (); |
|
|
1496 | PUSHMARK (SP); |
1458 | XPUSHs (HeVAL (he)); |
1497 | XPUSHs (HeVAL (he)); |
1459 | sv_2mortal (sv); |
1498 | sv_2mortal (sv); |
1460 | |
1499 | |
1461 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1500 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1462 | |
1501 | |
… | |
… | |
1475 | if (dec->json.cb_object) |
1514 | if (dec->json.cb_object) |
1476 | { |
1515 | { |
1477 | dSP; |
1516 | dSP; |
1478 | int count; |
1517 | int count; |
1479 | |
1518 | |
1480 | ENTER; SAVETMPS; PUSHMARK (SP); |
1519 | ENTER; SAVETMPS; |
|
|
1520 | SAVESTACK_POS (); |
|
|
1521 | PUSHMARK (SP); |
1481 | XPUSHs (sv_2mortal (sv)); |
1522 | XPUSHs (sv_2mortal (sv)); |
1482 | |
1523 | |
1483 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1524 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1484 | |
1525 | |
1485 | if (count == 1) |
1526 | if (count == 1) |
… | |
… | |
1511 | if (!(dec->json.flags & F_ALLOW_TAGS)) |
1552 | if (!(dec->json.flags & F_ALLOW_TAGS)) |
1512 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1553 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1513 | |
1554 | |
1514 | ++dec->cur; |
1555 | ++dec->cur; |
1515 | |
1556 | |
|
|
1557 | decode_ws (dec); |
|
|
1558 | |
1516 | tag = decode_sv (dec); |
1559 | tag = decode_sv (dec); |
1517 | if (!tag) |
1560 | if (!tag) |
1518 | goto fail; |
1561 | goto fail; |
1519 | |
1562 | |
1520 | if (!SvPOK (tag)) |
1563 | if (!SvPOK (tag)) |
1521 | ERR ("malformed JSON string, (tag) must be a string"); |
1564 | ERR ("malformed JSON string, (tag) must be a string"); |
1522 | |
1565 | |
|
|
1566 | decode_ws (dec); |
|
|
1567 | |
1523 | if (*dec->cur != ')') |
1568 | if (*dec->cur != ')') |
1524 | ERR (") expected after tag"); |
1569 | ERR (") expected after tag"); |
1525 | |
1570 | |
1526 | ++dec->cur; |
1571 | ++dec->cur; |
|
|
1572 | |
|
|
1573 | decode_ws (dec); |
1527 | |
1574 | |
1528 | val = decode_sv (dec); |
1575 | val = decode_sv (dec); |
1529 | if (!val) |
1576 | if (!val) |
1530 | goto fail; |
1577 | goto fail; |
1531 | |
1578 | |
… | |
… | |
1546 | if (!method) |
1593 | if (!method) |
1547 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1594 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1548 | |
1595 | |
1549 | dSP; |
1596 | dSP; |
1550 | |
1597 | |
1551 | ENTER; SAVETMPS; PUSHMARK (SP); |
1598 | ENTER; SAVETMPS; |
|
|
1599 | PUSHMARK (SP); |
1552 | EXTEND (SP, len + 2); |
1600 | EXTEND (SP, len + 2); |
1553 | // we re-bless the reference to get overload and other niceties right |
1601 | // we re-bless the reference to get overload and other niceties right |
1554 | PUSHs (tag); |
1602 | PUSHs (tag); |
1555 | PUSHs (sv_json); |
1603 | PUSHs (sv_json); |
1556 | |
1604 | |
… | |
… | |
1598 | case 't': |
1646 | case 't': |
1599 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1647 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1600 | { |
1648 | { |
1601 | dec->cur += 4; |
1649 | dec->cur += 4; |
1602 | #if JSON_SLOW |
1650 | #if JSON_SLOW |
1603 | types_true = get_bool ("Types::Serialiser::true"); |
1651 | bool_true = get_bool ("Types::Serialiser::true"); |
1604 | #endif |
1652 | #endif |
1605 | return newSVsv (types_true); |
1653 | return newSVsv (bool_true); |
1606 | } |
1654 | } |
1607 | else |
1655 | else |
1608 | ERR ("'true' expected"); |
1656 | ERR ("'true' expected"); |
1609 | |
1657 | |
1610 | break; |
1658 | break; |
… | |
… | |
1612 | case 'f': |
1660 | case 'f': |
1613 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1661 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1614 | { |
1662 | { |
1615 | dec->cur += 5; |
1663 | dec->cur += 5; |
1616 | #if JSON_SLOW |
1664 | #if JSON_SLOW |
1617 | types_false = get_bool ("Types::Serialiser::false"); |
1665 | bool_false = get_bool ("Types::Serialiser::false"); |
1618 | #endif |
1666 | #endif |
1619 | return newSVsv (types_false); |
1667 | return newSVsv (bool_false); |
1620 | } |
1668 | } |
1621 | else |
1669 | else |
1622 | ERR ("'false' expected"); |
1670 | ERR ("'false' expected"); |
1623 | |
1671 | |
1624 | break; |
1672 | break; |
… | |
… | |
1642 | fail: |
1690 | fail: |
1643 | return 0; |
1691 | return 0; |
1644 | } |
1692 | } |
1645 | |
1693 | |
1646 | static SV * |
1694 | static SV * |
1647 | decode_json (SV *string, JSON *json, char **offset_return) |
1695 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1648 | { |
1696 | { |
1649 | dec_t dec; |
1697 | dec_t dec; |
1650 | SV *sv; |
1698 | SV *sv; |
1651 | |
1699 | |
1652 | /* work around bugs in 5.10 where manipulating magic values |
1700 | /* work around bugs in 5.10 where manipulating magic values |
1653 | * makes perl ignore the magic in subsequent accesses. |
1701 | * makes perl ignore the magic in subsequent accesses. |
1654 | * 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 |
1655 | * 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. |
1656 | */ |
1707 | */ |
1657 | /*SvGETMAGIC (string);*/ |
1708 | /*SvGETMAGIC (string);*/ |
1658 | if (SvMAGICAL (string) || !SvPOK (string)) |
1709 | if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) |
1659 | string = sv_2mortal (newSVsv (string)); |
1710 | string = sv_2mortal (newSVsv (string)); |
1660 | |
1711 | |
1661 | SvUPGRADE (string, SVt_PV); |
1712 | SvUPGRADE (string, SVt_PV); |
1662 | |
1713 | |
1663 | /* 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 |
… | |
… | |
1702 | |
1753 | |
1703 | decode_ws (&dec); |
1754 | decode_ws (&dec); |
1704 | sv = decode_sv (&dec); |
1755 | sv = decode_sv (&dec); |
1705 | |
1756 | |
1706 | if (offset_return) |
1757 | if (offset_return) |
1707 | *offset_return = dec.cur; |
1758 | *offset_return = dec.cur - SvPVX (string); |
1708 | |
1759 | else if (sv) |
1709 | if (!(offset_return || !sv)) |
|
|
1710 | { |
1760 | { |
1711 | // check for trailing garbage |
1761 | // check for trailing garbage |
1712 | decode_ws (&dec); |
1762 | decode_ws (&dec); |
1713 | |
1763 | |
1714 | if (*dec.cur) |
1764 | if (*dec.cur) |
… | |
… | |
1738 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1788 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1739 | } |
1789 | } |
1740 | |
1790 | |
1741 | sv = sv_2mortal (sv); |
1791 | sv = sv_2mortal (sv); |
1742 | |
1792 | |
1743 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1793 | if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv)) |
1744 | 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)"); |
1745 | |
1795 | |
1746 | return sv; |
1796 | return sv; |
1747 | } |
1797 | } |
1748 | |
1798 | |
… | |
… | |
1918 | i >= '0' && i <= '9' ? i - '0' |
1968 | i >= '0' && i <= '9' ? i - '0' |
1919 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1969 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1920 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1970 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1921 | : -1; |
1971 | : -1; |
1922 | |
1972 | |
1923 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1973 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1924 | types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1974 | bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1925 | |
|
|
1926 | types_true = get_bool ("Types::Serialiser::true"); |
1975 | bool_true = get_bool ("Types::Serialiser::true"); |
1927 | types_false = get_bool ("Types::Serialiser::false"); |
1976 | bool_false = get_bool ("Types::Serialiser::false"); |
1928 | |
1977 | |
1929 | sv_json = newSVpv ("JSON", 0); |
1978 | sv_json = newSVpv ("JSON", 0); |
1930 | SvREADONLY_on (sv_json); |
1979 | SvREADONLY_on (sv_json); |
1931 | |
1980 | |
1932 | 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 */ |
… | |
… | |
1934 | |
1983 | |
1935 | PROTOTYPES: DISABLE |
1984 | PROTOTYPES: DISABLE |
1936 | |
1985 | |
1937 | void CLONE (...) |
1986 | void CLONE (...) |
1938 | CODE: |
1987 | CODE: |
1939 | json_stash = 0; |
1988 | json_stash = 0; |
1940 | types_boolean_stash = 0; |
1989 | bool_stash = 0; |
1941 | |
1990 | |
1942 | void new (char *klass) |
1991 | void new (char *klass) |
1943 | PPCODE: |
1992 | PPCODE: |
1944 | { |
1993 | { |
1945 | SV *pv = NEWSV (0, sizeof (JSON)); |
1994 | SV *pv = NEWSV (0, sizeof (JSON)); |
… | |
… | |
2062 | |
2111 | |
2063 | void decode_prefix (JSON *self, SV *jsonstr) |
2112 | void decode_prefix (JSON *self, SV *jsonstr) |
2064 | PPCODE: |
2113 | PPCODE: |
2065 | { |
2114 | { |
2066 | SV *sv; |
2115 | SV *sv; |
2067 | char *offset; |
2116 | STRLEN offset; |
2068 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
2117 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
2069 | EXTEND (SP, 2); |
2118 | EXTEND (SP, 2); |
2070 | PUSHs (sv); |
2119 | PUSHs (sv); |
2071 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
2120 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, SvPV_nolen (jsonstr) + offset)))); |
2072 | } |
2121 | } |
2073 | |
2122 | |
2074 | void incr_parse (JSON *self, SV *jsonstr = 0) |
2123 | void incr_parse (JSON *self, SV *jsonstr = 0) |
2075 | PPCODE: |
2124 | PPCODE: |
2076 | { |
2125 | { |
… | |
… | |
2123 | |
2172 | |
2124 | if (GIMME_V != G_VOID) |
2173 | if (GIMME_V != G_VOID) |
2125 | do |
2174 | do |
2126 | { |
2175 | { |
2127 | SV *sv; |
2176 | SV *sv; |
2128 | char *offset; |
2177 | STRLEN offset; |
2129 | |
2178 | |
2130 | if (!INCR_DONE (self)) |
2179 | if (!INCR_DONE (self)) |
2131 | { |
2180 | { |
2132 | incr_parse (self); |
2181 | incr_parse (self); |
2133 | |
2182 | |
… | |
… | |
2149 | } |
2198 | } |
2150 | |
2199 | |
2151 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
2200 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
2152 | XPUSHs (sv); |
2201 | XPUSHs (sv); |
2153 | |
2202 | |
2154 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2203 | self->incr_pos -= offset; |
2155 | self->incr_nest = 0; |
2204 | self->incr_nest = 0; |
2156 | self->incr_mode = 0; |
2205 | self->incr_mode = 0; |
2157 | |
2206 | |
2158 | sv_chop (self->incr_text, offset); |
2207 | sv_chop (self->incr_text, SvPVX (self->incr_text) + offset); |
2159 | } |
2208 | } |
2160 | while (GIMME_V == G_ARRAY); |
2209 | while (GIMME_V == G_ARRAY); |
2161 | } |
2210 | } |
2162 | |
2211 | |
2163 | SV *incr_text (JSON *self) |
2212 | SV *incr_text (JSON *self) |