… | |
… | |
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 | |
… | |
… | |
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", 0); |
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; |
498 | dSP; |
505 | dSP; |
499 | |
506 | |
500 | ENTER; SAVETMPS; PUSHMARK (SP); |
507 | ENTER; SAVETMPS; PUSHMARK (SP); |
501 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
502 | |
509 | |
503 | // calling with G_SCALAR ensures that we always get a 1 return value |
510 | // calling with G_SCALAR ensures that we always get a 1 return value |
504 | // check anyways. |
|
|
505 | PUTBACK; |
511 | PUTBACK; |
506 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
512 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
507 | SPAGAIN; |
513 | SPAGAIN; |
508 | |
514 | |
509 | // catch this surprisingly common error |
515 | // catch this surprisingly common error |
510 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
516 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
511 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
517 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |