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