… | |
… | |
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 | |
… | |
… | |
707 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
718 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
708 | { |
719 | { |
709 | int count; |
720 | int count; |
710 | dSP; |
721 | dSP; |
711 | |
722 | |
712 | ENTER; SAVETMPS; PUSHMARK (SP); |
723 | ENTER; SAVETMPS; |
|
|
724 | SAVESTACK_POS (); |
|
|
725 | PUSHMARK (SP); |
713 | EXTEND (SP, 2); |
726 | EXTEND (SP, 2); |
714 | // we re-bless the reference to get overload and other niceties right |
727 | // we re-bless the reference to get overload and other niceties right |
715 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
728 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
716 | PUSHs (sv_json); |
729 | PUSHs (sv_json); |
717 | |
730 | |
… | |
… | |
719 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
732 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
720 | SPAGAIN; |
733 | SPAGAIN; |
721 | |
734 | |
722 | // catch this surprisingly common error |
735 | // catch this surprisingly common error |
723 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
736 | 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))); |
737 | croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
725 | |
738 | |
726 | encode_ch (enc, '('); |
739 | encode_ch (enc, '('); |
727 | encode_ch (enc, '"'); |
740 | encode_ch (enc, '"'); |
728 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
741 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
729 | encode_ch (enc, '"'); |
742 | encode_ch (enc, '"'); |
… | |
… | |
738 | encode_ch (enc, ','); |
751 | encode_ch (enc, ','); |
739 | } |
752 | } |
740 | |
753 | |
741 | encode_ch (enc, ']'); |
754 | encode_ch (enc, ']'); |
742 | |
755 | |
743 | PUTBACK; |
|
|
744 | |
|
|
745 | FREETMPS; LEAVE; |
756 | FREETMPS; LEAVE; |
746 | } |
757 | } |
747 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
758 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
748 | { |
759 | { |
749 | dSP; |
760 | dSP; |
750 | |
761 | |
751 | ENTER; SAVETMPS; PUSHMARK (SP); |
762 | ENTER; SAVETMPS; |
|
|
763 | PUSHMARK (SP); |
752 | // we re-bless the reference to get overload and other niceties right |
764 | // we re-bless the reference to get overload and other niceties right |
753 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
765 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
754 | |
766 | |
755 | // calling with G_SCALAR ensures that we always get a 1 return value |
767 | // calling with G_SCALAR ensures that we always get a 1 return value |
756 | PUTBACK; |
768 | PUTBACK; |
… | |
… | |
871 | else |
883 | else |
872 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data", |
884 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data", |
873 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
885 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
874 | } |
886 | } |
875 | |
887 | |
|
|
888 | static int |
|
|
889 | json_scalar (SV *scalar) |
|
|
890 | { |
|
|
891 | return 0;//D |
|
|
892 | if (!SvROK (scalar)) |
|
|
893 | return 1; |
|
|
894 | } |
|
|
895 | |
876 | static SV * |
896 | static SV * |
877 | encode_json (SV *scalar, JSON *json) |
897 | encode_json (SV *scalar, JSON *json) |
878 | { |
898 | { |
879 | enc_t enc; |
899 | enc_t enc; |
880 | |
900 | |
881 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
901 | if (!(json->flags & F_ALLOW_NONREF) && json_scalar (scalar)) |
882 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
902 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
883 | |
903 | |
884 | enc.json = *json; |
904 | enc.json = *json; |
885 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
905 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
886 | enc.cur = SvPVX (enc.sv); |
906 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
1093 | *cur++ = *dec_cur++; |
1113 | *cur++ = *dec_cur++; |
1094 | while (--clen); |
1114 | while (--clen); |
1095 | |
1115 | |
1096 | utf8 = 1; |
1116 | utf8 = 1; |
1097 | } |
1117 | } |
|
|
1118 | else if (ch == '\t' && dec->json.flags & F_RELAXED) |
|
|
1119 | *cur++ = ch; |
1098 | else |
1120 | else |
1099 | { |
1121 | { |
1100 | --dec_cur; |
1122 | --dec_cur; |
1101 | |
1123 | |
1102 | if (!ch) |
1124 | if (!ch) |
… | |
… | |
1424 | |
1446 | |
1425 | hv_iterinit (hv); |
1447 | hv_iterinit (hv); |
1426 | he = hv_iternext (hv); |
1448 | he = hv_iternext (hv); |
1427 | hv_iterinit (hv); |
1449 | hv_iterinit (hv); |
1428 | |
1450 | |
1429 | // the next line creates a mortal sv each time its called. |
1451 | // the next line creates a mortal sv each time it's called. |
1430 | // might want to optimise this for common cases. |
1452 | // might want to optimise this for common cases. |
1431 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1453 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1432 | |
1454 | |
1433 | if (cb) |
1455 | if (cb) |
1434 | { |
1456 | { |
1435 | dSP; |
1457 | dSP; |
1436 | int count; |
1458 | int count; |
1437 | |
1459 | |
1438 | ENTER; SAVETMPS; PUSHMARK (SP); |
1460 | ENTER; SAVETMPS; |
|
|
1461 | SAVESTACK_POS (); |
|
|
1462 | PUSHMARK (SP); |
1439 | XPUSHs (HeVAL (he)); |
1463 | XPUSHs (HeVAL (he)); |
1440 | sv_2mortal (sv); |
1464 | sv_2mortal (sv); |
1441 | |
1465 | |
1442 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1466 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1443 | |
1467 | |
… | |
… | |
1456 | if (dec->json.cb_object) |
1480 | if (dec->json.cb_object) |
1457 | { |
1481 | { |
1458 | dSP; |
1482 | dSP; |
1459 | int count; |
1483 | int count; |
1460 | |
1484 | |
1461 | ENTER; SAVETMPS; PUSHMARK (SP); |
1485 | ENTER; SAVETMPS; |
|
|
1486 | SAVESTACK_POS (); |
|
|
1487 | PUSHMARK (SP); |
1462 | XPUSHs (sv_2mortal (sv)); |
1488 | XPUSHs (sv_2mortal (sv)); |
1463 | |
1489 | |
1464 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1490 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1465 | |
1491 | |
1466 | if (count == 1) |
1492 | if (count == 1) |
… | |
… | |
1492 | if (!(dec->json.flags & F_ALLOW_TAGS)) |
1518 | if (!(dec->json.flags & F_ALLOW_TAGS)) |
1493 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1519 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1494 | |
1520 | |
1495 | ++dec->cur; |
1521 | ++dec->cur; |
1496 | |
1522 | |
|
|
1523 | decode_ws (dec); |
|
|
1524 | |
1497 | tag = decode_sv (dec); |
1525 | tag = decode_sv (dec); |
1498 | if (!tag) |
1526 | if (!tag) |
1499 | goto fail; |
1527 | goto fail; |
1500 | |
1528 | |
1501 | if (!SvPOK (tag)) |
1529 | if (!SvPOK (tag)) |
1502 | ERR ("malformed JSON string, (tag) must be a string"); |
1530 | ERR ("malformed JSON string, (tag) must be a string"); |
1503 | |
1531 | |
|
|
1532 | decode_ws (dec); |
|
|
1533 | |
1504 | if (*dec->cur != ')') |
1534 | if (*dec->cur != ')') |
1505 | ERR (") expected after tag"); |
1535 | ERR (") expected after tag"); |
1506 | |
1536 | |
1507 | ++dec->cur; |
1537 | ++dec->cur; |
|
|
1538 | |
|
|
1539 | decode_ws (dec); |
1508 | |
1540 | |
1509 | val = decode_sv (dec); |
1541 | val = decode_sv (dec); |
1510 | if (!val) |
1542 | if (!val) |
1511 | goto fail; |
1543 | goto fail; |
1512 | |
1544 | |
… | |
… | |
1527 | if (!method) |
1559 | if (!method) |
1528 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1560 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1529 | |
1561 | |
1530 | dSP; |
1562 | dSP; |
1531 | |
1563 | |
1532 | ENTER; SAVETMPS; PUSHMARK (SP); |
1564 | ENTER; SAVETMPS; |
|
|
1565 | PUSHMARK (SP); |
1533 | EXTEND (SP, len + 2); |
1566 | EXTEND (SP, len + 2); |
1534 | // we re-bless the reference to get overload and other niceties right |
1567 | // we re-bless the reference to get overload and other niceties right |
1535 | PUSHs (tag); |
1568 | PUSHs (tag); |
1536 | PUSHs (sv_json); |
1569 | PUSHs (sv_json); |
1537 | |
1570 | |