… | |
… | |
62 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
62 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
63 | static SV *json_true, *json_false; |
63 | static SV *json_true, *json_false; |
64 | |
64 | |
65 | typedef struct { |
65 | typedef struct { |
66 | U32 flags; |
66 | U32 flags; |
67 | SV *cb_object, *cb_sk_object; |
67 | SV *cb_object; |
|
|
68 | HV *cb_sk_object; |
68 | } JSON; |
69 | } JSON; |
69 | |
70 | |
70 | ///////////////////////////////////////////////////////////////////////////// |
71 | ///////////////////////////////////////////////////////////////////////////// |
71 | // utility functions |
72 | // utility functions |
72 | |
73 | |
… | |
… | |
305 | encode_ch (enc, '['); encode_nl (enc); |
306 | encode_ch (enc, '['); encode_nl (enc); |
306 | ++enc->indent; |
307 | ++enc->indent; |
307 | |
308 | |
308 | for (i = 0; i <= len; ++i) |
309 | for (i = 0; i <= len; ++i) |
309 | { |
310 | { |
|
|
311 | SV **svp = av_fetch (av, i, 0); |
|
|
312 | |
310 | encode_indent (enc); |
313 | encode_indent (enc); |
311 | 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); |
312 | |
319 | |
313 | if (i < len) |
320 | if (i < len) |
314 | encode_comma (enc); |
321 | encode_comma (enc); |
315 | } |
322 | } |
316 | |
323 | |
… | |
… | |
488 | } |
495 | } |
489 | #endif |
496 | #endif |
490 | if (enc->json.flags & F_CONV_BLESSED) |
497 | if (enc->json.flags & F_CONV_BLESSED) |
491 | { |
498 | { |
492 | // 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 |
493 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
494 | |
501 | |
495 | if (to_json) |
502 | if (to_json) |
496 | { |
503 | { |
|
|
504 | int count; |
|
|
505 | dSP; |
|
|
506 | |
497 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
507 | ENTER; SAVETMPS; PUSHMARK (SP); |
498 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
499 | |
509 | |
500 | // 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 |
501 | // check anyways. |
|
|
502 | PUTBACK; |
511 | PUTBACK; |
503 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
512 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
504 | SPAGAIN; |
513 | SPAGAIN; |
505 | |
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 | |
506 | encode_sv (enc, POPs); |
522 | encode_sv (enc, sv); |
507 | |
523 | |
508 | FREETMPS; LEAVE; |
524 | FREETMPS; LEAVE; |
509 | } |
525 | } |
510 | else if (enc->json.flags & F_ALLOW_BLESSED) |
526 | else if (enc->json.flags & F_ALLOW_BLESSED) |
511 | encode_str (enc, "null", 4, 0); |
527 | encode_str (enc, "null", 4, 0); |
… | |
… | |
1098 | sv = newRV_noinc ((SV *)hv); |
1114 | sv = newRV_noinc ((SV *)hv); |
1099 | |
1115 | |
1100 | // check filter callbacks |
1116 | // check filter callbacks |
1101 | if (dec->json.flags & F_HOOK) |
1117 | if (dec->json.flags & F_HOOK) |
1102 | { |
1118 | { |
1103 | ENTER; SAVETMPS; |
|
|
1104 | |
|
|
1105 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
1119 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
1106 | { |
1120 | { |
|
|
1121 | HE *cb, *he; |
|
|
1122 | |
|
|
1123 | hv_iterinit (hv); |
|
|
1124 | he = hv_iternext (hv); |
|
|
1125 | hv_iterinit (hv); |
|
|
1126 | |
|
|
1127 | // the next line creates a mortal sv each time its called. |
|
|
1128 | // might want to optimise this for common cases. |
|
|
1129 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1130 | |
|
|
1131 | if (cb) |
|
|
1132 | { |
|
|
1133 | dSP; |
1107 | int count; |
1134 | int count; |
1108 | |
1135 | |
1109 | dSP; PUSHMARK (SP); |
1136 | ENTER; SAVETMPS; PUSHMARK (SP); |
1110 | XPUSHs (sv_2mortal (sv)); |
1137 | XPUSHs (HeVAL (he)); |
1111 | |
1138 | |
1112 | PUTBACK; count = call_sv (dec->json.cb_sk_object, G_ARRAY); SPAGAIN; |
1139 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1113 | |
1140 | |
1114 | if (count == 1) |
1141 | if (count == 1) |
1115 | { |
1142 | { |
1116 | sv = newSVsv (POPs); |
1143 | sv = newSVsv (POPs); |
1117 | goto filter_ok; |
1144 | FREETMPS; LEAVE; |
|
|
1145 | return sv; |
1118 | } |
1146 | } |
1119 | |
1147 | |
1120 | SvREFCNT_inc (sv); |
1148 | FREETMPS; LEAVE; |
|
|
1149 | } |
1121 | } |
1150 | } |
1122 | |
1151 | |
1123 | if (dec->json.cb_object) |
1152 | if (dec->json.cb_object) |
1124 | { |
1153 | { |
|
|
1154 | dSP; |
1125 | int count; |
1155 | int count; |
1126 | |
1156 | |
1127 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
1157 | ENTER; SAVETMPS; PUSHMARK (SP); |
1128 | XPUSHs (sv_2mortal (sv)); |
1158 | XPUSHs (sv_2mortal (sv)); |
1129 | |
1159 | |
1130 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1160 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1131 | |
1161 | |
1132 | if (count == 1) |
1162 | if (count == 1) |
1133 | { |
1163 | { |
1134 | sv = newSVsv (POPs); |
1164 | sv = newSVsv (POPs); |
1135 | goto filter_ok; |
1165 | FREETMPS; LEAVE; |
|
|
1166 | return sv; |
1136 | } |
1167 | } |
1137 | |
1168 | |
1138 | SvREFCNT_inc (sv); |
1169 | SvREFCNT_inc (sv); |
|
|
1170 | FREETMPS; LEAVE; |
1139 | } |
1171 | } |
1140 | |
|
|
1141 | filter_ok: |
|
|
1142 | FREETMPS; LEAVE; |
|
|
1143 | } |
1172 | } |
1144 | |
1173 | |
1145 | return sv; |
1174 | return sv; |
1146 | |
1175 | |
1147 | fail: |
1176 | fail: |
… | |
… | |
1383 | |
1412 | |
1384 | XPUSHs (ST (0)); |
1413 | XPUSHs (ST (0)); |
1385 | } |
1414 | } |
1386 | |
1415 | |
1387 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1416 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1388 | ALIAS: |
|
|
1389 | filter_json_single_key_object = 1 |
|
|
1390 | PPCODE: |
1417 | PPCODE: |
1391 | { |
1418 | { |
1392 | SV **svp; |
1419 | SvREFCNT_dec (self->cb_object); |
|
|
1420 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1393 | |
1421 | |
|
|
1422 | XPUSHs (ST (0)); |
|
|
1423 | } |
|
|
1424 | |
|
|
1425 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1426 | PPCODE: |
|
|
1427 | { |
|
|
1428 | if (!self->cb_sk_object) |
|
|
1429 | self->cb_sk_object = newHV (); |
|
|
1430 | |
1394 | if (!SvOK (cb)) |
1431 | if (SvOK (cb)) |
1395 | cb = 0; |
1432 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1396 | else |
1433 | else |
1397 | cb = newSVsv (cb); |
|
|
1398 | |
|
|
1399 | switch (ix) |
|
|
1400 | { |
1434 | { |
1401 | case 0: svp = &self->cb_object ; break; |
1435 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
1402 | case 1: svp = &self->cb_sk_object; break; |
1436 | |
|
|
1437 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1438 | { |
|
|
1439 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1440 | self->cb_sk_object = 0; |
|
|
1441 | } |
1403 | } |
1442 | } |
1404 | |
|
|
1405 | if (*svp) |
|
|
1406 | SvREFCNT_dec (*svp); |
|
|
1407 | |
|
|
1408 | *svp = cb; |
|
|
1409 | |
1443 | |
1410 | XPUSHs (ST (0)); |
1444 | XPUSHs (ST (0)); |
1411 | } |
1445 | } |
1412 | |
1446 | |
1413 | void encode (JSON *self, SV *scalar) |
1447 | void encode (JSON *self, SV *scalar) |
… | |
… | |
1425 | EXTEND (SP, 2); |
1459 | EXTEND (SP, 2); |
1426 | PUSHs (decode_json (jsonstr, self, &offset)); |
1460 | PUSHs (decode_json (jsonstr, self, &offset)); |
1427 | PUSHs (sv_2mortal (newSVuv (offset))); |
1461 | PUSHs (sv_2mortal (newSVuv (offset))); |
1428 | } |
1462 | } |
1429 | |
1463 | |
|
|
1464 | void DESTROY (JSON *self) |
|
|
1465 | CODE: |
|
|
1466 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1467 | SvREFCNT_dec (self->cb_object); |
|
|
1468 | |
1430 | PROTOTYPES: ENABLE |
1469 | PROTOTYPES: ENABLE |
1431 | |
1470 | |
1432 | void to_json (SV *scalar) |
1471 | void to_json (SV *scalar) |
1433 | PPCODE: |
1472 | PPCODE: |
1434 | { |
1473 | { |