… | |
… | |
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 | |
… | |
… | |
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); |
… | |
… | |
1437 | |
1446 | |
1438 | hv_iterinit (hv); |
1447 | hv_iterinit (hv); |
1439 | he = hv_iternext (hv); |
1448 | he = hv_iternext (hv); |
1440 | hv_iterinit (hv); |
1449 | hv_iterinit (hv); |
1441 | |
1450 | |
1442 | // the next line creates a mortal sv each time its called. |
1451 | // the next line creates a mortal sv each time it's called. |
1443 | // might want to optimise this for common cases. |
1452 | // might want to optimise this for common cases. |
1444 | 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); |
1445 | |
1454 | |
1446 | if (cb) |
1455 | if (cb) |
1447 | { |
1456 | { |
1448 | dSP; |
1457 | dSP; |
1449 | int count; |
1458 | int count; |
1450 | |
1459 | |
1451 | ENTER; SAVETMPS; PUSHMARK (SP); |
1460 | ENTER; SAVETMPS; |
|
|
1461 | SAVESTACK_POS (); |
|
|
1462 | PUSHMARK (SP); |
1452 | XPUSHs (HeVAL (he)); |
1463 | XPUSHs (HeVAL (he)); |
1453 | sv_2mortal (sv); |
1464 | sv_2mortal (sv); |
1454 | |
1465 | |
1455 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1466 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1456 | |
1467 | |
… | |
… | |
1469 | if (dec->json.cb_object) |
1480 | if (dec->json.cb_object) |
1470 | { |
1481 | { |
1471 | dSP; |
1482 | dSP; |
1472 | int count; |
1483 | int count; |
1473 | |
1484 | |
1474 | ENTER; SAVETMPS; PUSHMARK (SP); |
1485 | ENTER; SAVETMPS; |
|
|
1486 | SAVESTACK_POS (); |
|
|
1487 | PUSHMARK (SP); |
1475 | XPUSHs (sv_2mortal (sv)); |
1488 | XPUSHs (sv_2mortal (sv)); |
1476 | |
1489 | |
1477 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1490 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1478 | |
1491 | |
1479 | if (count == 1) |
1492 | if (count == 1) |
… | |
… | |
1546 | if (!method) |
1559 | if (!method) |
1547 | 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)"); |
1548 | |
1561 | |
1549 | dSP; |
1562 | dSP; |
1550 | |
1563 | |
1551 | ENTER; SAVETMPS; PUSHMARK (SP); |
1564 | ENTER; SAVETMPS; |
|
|
1565 | PUSHMARK (SP); |
1552 | EXTEND (SP, len + 2); |
1566 | EXTEND (SP, len + 2); |
1553 | // 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 |
1554 | PUSHs (tag); |
1568 | PUSHs (tag); |
1555 | PUSHs (sv_json); |
1569 | PUSHs (sv_json); |
1556 | |
1570 | |