… | |
… | |
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 |
… | |
… | |
67 | #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?)" |
68 | |
80 | |
69 | #ifdef USE_ITHREADS |
81 | #ifdef USE_ITHREADS |
70 | # define JSON_SLOW 1 |
82 | # define JSON_SLOW 1 |
71 | # 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)) |
72 | #else |
85 | #else |
73 | # define JSON_SLOW 0 |
86 | # define JSON_SLOW 0 |
74 | # define JSON_STASH json_stash |
87 | # define JSON_STASH json_stash |
|
|
88 | # define BOOL_STASH bool_stash |
75 | #endif |
89 | #endif |
76 | |
90 | |
77 | // the amount of HEs to allocate on the stack, when sorting keys |
91 | // the amount of HEs to allocate on the stack, when sorting keys |
78 | #define STACK_HES 64 |
92 | #define STACK_HES 64 |
79 | |
93 | |
80 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
94 | static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean:: |
81 | static SV *json_true, *json_false; |
95 | static SV *bool_true, *bool_false, *sv_json; |
82 | |
96 | |
83 | enum { |
97 | enum { |
84 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
98 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
85 | INCR_M_STR, // inside string |
99 | INCR_M_STR, // inside string |
86 | INCR_M_BS, // inside backslash |
100 | INCR_M_BS, // inside backslash |
… | |
… | |
287 | // a recursion depth of ten gives us >>500 bits |
301 | // a recursion depth of ten gives us >>500 bits |
288 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
302 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
289 | |
303 | |
290 | return neg ? -accum : accum; |
304 | return neg ? -accum : accum; |
291 | } |
305 | } |
|
|
306 | |
|
|
307 | // target of scalar reference is bool? -1 == nope, 0 == false, 1 == true |
|
|
308 | static int |
|
|
309 | ref_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 |
|
|
330 | static int |
|
|
331 | json_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 | |
292 | ///////////////////////////////////////////////////////////////////////////// |
347 | ///////////////////////////////////////////////////////////////////////////// |
293 | // encoder |
348 | // encoder |
294 | |
349 | |
295 | // structure used for encoding JSON |
350 | // structure used for encoding JSON |
296 | typedef struct |
351 | typedef struct |
… | |
… | |
682 | // encode objects, arrays and special \0=false and \1=true values. |
737 | // encode objects, arrays and special \0=false and \1=true values. |
683 | static void |
738 | static void |
684 | encode_rv (enc_t *enc, SV *sv) |
739 | encode_rv (enc_t *enc, SV *sv) |
685 | { |
740 | { |
686 | svtype svt; |
741 | svtype svt; |
|
|
742 | GV *method; |
687 | |
743 | |
688 | SvGETMAGIC (sv); |
744 | SvGETMAGIC (sv); |
689 | svt = SvTYPE (sv); |
745 | svt = SvTYPE (sv); |
690 | |
746 | |
691 | if (expect_false (SvOBJECT (sv))) |
747 | if (expect_false (SvOBJECT (sv))) |
692 | { |
748 | { |
693 | HV *stash = !JSON_SLOW || json_boolean_stash |
749 | HV *stash = SvSTASH (sv); |
694 | ? json_boolean_stash |
|
|
695 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
696 | |
750 | |
697 | if (SvSTASH (sv) == stash) |
751 | if (stash == bool_stash) |
698 | { |
752 | { |
699 | if (SvIV (sv)) |
753 | if (SvIV (sv)) |
700 | encode_str (enc, "true", 4, 0); |
754 | encode_str (enc, "true", 4, 0); |
701 | else |
755 | else |
702 | encode_str (enc, "false", 5, 0); |
756 | encode_str (enc, "false", 5, 0); |
703 | } |
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); |
704 | else |
825 | 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", |
826 | 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)))); |
827 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
751 | } |
|
|
752 | } |
828 | } |
753 | else if (svt == SVt_PVHV) |
829 | else if (svt == SVt_PVHV) |
754 | encode_hv (enc, (HV *)sv); |
830 | encode_hv (enc, (HV *)sv); |
755 | else if (svt == SVt_PVAV) |
831 | else if (svt == SVt_PVAV) |
756 | encode_av (enc, (AV *)sv); |
832 | encode_av (enc, (AV *)sv); |
757 | else if (svt < SVt_PVAV) |
833 | else if (svt < SVt_PVAV) |
758 | { |
834 | { |
759 | STRLEN len = 0; |
835 | int bool_type = ref_bool_type (sv); |
760 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
761 | |
836 | |
762 | if (len == 1 && *pv == '1') |
837 | if (bool_type == 1) |
763 | encode_str (enc, "true", 4, 0); |
838 | encode_str (enc, "true", 4, 0); |
764 | else if (len == 1 && *pv == '0') |
839 | else if (bool_type == 0) |
765 | encode_str (enc, "false", 5, 0); |
840 | encode_str (enc, "false", 5, 0); |
766 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
841 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
767 | encode_str (enc, "null", 4, 0); |
842 | encode_str (enc, "null", 4, 0); |
768 | else |
843 | else |
769 | 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", |
… | |
… | |
852 | static SV * |
927 | static SV * |
853 | encode_json (SV *scalar, JSON *json) |
928 | encode_json (SV *scalar, JSON *json) |
854 | { |
929 | { |
855 | enc_t enc; |
930 | enc_t enc; |
856 | |
931 | |
857 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
932 | 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)"); |
933 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
859 | |
934 | |
860 | enc.json = *json; |
935 | enc.json = *json; |
861 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
936 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
862 | enc.cur = SvPVX (enc.sv); |
937 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
1069 | *cur++ = *dec_cur++; |
1144 | *cur++ = *dec_cur++; |
1070 | while (--clen); |
1145 | while (--clen); |
1071 | |
1146 | |
1072 | utf8 = 1; |
1147 | utf8 = 1; |
1073 | } |
1148 | } |
|
|
1149 | else if (ch == '\t' && dec->json.flags & F_RELAXED) |
|
|
1150 | *cur++ = ch; |
1074 | else |
1151 | else |
1075 | { |
1152 | { |
1076 | --dec_cur; |
1153 | --dec_cur; |
1077 | |
1154 | |
1078 | if (!ch) |
1155 | if (!ch) |
… | |
… | |
1400 | |
1477 | |
1401 | hv_iterinit (hv); |
1478 | hv_iterinit (hv); |
1402 | he = hv_iternext (hv); |
1479 | he = hv_iternext (hv); |
1403 | hv_iterinit (hv); |
1480 | hv_iterinit (hv); |
1404 | |
1481 | |
1405 | // the next line creates a mortal sv each time its called. |
1482 | // the next line creates a mortal sv each time it's called. |
1406 | // might want to optimise this for common cases. |
1483 | // might want to optimise this for common cases. |
1407 | 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); |
1408 | |
1485 | |
1409 | if (cb) |
1486 | if (cb) |
1410 | { |
1487 | { |
1411 | dSP; |
1488 | dSP; |
1412 | int count; |
1489 | int count; |
1413 | |
1490 | |
1414 | ENTER; SAVETMPS; PUSHMARK (SP); |
1491 | ENTER; SAVETMPS; |
|
|
1492 | SAVESTACK_POS (); |
|
|
1493 | PUSHMARK (SP); |
1415 | XPUSHs (HeVAL (he)); |
1494 | XPUSHs (HeVAL (he)); |
1416 | sv_2mortal (sv); |
1495 | sv_2mortal (sv); |
1417 | |
1496 | |
1418 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1497 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1419 | |
1498 | |
… | |
… | |
1432 | if (dec->json.cb_object) |
1511 | if (dec->json.cb_object) |
1433 | { |
1512 | { |
1434 | dSP; |
1513 | dSP; |
1435 | int count; |
1514 | int count; |
1436 | |
1515 | |
1437 | ENTER; SAVETMPS; PUSHMARK (SP); |
1516 | ENTER; SAVETMPS; |
|
|
1517 | SAVESTACK_POS (); |
|
|
1518 | PUSHMARK (SP); |
1438 | XPUSHs (sv_2mortal (sv)); |
1519 | XPUSHs (sv_2mortal (sv)); |
1439 | |
1520 | |
1440 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1521 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1441 | |
1522 | |
1442 | if (count == 1) |
1523 | if (count == 1) |
… | |
… | |
1458 | DEC_DEC_DEPTH; |
1539 | DEC_DEC_DEPTH; |
1459 | return 0; |
1540 | return 0; |
1460 | } |
1541 | } |
1461 | |
1542 | |
1462 | static SV * |
1543 | static SV * |
|
|
1544 | decode_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 | |
|
|
1620 | fail: |
|
|
1621 | SvREFCNT_dec (tag); |
|
|
1622 | SvREFCNT_dec (val); |
|
|
1623 | return 0; |
|
|
1624 | } |
|
|
1625 | |
|
|
1626 | static SV * |
1463 | decode_sv (dec_t *dec) |
1627 | decode_sv (dec_t *dec) |
1464 | { |
1628 | { |
1465 | // the beauty of JSON: you need exactly one character lookahead |
1629 | // the beauty of JSON: you need exactly one character lookahead |
1466 | // to parse everything. |
1630 | // to parse everything. |
1467 | switch (*dec->cur) |
1631 | switch (*dec->cur) |
1468 | { |
1632 | { |
1469 | case '"': ++dec->cur; return decode_str (dec); |
1633 | case '"': ++dec->cur; return decode_str (dec); |
1470 | case '[': ++dec->cur; return decode_av (dec); |
1634 | case '[': ++dec->cur; return decode_av (dec); |
1471 | case '{': ++dec->cur; return decode_hv (dec); |
1635 | case '{': ++dec->cur; return decode_hv (dec); |
|
|
1636 | case '(': return decode_tag (dec); |
1472 | |
1637 | |
1473 | case '-': |
1638 | case '-': |
1474 | case '0': case '1': case '2': case '3': case '4': |
1639 | case '0': case '1': case '2': case '3': case '4': |
1475 | case '5': case '6': case '7': case '8': case '9': |
1640 | case '5': case '6': case '7': case '8': case '9': |
1476 | return decode_num (dec); |
1641 | return decode_num (dec); |
… | |
… | |
1478 | case 't': |
1643 | case 't': |
1479 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1644 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1480 | { |
1645 | { |
1481 | dec->cur += 4; |
1646 | dec->cur += 4; |
1482 | #if JSON_SLOW |
1647 | #if JSON_SLOW |
1483 | json_true = get_bool ("JSON::XS::true"); |
1648 | bool_true = get_bool ("Types::Serialiser::true"); |
1484 | #endif |
1649 | #endif |
1485 | return newSVsv (json_true); |
1650 | return newSVsv (bool_true); |
1486 | } |
1651 | } |
1487 | else |
1652 | else |
1488 | ERR ("'true' expected"); |
1653 | ERR ("'true' expected"); |
1489 | |
1654 | |
1490 | break; |
1655 | break; |
… | |
… | |
1492 | case 'f': |
1657 | case 'f': |
1493 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1658 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1494 | { |
1659 | { |
1495 | dec->cur += 5; |
1660 | dec->cur += 5; |
1496 | #if JSON_SLOW |
1661 | #if JSON_SLOW |
1497 | json_false = get_bool ("JSON::XS::false"); |
1662 | bool_false = get_bool ("Types::Serialiser::false"); |
1498 | #endif |
1663 | #endif |
1499 | return newSVsv (json_false); |
1664 | return newSVsv (bool_false); |
1500 | } |
1665 | } |
1501 | else |
1666 | else |
1502 | ERR ("'false' expected"); |
1667 | ERR ("'false' expected"); |
1503 | |
1668 | |
1504 | break; |
1669 | break; |
… | |
… | |
1513 | ERR ("'null' expected"); |
1678 | ERR ("'null' expected"); |
1514 | |
1679 | |
1515 | break; |
1680 | break; |
1516 | |
1681 | |
1517 | default: |
1682 | default: |
1518 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1683 | ERR ("malformed JSON string, neither tag, array, object, number, string or atom"); |
1519 | break; |
1684 | break; |
1520 | } |
1685 | } |
1521 | |
1686 | |
1522 | fail: |
1687 | fail: |
1523 | return 0; |
1688 | return 0; |
… | |
… | |
1618 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1783 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1619 | } |
1784 | } |
1620 | |
1785 | |
1621 | sv = sv_2mortal (sv); |
1786 | sv = sv_2mortal (sv); |
1622 | |
1787 | |
1623 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1788 | 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)"); |
1789 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1625 | |
1790 | |
1626 | return sv; |
1791 | return sv; |
1627 | } |
1792 | } |
1628 | |
1793 | |
… | |
… | |
1750 | self->incr_mode = INCR_M_STR; |
1915 | self->incr_mode = INCR_M_STR; |
1751 | goto incr_m_str; |
1916 | goto incr_m_str; |
1752 | |
1917 | |
1753 | case '[': |
1918 | case '[': |
1754 | case '{': |
1919 | case '{': |
|
|
1920 | case '(': |
1755 | if (++self->incr_nest > self->max_depth) |
1921 | if (++self->incr_nest > self->max_depth) |
1756 | croak (ERR_NESTING_EXCEEDED); |
1922 | croak (ERR_NESTING_EXCEEDED); |
1757 | break; |
1923 | break; |
1758 | |
1924 | |
1759 | case ']': |
1925 | case ']': |
1760 | case '}': |
1926 | case '}': |
1761 | if (--self->incr_nest <= 0) |
1927 | if (--self->incr_nest <= 0) |
1762 | goto interrupt; |
1928 | goto interrupt; |
|
|
1929 | break; |
|
|
1930 | |
|
|
1931 | case ')': |
|
|
1932 | --self->incr_nest; |
1763 | break; |
1933 | break; |
1764 | |
1934 | |
1765 | case '#': |
1935 | case '#': |
1766 | self->incr_mode = INCR_M_C1; |
1936 | self->incr_mode = INCR_M_C1; |
1767 | goto incr_m_c; |
1937 | goto incr_m_c; |
… | |
… | |
1793 | i >= '0' && i <= '9' ? i - '0' |
1963 | i >= '0' && i <= '9' ? i - '0' |
1794 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1964 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1795 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1965 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1796 | : -1; |
1966 | : -1; |
1797 | |
1967 | |
1798 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1968 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1799 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1969 | bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1800 | |
|
|
1801 | json_true = get_bool ("JSON::XS::true"); |
1970 | bool_true = get_bool ("Types::Serialiser::true"); |
1802 | 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); |
1803 | |
1975 | |
1804 | 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 */ |
1805 | } |
1977 | } |
1806 | |
1978 | |
1807 | PROTOTYPES: DISABLE |
1979 | PROTOTYPES: DISABLE |
1808 | |
1980 | |
1809 | void CLONE (...) |
1981 | void CLONE (...) |
1810 | CODE: |
1982 | CODE: |
1811 | json_stash = 0; |
1983 | json_stash = 0; |
1812 | json_boolean_stash = 0; |
1984 | bool_stash = 0; |
1813 | |
1985 | |
1814 | void new (char *klass) |
1986 | void new (char *klass) |
1815 | PPCODE: |
1987 | PPCODE: |
1816 | { |
1988 | { |
1817 | SV *pv = NEWSV (0, sizeof (JSON)); |
1989 | SV *pv = NEWSV (0, sizeof (JSON)); |
… | |
… | |
1837 | shrink = F_SHRINK |
2009 | shrink = F_SHRINK |
1838 | allow_blessed = F_ALLOW_BLESSED |
2010 | allow_blessed = F_ALLOW_BLESSED |
1839 | convert_blessed = F_CONV_BLESSED |
2011 | convert_blessed = F_CONV_BLESSED |
1840 | relaxed = F_RELAXED |
2012 | relaxed = F_RELAXED |
1841 | allow_unknown = F_ALLOW_UNKNOWN |
2013 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
2014 | allow_tags = F_ALLOW_TAGS |
1842 | PPCODE: |
2015 | PPCODE: |
1843 | { |
2016 | { |
1844 | if (enable) |
2017 | if (enable) |
1845 | self->flags |= ix; |
2018 | self->flags |= ix; |
1846 | else |
2019 | else |
… | |
… | |
1862 | get_shrink = F_SHRINK |
2035 | get_shrink = F_SHRINK |
1863 | get_allow_blessed = F_ALLOW_BLESSED |
2036 | get_allow_blessed = F_ALLOW_BLESSED |
1864 | get_convert_blessed = F_CONV_BLESSED |
2037 | get_convert_blessed = F_CONV_BLESSED |
1865 | get_relaxed = F_RELAXED |
2038 | get_relaxed = F_RELAXED |
1866 | get_allow_unknown = F_ALLOW_UNKNOWN |
2039 | get_allow_unknown = F_ALLOW_UNKNOWN |
|
|
2040 | get_allow_tags = F_ALLOW_TAGS |
1867 | PPCODE: |
2041 | PPCODE: |
1868 | XPUSHs (boolSV (self->flags & ix)); |
2042 | XPUSHs (boolSV (self->flags & ix)); |
1869 | |
2043 | |
1870 | void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
2044 | void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
1871 | PPCODE: |
2045 | PPCODE: |