… | |
… | |
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 | |
… | |
… | |
488 | } |
489 | } |
489 | #endif |
490 | #endif |
490 | if (enc->json.flags & F_CONV_BLESSED) |
491 | if (enc->json.flags & F_CONV_BLESSED) |
491 | { |
492 | { |
492 | // we re-bless the reference to get overload and other niceties right |
493 | // we re-bless the reference to get overload and other niceties right |
493 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
494 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
494 | |
495 | |
495 | if (to_json) |
496 | if (to_json) |
496 | { |
497 | { |
|
|
498 | int count; |
|
|
499 | dSP; |
|
|
500 | |
497 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
501 | ENTER; SAVETMPS; PUSHMARK (SP); |
498 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
502 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
499 | |
503 | |
500 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
504 | // calling with G_SCALAR ensures that we always get a 1 return value |
501 | // check anyways. |
|
|
502 | PUTBACK; |
505 | PUTBACK; |
503 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
506 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
504 | SPAGAIN; |
507 | SPAGAIN; |
505 | |
508 | |
|
|
509 | // catch this surprisingly common error |
|
|
510 | 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))); |
|
|
512 | |
|
|
513 | sv = POPs; |
|
|
514 | PUTBACK; |
|
|
515 | |
506 | encode_sv (enc, POPs); |
516 | encode_sv (enc, sv); |
507 | |
517 | |
508 | FREETMPS; LEAVE; |
518 | FREETMPS; LEAVE; |
509 | } |
519 | } |
510 | else if (enc->json.flags & F_ALLOW_BLESSED) |
520 | else if (enc->json.flags & F_ALLOW_BLESSED) |
511 | encode_str (enc, "null", 4, 0); |
521 | encode_str (enc, "null", 4, 0); |
… | |
… | |
1098 | sv = newRV_noinc ((SV *)hv); |
1108 | sv = newRV_noinc ((SV *)hv); |
1099 | |
1109 | |
1100 | // check filter callbacks |
1110 | // check filter callbacks |
1101 | if (dec->json.flags & F_HOOK) |
1111 | if (dec->json.flags & F_HOOK) |
1102 | { |
1112 | { |
1103 | ENTER; SAVETMPS; |
|
|
1104 | |
|
|
1105 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
1113 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
1106 | { |
1114 | { |
|
|
1115 | HE *cb, *he; |
|
|
1116 | |
|
|
1117 | hv_iterinit (hv); |
|
|
1118 | he = hv_iternext (hv); |
|
|
1119 | hv_iterinit (hv); |
|
|
1120 | |
|
|
1121 | // the next line creates a mortal sv each time its called. |
|
|
1122 | // might want to optimise this for common cases. |
|
|
1123 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1124 | |
|
|
1125 | if (cb) |
|
|
1126 | { |
|
|
1127 | dSP; |
1107 | int count; |
1128 | int count; |
1108 | |
1129 | |
1109 | dSP; PUSHMARK (SP); |
1130 | ENTER; SAVETMPS; PUSHMARK (SP); |
1110 | XPUSHs (sv_2mortal (sv)); |
1131 | XPUSHs (HeVAL (he)); |
1111 | |
1132 | |
1112 | PUTBACK; count = call_sv (dec->json.cb_sk_object, G_ARRAY); SPAGAIN; |
1133 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1113 | |
1134 | |
1114 | if (count == 1) |
1135 | if (count == 1) |
1115 | { |
1136 | { |
1116 | sv = newSVsv (POPs); |
1137 | sv = newSVsv (POPs); |
1117 | goto filter_ok; |
1138 | FREETMPS; LEAVE; |
|
|
1139 | return sv; |
1118 | } |
1140 | } |
1119 | |
1141 | |
1120 | SvREFCNT_inc (sv); |
1142 | FREETMPS; LEAVE; |
|
|
1143 | } |
1121 | } |
1144 | } |
1122 | |
1145 | |
1123 | if (dec->json.cb_object) |
1146 | if (dec->json.cb_object) |
1124 | { |
1147 | { |
|
|
1148 | dSP; |
1125 | int count; |
1149 | int count; |
1126 | |
1150 | |
1127 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
1151 | ENTER; SAVETMPS; PUSHMARK (SP); |
1128 | XPUSHs (sv_2mortal (sv)); |
1152 | XPUSHs (sv_2mortal (sv)); |
1129 | |
1153 | |
1130 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1154 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1131 | |
1155 | |
1132 | if (count == 1) |
1156 | if (count == 1) |
1133 | { |
1157 | { |
1134 | sv = newSVsv (POPs); |
1158 | sv = newSVsv (POPs); |
1135 | goto filter_ok; |
1159 | FREETMPS; LEAVE; |
|
|
1160 | return sv; |
1136 | } |
1161 | } |
1137 | |
1162 | |
1138 | SvREFCNT_inc (sv); |
1163 | SvREFCNT_inc (sv); |
|
|
1164 | FREETMPS; LEAVE; |
1139 | } |
1165 | } |
1140 | |
|
|
1141 | filter_ok: |
|
|
1142 | FREETMPS; LEAVE; |
|
|
1143 | } |
1166 | } |
1144 | |
1167 | |
1145 | return sv; |
1168 | return sv; |
1146 | |
1169 | |
1147 | fail: |
1170 | fail: |
… | |
… | |
1383 | |
1406 | |
1384 | XPUSHs (ST (0)); |
1407 | XPUSHs (ST (0)); |
1385 | } |
1408 | } |
1386 | |
1409 | |
1387 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1410 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1388 | ALIAS: |
|
|
1389 | filter_json_single_key_object = 1 |
|
|
1390 | PPCODE: |
1411 | PPCODE: |
1391 | { |
1412 | { |
1392 | SV **svp; |
1413 | SvREFCNT_dec (self->cb_object); |
|
|
1414 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1393 | |
1415 | |
|
|
1416 | XPUSHs (ST (0)); |
|
|
1417 | } |
|
|
1418 | |
|
|
1419 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1420 | PPCODE: |
|
|
1421 | { |
|
|
1422 | if (!self->cb_sk_object) |
|
|
1423 | self->cb_sk_object = newHV (); |
|
|
1424 | |
1394 | if (!SvOK (cb)) |
1425 | if (SvOK (cb)) |
1395 | cb = 0; |
1426 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1396 | else |
1427 | else |
1397 | cb = newSVsv (cb); |
|
|
1398 | |
|
|
1399 | switch (ix) |
|
|
1400 | { |
1428 | { |
1401 | case 0: svp = &self->cb_object ; break; |
1429 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
1402 | case 1: svp = &self->cb_sk_object; break; |
1430 | |
|
|
1431 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1432 | { |
|
|
1433 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1434 | self->cb_sk_object = 0; |
|
|
1435 | } |
1403 | } |
1436 | } |
1404 | |
|
|
1405 | if (*svp) |
|
|
1406 | SvREFCNT_dec (*svp); |
|
|
1407 | |
|
|
1408 | *svp = cb; |
|
|
1409 | |
1437 | |
1410 | XPUSHs (ST (0)); |
1438 | XPUSHs (ST (0)); |
1411 | } |
1439 | } |
1412 | |
1440 | |
1413 | void encode (JSON *self, SV *scalar) |
1441 | void encode (JSON *self, SV *scalar) |
… | |
… | |
1425 | EXTEND (SP, 2); |
1453 | EXTEND (SP, 2); |
1426 | PUSHs (decode_json (jsonstr, self, &offset)); |
1454 | PUSHs (decode_json (jsonstr, self, &offset)); |
1427 | PUSHs (sv_2mortal (newSVuv (offset))); |
1455 | PUSHs (sv_2mortal (newSVuv (offset))); |
1428 | } |
1456 | } |
1429 | |
1457 | |
|
|
1458 | void DESTROY (JSON *self) |
|
|
1459 | CODE: |
|
|
1460 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1461 | SvREFCNT_dec (self->cb_object); |
|
|
1462 | |
1430 | PROTOTYPES: ENABLE |
1463 | PROTOTYPES: ENABLE |
1431 | |
1464 | |
1432 | void to_json (SV *scalar) |
1465 | void to_json (SV *scalar) |
1433 | PPCODE: |
1466 | PPCODE: |
1434 | { |
1467 | { |