… | |
… | |
306 | encode_ch (enc, '['); encode_nl (enc); |
306 | encode_ch (enc, '['); encode_nl (enc); |
307 | ++enc->indent; |
307 | ++enc->indent; |
308 | |
308 | |
309 | for (i = 0; i <= len; ++i) |
309 | for (i = 0; i <= len; ++i) |
310 | { |
310 | { |
|
|
311 | SV **svp = av_fetch (av, i, 0); |
|
|
312 | |
311 | encode_indent (enc); |
313 | encode_indent (enc); |
312 | encode_sv (enc, *av_fetch (av, i, 0)); |
314 | |
|
|
315 | if (svp) |
|
|
316 | encode_sv (enc, *svp); |
|
|
317 | else |
|
|
318 | encode_str (enc, "null", 4, 0); |
313 | |
319 | |
314 | if (i < len) |
320 | if (i < len) |
315 | encode_comma (enc); |
321 | encode_comma (enc); |
316 | } |
322 | } |
317 | |
323 | |
… | |
… | |
489 | } |
495 | } |
490 | #endif |
496 | #endif |
491 | if (enc->json.flags & F_CONV_BLESSED) |
497 | if (enc->json.flags & F_CONV_BLESSED) |
492 | { |
498 | { |
493 | // we re-bless the reference to get overload and other niceties right |
499 | // we re-bless the reference to get overload and other niceties right |
494 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
495 | |
501 | |
496 | if (to_json) |
502 | if (to_json) |
497 | { |
503 | { |
|
|
504 | int count; |
|
|
505 | dSP; |
|
|
506 | |
498 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
507 | ENTER; SAVETMPS; PUSHMARK (SP); |
499 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
500 | |
509 | |
501 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
510 | // calling with G_SCALAR ensures that we always get a 1 return value |
502 | // check anyways. |
|
|
503 | PUTBACK; |
511 | PUTBACK; |
504 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
512 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
505 | SPAGAIN; |
513 | SPAGAIN; |
506 | |
514 | |
|
|
515 | // catch this surprisingly common error |
|
|
516 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
517 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
518 | |
|
|
519 | sv = POPs; |
|
|
520 | PUTBACK; |
|
|
521 | |
507 | encode_sv (enc, POPs); |
522 | encode_sv (enc, sv); |
508 | |
523 | |
509 | FREETMPS; LEAVE; |
524 | FREETMPS; LEAVE; |
510 | } |
525 | } |
511 | else if (enc->json.flags & F_ALLOW_BLESSED) |
526 | else if (enc->json.flags & F_ALLOW_BLESSED) |
512 | encode_str (enc, "null", 4, 0); |
527 | encode_str (enc, "null", 4, 0); |
… | |
… | |
1113 | // might want to optimise this for common cases. |
1128 | // might want to optimise this for common cases. |
1114 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1129 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1115 | |
1130 | |
1116 | if (cb) |
1131 | if (cb) |
1117 | { |
1132 | { |
|
|
1133 | dSP; |
1118 | int count; |
1134 | int count; |
1119 | ENTER; SAVETMPS; |
|
|
1120 | |
1135 | |
1121 | dSP; PUSHMARK (SP); |
1136 | ENTER; SAVETMPS; PUSHMARK (SP); |
1122 | XPUSHs (HeVAL (he)); |
1137 | XPUSHs (HeVAL (he)); |
1123 | |
1138 | |
1124 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1139 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1125 | |
1140 | |
1126 | if (count == 1) |
1141 | if (count == 1) |
… | |
… | |
1134 | } |
1149 | } |
1135 | } |
1150 | } |
1136 | |
1151 | |
1137 | if (dec->json.cb_object) |
1152 | if (dec->json.cb_object) |
1138 | { |
1153 | { |
|
|
1154 | dSP; |
1139 | int count; |
1155 | int count; |
1140 | ENTER; SAVETMPS; |
|
|
1141 | |
1156 | |
1142 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
1157 | ENTER; SAVETMPS; PUSHMARK (SP); |
1143 | XPUSHs (sv_2mortal (sv)); |
1158 | XPUSHs (sv_2mortal (sv)); |
1144 | |
1159 | |
1145 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1160 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1146 | |
1161 | |
1147 | if (count == 1) |
1162 | if (count == 1) |