… | |
… | |
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 | |
… | |
… | |
68 | #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?)" |
69 | |
80 | |
70 | #ifdef USE_ITHREADS |
81 | #ifdef USE_ITHREADS |
71 | # define JSON_SLOW 1 |
82 | # define JSON_SLOW 1 |
72 | # 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)) |
73 | #else |
85 | #else |
74 | # define JSON_SLOW 0 |
86 | # define JSON_SLOW 0 |
75 | # define JSON_STASH json_stash |
87 | # define JSON_STASH json_stash |
|
|
88 | # define BOOL_STASH bool_stash |
76 | #endif |
89 | #endif |
77 | |
90 | |
78 | // 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 |
79 | #define STACK_HES 64 |
92 | #define STACK_HES 64 |
80 | |
93 | |
81 | static HV *json_stash, *types_boolean_stash; // JSON::XS:: |
94 | static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean:: |
82 | static SV *types_true, *types_false, *sv_json; |
95 | static SV *bool_true, *bool_false, *sv_json; |
83 | |
96 | |
84 | enum { |
97 | enum { |
85 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
98 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
86 | INCR_M_STR, // inside string |
99 | INCR_M_STR, // inside string |
87 | INCR_M_BS, // inside backslash |
100 | INCR_M_BS, // inside backslash |
… | |
… | |
288 | // a recursion depth of ten gives us >>500 bits |
301 | // a recursion depth of ten gives us >>500 bits |
289 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
302 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
290 | |
303 | |
291 | return neg ? -accum : accum; |
304 | return neg ? -accum : accum; |
292 | } |
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 | return -1; |
|
|
326 | } |
|
|
327 | |
|
|
328 | // returns whether scalar is not a reference in the sense of allow_nonref |
|
|
329 | static int |
|
|
330 | json_nonref (SV *scalar) |
|
|
331 | { |
|
|
332 | if (!SvROK (scalar)) |
|
|
333 | return 1; |
|
|
334 | |
|
|
335 | scalar = SvRV (scalar); |
|
|
336 | |
|
|
337 | if (SvTYPE (scalar) >= SVt_PVMG) |
|
|
338 | { |
|
|
339 | if (SvSTASH (scalar) == bool_stash) |
|
|
340 | return 1; |
|
|
341 | |
|
|
342 | if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0) |
|
|
343 | return 1; |
|
|
344 | } |
|
|
345 | |
|
|
346 | return 0; |
|
|
347 | } |
|
|
348 | |
293 | ///////////////////////////////////////////////////////////////////////////// |
349 | ///////////////////////////////////////////////////////////////////////////// |
294 | // encoder |
350 | // encoder |
295 | |
351 | |
296 | // structure used for encoding JSON |
352 | // structure used for encoding JSON |
297 | typedef struct |
353 | typedef struct |
… | |
… | |
690 | SvGETMAGIC (sv); |
746 | SvGETMAGIC (sv); |
691 | svt = SvTYPE (sv); |
747 | svt = SvTYPE (sv); |
692 | |
748 | |
693 | if (expect_false (SvOBJECT (sv))) |
749 | if (expect_false (SvOBJECT (sv))) |
694 | { |
750 | { |
695 | HV *boolean_stash = !JSON_SLOW || types_boolean_stash |
|
|
696 | ? types_boolean_stash |
|
|
697 | : gv_stashpv ("Types::Serialiser::Boolean", 1); |
|
|
698 | HV *stash = SvSTASH (sv); |
751 | HV *stash = SvSTASH (sv); |
699 | |
752 | |
700 | if (stash == boolean_stash) |
753 | if (stash == bool_stash) |
701 | { |
754 | { |
702 | if (SvIV (sv)) |
755 | if (SvIV (sv)) |
703 | encode_str (enc, "true", 4, 0); |
756 | encode_str (enc, "true", 4, 0); |
704 | else |
757 | else |
705 | encode_str (enc, "false", 5, 0); |
758 | encode_str (enc, "false", 5, 0); |
… | |
… | |
707 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
760 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
708 | { |
761 | { |
709 | int count; |
762 | int count; |
710 | dSP; |
763 | dSP; |
711 | |
764 | |
712 | ENTER; SAVETMPS; PUSHMARK (SP); |
765 | ENTER; SAVETMPS; |
|
|
766 | SAVESTACK_POS (); |
|
|
767 | PUSHMARK (SP); |
713 | EXTEND (SP, 2); |
768 | EXTEND (SP, 2); |
714 | // we re-bless the reference to get overload and other niceties right |
769 | // we re-bless the reference to get overload and other niceties right |
715 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
770 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
716 | PUSHs (sv_json); |
771 | PUSHs (sv_json); |
717 | |
772 | |
… | |
… | |
719 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
774 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
720 | SPAGAIN; |
775 | SPAGAIN; |
721 | |
776 | |
722 | // catch this surprisingly common error |
777 | // catch this surprisingly common error |
723 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
778 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
724 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
779 | croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
725 | |
780 | |
726 | encode_ch (enc, '('); |
781 | encode_ch (enc, '('); |
727 | encode_ch (enc, '"'); |
782 | encode_ch (enc, '"'); |
728 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
783 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
729 | encode_ch (enc, '"'); |
784 | encode_ch (enc, '"'); |
… | |
… | |
738 | encode_ch (enc, ','); |
793 | encode_ch (enc, ','); |
739 | } |
794 | } |
740 | |
795 | |
741 | encode_ch (enc, ']'); |
796 | encode_ch (enc, ']'); |
742 | |
797 | |
743 | PUTBACK; |
|
|
744 | |
|
|
745 | FREETMPS; LEAVE; |
798 | FREETMPS; LEAVE; |
746 | } |
799 | } |
747 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
800 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
748 | { |
801 | { |
749 | dSP; |
802 | dSP; |
750 | |
803 | |
751 | ENTER; SAVETMPS; PUSHMARK (SP); |
804 | ENTER; SAVETMPS; |
|
|
805 | PUSHMARK (SP); |
752 | // we re-bless the reference to get overload and other niceties right |
806 | // we re-bless the reference to get overload and other niceties right |
753 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
807 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
754 | |
808 | |
755 | // calling with G_SCALAR ensures that we always get a 1 return value |
809 | // calling with G_SCALAR ensures that we always get a 1 return value |
756 | PUTBACK; |
810 | PUTBACK; |
… | |
… | |
778 | encode_hv (enc, (HV *)sv); |
832 | encode_hv (enc, (HV *)sv); |
779 | else if (svt == SVt_PVAV) |
833 | else if (svt == SVt_PVAV) |
780 | encode_av (enc, (AV *)sv); |
834 | encode_av (enc, (AV *)sv); |
781 | else if (svt < SVt_PVAV) |
835 | else if (svt < SVt_PVAV) |
782 | { |
836 | { |
783 | STRLEN len = 0; |
837 | int bool_type = ref_bool_type (sv); |
784 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
785 | |
838 | |
786 | if (len == 1 && *pv == '1') |
839 | if (bool_type == 1) |
787 | encode_str (enc, "true", 4, 0); |
840 | encode_str (enc, "true", 4, 0); |
788 | else if (len == 1 && *pv == '0') |
841 | else if (bool_type == 0) |
789 | encode_str (enc, "false", 5, 0); |
842 | encode_str (enc, "false", 5, 0); |
790 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
843 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
791 | encode_str (enc, "null", 4, 0); |
844 | encode_str (enc, "null", 4, 0); |
792 | else |
845 | else |
793 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
846 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
… | |
… | |
876 | static SV * |
929 | static SV * |
877 | encode_json (SV *scalar, JSON *json) |
930 | encode_json (SV *scalar, JSON *json) |
878 | { |
931 | { |
879 | enc_t enc; |
932 | enc_t enc; |
880 | |
933 | |
881 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
934 | if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar)) |
882 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
935 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
883 | |
936 | |
884 | enc.json = *json; |
937 | enc.json = *json; |
885 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
938 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
886 | enc.cur = SvPVX (enc.sv); |
939 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
1093 | *cur++ = *dec_cur++; |
1146 | *cur++ = *dec_cur++; |
1094 | while (--clen); |
1147 | while (--clen); |
1095 | |
1148 | |
1096 | utf8 = 1; |
1149 | utf8 = 1; |
1097 | } |
1150 | } |
|
|
1151 | else if (ch == '\t' && dec->json.flags & F_RELAXED) |
|
|
1152 | *cur++ = ch; |
1098 | else |
1153 | else |
1099 | { |
1154 | { |
1100 | --dec_cur; |
1155 | --dec_cur; |
1101 | |
1156 | |
1102 | if (!ch) |
1157 | if (!ch) |
… | |
… | |
1424 | |
1479 | |
1425 | hv_iterinit (hv); |
1480 | hv_iterinit (hv); |
1426 | he = hv_iternext (hv); |
1481 | he = hv_iternext (hv); |
1427 | hv_iterinit (hv); |
1482 | hv_iterinit (hv); |
1428 | |
1483 | |
1429 | // the next line creates a mortal sv each time its called. |
1484 | // the next line creates a mortal sv each time it's called. |
1430 | // might want to optimise this for common cases. |
1485 | // might want to optimise this for common cases. |
1431 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1486 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1432 | |
1487 | |
1433 | if (cb) |
1488 | if (cb) |
1434 | { |
1489 | { |
1435 | dSP; |
1490 | dSP; |
1436 | int count; |
1491 | int count; |
1437 | |
1492 | |
1438 | ENTER; SAVETMPS; PUSHMARK (SP); |
1493 | ENTER; SAVETMPS; |
|
|
1494 | SAVESTACK_POS (); |
|
|
1495 | PUSHMARK (SP); |
1439 | XPUSHs (HeVAL (he)); |
1496 | XPUSHs (HeVAL (he)); |
1440 | sv_2mortal (sv); |
1497 | sv_2mortal (sv); |
1441 | |
1498 | |
1442 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1499 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1443 | |
1500 | |
… | |
… | |
1456 | if (dec->json.cb_object) |
1513 | if (dec->json.cb_object) |
1457 | { |
1514 | { |
1458 | dSP; |
1515 | dSP; |
1459 | int count; |
1516 | int count; |
1460 | |
1517 | |
1461 | ENTER; SAVETMPS; PUSHMARK (SP); |
1518 | ENTER; SAVETMPS; |
|
|
1519 | SAVESTACK_POS (); |
|
|
1520 | PUSHMARK (SP); |
1462 | XPUSHs (sv_2mortal (sv)); |
1521 | XPUSHs (sv_2mortal (sv)); |
1463 | |
1522 | |
1464 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1523 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1465 | |
1524 | |
1466 | if (count == 1) |
1525 | if (count == 1) |
… | |
… | |
1492 | if (!(dec->json.flags & F_ALLOW_TAGS)) |
1551 | if (!(dec->json.flags & F_ALLOW_TAGS)) |
1493 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1552 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1494 | |
1553 | |
1495 | ++dec->cur; |
1554 | ++dec->cur; |
1496 | |
1555 | |
|
|
1556 | decode_ws (dec); |
|
|
1557 | |
1497 | tag = decode_sv (dec); |
1558 | tag = decode_sv (dec); |
1498 | if (!tag) |
1559 | if (!tag) |
1499 | goto fail; |
1560 | goto fail; |
1500 | |
1561 | |
1501 | if (!SvPOK (tag)) |
1562 | if (!SvPOK (tag)) |
1502 | ERR ("malformed JSON string, (tag) must be a string"); |
1563 | ERR ("malformed JSON string, (tag) must be a string"); |
1503 | |
1564 | |
|
|
1565 | decode_ws (dec); |
|
|
1566 | |
1504 | if (*dec->cur != ')') |
1567 | if (*dec->cur != ')') |
1505 | ERR (") expected after tag"); |
1568 | ERR (") expected after tag"); |
1506 | |
1569 | |
1507 | ++dec->cur; |
1570 | ++dec->cur; |
|
|
1571 | |
|
|
1572 | decode_ws (dec); |
1508 | |
1573 | |
1509 | val = decode_sv (dec); |
1574 | val = decode_sv (dec); |
1510 | if (!val) |
1575 | if (!val) |
1511 | goto fail; |
1576 | goto fail; |
1512 | |
1577 | |
… | |
… | |
1527 | if (!method) |
1592 | if (!method) |
1528 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1593 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1529 | |
1594 | |
1530 | dSP; |
1595 | dSP; |
1531 | |
1596 | |
1532 | ENTER; SAVETMPS; PUSHMARK (SP); |
1597 | ENTER; SAVETMPS; |
|
|
1598 | PUSHMARK (SP); |
1533 | EXTEND (SP, len + 2); |
1599 | EXTEND (SP, len + 2); |
1534 | // we re-bless the reference to get overload and other niceties right |
1600 | // we re-bless the reference to get overload and other niceties right |
1535 | PUSHs (tag); |
1601 | PUSHs (tag); |
1536 | PUSHs (sv_json); |
1602 | PUSHs (sv_json); |
1537 | |
1603 | |
… | |
… | |
1579 | case 't': |
1645 | case 't': |
1580 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1646 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1581 | { |
1647 | { |
1582 | dec->cur += 4; |
1648 | dec->cur += 4; |
1583 | #if JSON_SLOW |
1649 | #if JSON_SLOW |
1584 | types_true = get_bool ("Types::Serialiser::true"); |
1650 | bool_true = get_bool ("Types::Serialiser::true"); |
1585 | #endif |
1651 | #endif |
1586 | return newSVsv (types_true); |
1652 | return newSVsv (bool_true); |
1587 | } |
1653 | } |
1588 | else |
1654 | else |
1589 | ERR ("'true' expected"); |
1655 | ERR ("'true' expected"); |
1590 | |
1656 | |
1591 | break; |
1657 | break; |
… | |
… | |
1593 | case 'f': |
1659 | case 'f': |
1594 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1660 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1595 | { |
1661 | { |
1596 | dec->cur += 5; |
1662 | dec->cur += 5; |
1597 | #if JSON_SLOW |
1663 | #if JSON_SLOW |
1598 | types_false = get_bool ("Types::Serialiser::false"); |
1664 | bool_false = get_bool ("Types::Serialiser::false"); |
1599 | #endif |
1665 | #endif |
1600 | return newSVsv (types_false); |
1666 | return newSVsv (bool_false); |
1601 | } |
1667 | } |
1602 | else |
1668 | else |
1603 | ERR ("'false' expected"); |
1669 | ERR ("'false' expected"); |
1604 | |
1670 | |
1605 | break; |
1671 | break; |
… | |
… | |
1623 | fail: |
1689 | fail: |
1624 | return 0; |
1690 | return 0; |
1625 | } |
1691 | } |
1626 | |
1692 | |
1627 | static SV * |
1693 | static SV * |
1628 | decode_json (SV *string, JSON *json, char **offset_return) |
1694 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1629 | { |
1695 | { |
1630 | dec_t dec; |
1696 | dec_t dec; |
1631 | SV *sv; |
1697 | SV *sv; |
1632 | |
1698 | |
1633 | /* work around bugs in 5.10 where manipulating magic values |
1699 | /* work around bugs in 5.10 where manipulating magic values |
1634 | * makes perl ignore the magic in subsequent accesses. |
1700 | * makes perl ignore the magic in subsequent accesses. |
1635 | * also make a copy of non-PV values, to get them into a clean |
1701 | * also make a copy of non-PV values, to get them into a clean |
1636 | * state (SvPV should do that, but it's buggy, see below). |
1702 | * state (SvPV should do that, but it's buggy, see below). |
|
|
1703 | * |
|
|
1704 | * SvIsCOW_shared_hash works around a bug in perl (possibly 5.16), |
|
|
1705 | * as reported by Reini Urban. |
1637 | */ |
1706 | */ |
1638 | /*SvGETMAGIC (string);*/ |
1707 | /*SvGETMAGIC (string);*/ |
1639 | if (SvMAGICAL (string) || !SvPOK (string)) |
1708 | if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) |
1640 | string = sv_2mortal (newSVsv (string)); |
1709 | string = sv_2mortal (newSVsv (string)); |
1641 | |
1710 | |
1642 | SvUPGRADE (string, SVt_PV); |
1711 | SvUPGRADE (string, SVt_PV); |
1643 | |
1712 | |
1644 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1713 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
… | |
… | |
1683 | |
1752 | |
1684 | decode_ws (&dec); |
1753 | decode_ws (&dec); |
1685 | sv = decode_sv (&dec); |
1754 | sv = decode_sv (&dec); |
1686 | |
1755 | |
1687 | if (offset_return) |
1756 | if (offset_return) |
1688 | *offset_return = dec.cur; |
1757 | *offset_return = dec.cur - SvPVX (string); |
1689 | |
1758 | else if (sv) |
1690 | if (!(offset_return || !sv)) |
|
|
1691 | { |
1759 | { |
1692 | // check for trailing garbage |
1760 | // check for trailing garbage |
1693 | decode_ws (&dec); |
1761 | decode_ws (&dec); |
1694 | |
1762 | |
1695 | if (*dec.cur) |
1763 | if (*dec.cur) |
… | |
… | |
1719 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1787 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1720 | } |
1788 | } |
1721 | |
1789 | |
1722 | sv = sv_2mortal (sv); |
1790 | sv = sv_2mortal (sv); |
1723 | |
1791 | |
1724 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1792 | if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv)) |
1725 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1793 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1726 | |
1794 | |
1727 | return sv; |
1795 | return sv; |
1728 | } |
1796 | } |
1729 | |
1797 | |
… | |
… | |
1899 | i >= '0' && i <= '9' ? i - '0' |
1967 | i >= '0' && i <= '9' ? i - '0' |
1900 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1968 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1901 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1969 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1902 | : -1; |
1970 | : -1; |
1903 | |
1971 | |
1904 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1972 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1905 | types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1973 | bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1906 | |
|
|
1907 | types_true = get_bool ("Types::Serialiser::true"); |
1974 | bool_true = get_bool ("Types::Serialiser::true"); |
1908 | types_false = get_bool ("Types::Serialiser::false"); |
1975 | bool_false = get_bool ("Types::Serialiser::false"); |
1909 | |
1976 | |
1910 | sv_json = newSVpv ("JSON", 0); |
1977 | sv_json = newSVpv ("JSON", 0); |
1911 | SvREADONLY_on (sv_json); |
1978 | SvREADONLY_on (sv_json); |
1912 | |
1979 | |
1913 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1980 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
… | |
… | |
1915 | |
1982 | |
1916 | PROTOTYPES: DISABLE |
1983 | PROTOTYPES: DISABLE |
1917 | |
1984 | |
1918 | void CLONE (...) |
1985 | void CLONE (...) |
1919 | CODE: |
1986 | CODE: |
1920 | json_stash = 0; |
1987 | json_stash = 0; |
1921 | types_boolean_stash = 0; |
1988 | bool_stash = 0; |
1922 | |
1989 | |
1923 | void new (char *klass) |
1990 | void new (char *klass) |
1924 | PPCODE: |
1991 | PPCODE: |
1925 | { |
1992 | { |
1926 | SV *pv = NEWSV (0, sizeof (JSON)); |
1993 | SV *pv = NEWSV (0, sizeof (JSON)); |
… | |
… | |
2043 | |
2110 | |
2044 | void decode_prefix (JSON *self, SV *jsonstr) |
2111 | void decode_prefix (JSON *self, SV *jsonstr) |
2045 | PPCODE: |
2112 | PPCODE: |
2046 | { |
2113 | { |
2047 | SV *sv; |
2114 | SV *sv; |
2048 | char *offset; |
2115 | STRLEN offset; |
2049 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
2116 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
2050 | EXTEND (SP, 2); |
2117 | EXTEND (SP, 2); |
2051 | PUSHs (sv); |
2118 | PUSHs (sv); |
2052 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
2119 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, SvPV_nolen (jsonstr) + offset)))); |
2053 | } |
2120 | } |
2054 | |
2121 | |
2055 | void incr_parse (JSON *self, SV *jsonstr = 0) |
2122 | void incr_parse (JSON *self, SV *jsonstr = 0) |
2056 | PPCODE: |
2123 | PPCODE: |
2057 | { |
2124 | { |
… | |
… | |
2104 | |
2171 | |
2105 | if (GIMME_V != G_VOID) |
2172 | if (GIMME_V != G_VOID) |
2106 | do |
2173 | do |
2107 | { |
2174 | { |
2108 | SV *sv; |
2175 | SV *sv; |
2109 | char *offset; |
2176 | STRLEN offset; |
2110 | |
2177 | |
2111 | if (!INCR_DONE (self)) |
2178 | if (!INCR_DONE (self)) |
2112 | { |
2179 | { |
2113 | incr_parse (self); |
2180 | incr_parse (self); |
2114 | |
2181 | |
… | |
… | |
2130 | } |
2197 | } |
2131 | |
2198 | |
2132 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
2199 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
2133 | XPUSHs (sv); |
2200 | XPUSHs (sv); |
2134 | |
2201 | |
2135 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2202 | self->incr_pos -= offset; |
2136 | self->incr_nest = 0; |
2203 | self->incr_nest = 0; |
2137 | self->incr_mode = 0; |
2204 | self->incr_mode = 0; |
2138 | |
2205 | |
2139 | sv_chop (self->incr_text, offset); |
2206 | sv_chop (self->incr_text, SvPVX (self->incr_text) + offset); |
2140 | } |
2207 | } |
2141 | while (GIMME_V == G_ARRAY); |
2208 | while (GIMME_V == G_ARRAY); |
2142 | } |
2209 | } |
2143 | |
2210 | |
2144 | SV *incr_text (JSON *self) |
2211 | SV *incr_text (JSON *self) |