… | |
… | |
718 | 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))) |
719 | { |
719 | { |
720 | int count; |
720 | int count; |
721 | dSP; |
721 | dSP; |
722 | |
722 | |
723 | ENTER; SAVETMPS; PUSHMARK (SP); |
723 | ENTER; SAVETMPS; |
|
|
724 | SAVESTACK_POS (); |
|
|
725 | PUSHMARK (SP); |
724 | EXTEND (SP, 2); |
726 | EXTEND (SP, 2); |
725 | // 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 |
726 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
728 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
727 | PUSHs (sv_json); |
729 | PUSHs (sv_json); |
728 | |
730 | |
… | |
… | |
730 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
732 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
731 | SPAGAIN; |
733 | SPAGAIN; |
732 | |
734 | |
733 | // catch this surprisingly common error |
735 | // catch this surprisingly common error |
734 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
736 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
735 | 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))); |
736 | |
738 | |
737 | encode_ch (enc, '('); |
739 | encode_ch (enc, '('); |
738 | encode_ch (enc, '"'); |
740 | encode_ch (enc, '"'); |
739 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
741 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
740 | encode_ch (enc, '"'); |
742 | encode_ch (enc, '"'); |
… | |
… | |
749 | encode_ch (enc, ','); |
751 | encode_ch (enc, ','); |
750 | } |
752 | } |
751 | |
753 | |
752 | encode_ch (enc, ']'); |
754 | encode_ch (enc, ']'); |
753 | |
755 | |
754 | PUTBACK; |
|
|
755 | |
|
|
756 | FREETMPS; LEAVE; |
756 | FREETMPS; LEAVE; |
757 | } |
757 | } |
758 | 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))) |
759 | { |
759 | { |
760 | dSP; |
760 | dSP; |
761 | |
761 | |
762 | ENTER; SAVETMPS; PUSHMARK (SP); |
762 | ENTER; SAVETMPS; |
|
|
763 | PUSHMARK (SP); |
763 | // 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 |
764 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
765 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
765 | |
766 | |
766 | // 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 |
767 | PUTBACK; |
768 | PUTBACK; |
… | |
… | |
882 | else |
883 | else |
883 | 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", |
884 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
885 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
885 | } |
886 | } |
886 | |
887 | |
|
|
888 | static int |
|
|
889 | json_scalar (SV *scalar) |
|
|
890 | { |
|
|
891 | return 0;//D |
|
|
892 | if (!SvROK (scalar)) |
|
|
893 | return 1; |
|
|
894 | } |
|
|
895 | |
887 | static SV * |
896 | static SV * |
888 | encode_json (SV *scalar, JSON *json) |
897 | encode_json (SV *scalar, JSON *json) |
889 | { |
898 | { |
890 | enc_t enc; |
899 | enc_t enc; |
891 | |
900 | |
892 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
901 | if (!(json->flags & F_ALLOW_NONREF) && json_scalar (scalar)) |
893 | 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)"); |
894 | |
903 | |
895 | enc.json = *json; |
904 | enc.json = *json; |
896 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
905 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
897 | enc.cur = SvPVX (enc.sv); |
906 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
1104 | *cur++ = *dec_cur++; |
1113 | *cur++ = *dec_cur++; |
1105 | while (--clen); |
1114 | while (--clen); |
1106 | |
1115 | |
1107 | utf8 = 1; |
1116 | utf8 = 1; |
1108 | } |
1117 | } |
|
|
1118 | else if (ch == '\t' && dec->json.flags & F_RELAXED) |
|
|
1119 | *cur++ = ch; |
1109 | else |
1120 | else |
1110 | { |
1121 | { |
1111 | --dec_cur; |
1122 | --dec_cur; |
1112 | |
1123 | |
1113 | if (!ch) |
1124 | if (!ch) |
… | |
… | |
1435 | |
1446 | |
1436 | hv_iterinit (hv); |
1447 | hv_iterinit (hv); |
1437 | he = hv_iternext (hv); |
1448 | he = hv_iternext (hv); |
1438 | hv_iterinit (hv); |
1449 | hv_iterinit (hv); |
1439 | |
1450 | |
1440 | // the next line creates a mortal sv each time its called. |
1451 | // the next line creates a mortal sv each time it's called. |
1441 | // might want to optimise this for common cases. |
1452 | // might want to optimise this for common cases. |
1442 | 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); |
1443 | |
1454 | |
1444 | if (cb) |
1455 | if (cb) |
1445 | { |
1456 | { |
1446 | dSP; |
1457 | dSP; |
1447 | int count; |
1458 | int count; |
1448 | |
1459 | |
1449 | ENTER; SAVETMPS; PUSHMARK (SP); |
1460 | ENTER; SAVETMPS; |
|
|
1461 | SAVESTACK_POS (); |
|
|
1462 | PUSHMARK (SP); |
1450 | XPUSHs (HeVAL (he)); |
1463 | XPUSHs (HeVAL (he)); |
1451 | sv_2mortal (sv); |
1464 | sv_2mortal (sv); |
1452 | |
1465 | |
1453 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1466 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1454 | |
1467 | |
… | |
… | |
1467 | if (dec->json.cb_object) |
1480 | if (dec->json.cb_object) |
1468 | { |
1481 | { |
1469 | dSP; |
1482 | dSP; |
1470 | int count; |
1483 | int count; |
1471 | |
1484 | |
1472 | ENTER; SAVETMPS; PUSHMARK (SP); |
1485 | ENTER; SAVETMPS; |
|
|
1486 | SAVESTACK_POS (); |
|
|
1487 | PUSHMARK (SP); |
1473 | XPUSHs (sv_2mortal (sv)); |
1488 | XPUSHs (sv_2mortal (sv)); |
1474 | |
1489 | |
1475 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1490 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1476 | |
1491 | |
1477 | if (count == 1) |
1492 | if (count == 1) |
… | |
… | |
1544 | if (!method) |
1559 | if (!method) |
1545 | 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)"); |
1546 | |
1561 | |
1547 | dSP; |
1562 | dSP; |
1548 | |
1563 | |
1549 | ENTER; SAVETMPS; PUSHMARK (SP); |
1564 | ENTER; SAVETMPS; |
|
|
1565 | PUSHMARK (SP); |
1550 | EXTEND (SP, len + 2); |
1566 | EXTEND (SP, len + 2); |
1551 | // 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 |
1552 | PUSHs (tag); |
1568 | PUSHs (tag); |
1553 | PUSHs (sv_json); |
1569 | PUSHs (sv_json); |
1554 | |
1570 | |