… | |
… | |
25 | #define F_SPACE_BEFORE 0x00000020UL |
25 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_SHRINK 0x00000200UL |
28 | #define F_SHRINK 0x00000200UL |
29 | #define F_ALLOW_BLESSED 0x00000400UL |
29 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
30 | #define F_CONV_BLESSED 0x00000800UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define S_MAXDEPTH 27 |
32 | #define S_MAXDEPTH 27 |
33 | #define F_MAXSIZE 0x01f00000UL |
33 | #define F_MAXSIZE 0x01f00000UL |
34 | #define S_MAXSIZE 20 |
34 | #define S_MAXSIZE 20 |
|
|
35 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
35 | |
36 | |
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
37 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
38 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
38 | |
39 | |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
40 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
… | |
… | |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
62 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
62 | static SV *json_true, *json_false; |
63 | static SV *json_true, *json_false; |
63 | |
64 | |
64 | typedef struct { |
65 | typedef struct { |
65 | U32 flags; |
66 | U32 flags; |
|
|
67 | SV *cb_object; |
|
|
68 | HV *cb_sk_object; |
66 | } JSON; |
69 | } JSON; |
67 | |
70 | |
68 | ///////////////////////////////////////////////////////////////////////////// |
71 | ///////////////////////////////////////////////////////////////////////////// |
69 | // utility functions |
72 | // utility functions |
70 | |
73 | |
… | |
… | |
470 | |
473 | |
471 | if (expect_false (SvOBJECT (sv))) |
474 | if (expect_false (SvOBJECT (sv))) |
472 | { |
475 | { |
473 | if (SvSTASH (sv) == json_boolean_stash) |
476 | if (SvSTASH (sv) == json_boolean_stash) |
474 | { |
477 | { |
475 | if (SvIV (sv) == 0) |
478 | if (SvIV (sv)) |
|
|
479 | encode_str (enc, "true", 4, 0); |
|
|
480 | else |
476 | encode_str (enc, "false", 5, 0); |
481 | encode_str (enc, "false", 5, 0); |
477 | else |
|
|
478 | encode_str (enc, "true", 4, 0); |
|
|
479 | } |
482 | } |
480 | else |
483 | else |
481 | { |
484 | { |
482 | #if 0 |
485 | #if 0 |
483 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
486 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
… | |
… | |
490 | // 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 |
491 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
494 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
492 | |
495 | |
493 | if (to_json) |
496 | if (to_json) |
494 | { |
497 | { |
495 | dSP; |
498 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
496 | ENTER; |
|
|
497 | SAVETMPS; |
|
|
498 | PUSHMARK (SP); |
|
|
499 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
499 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
500 | |
500 | |
501 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
501 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
502 | // check anyways. |
502 | // check anyways. |
503 | PUTBACK; |
503 | PUTBACK; |
504 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
504 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
505 | SPAGAIN; |
505 | SPAGAIN; |
506 | |
506 | |
507 | encode_sv (enc, POPs); |
507 | encode_sv (enc, POPs); |
508 | |
508 | |
509 | FREETMPS; |
509 | FREETMPS; LEAVE; |
510 | LEAVE; |
|
|
511 | } |
510 | } |
512 | else if (enc->json.flags & F_ALLOW_BLESSED) |
511 | else if (enc->json.flags & F_ALLOW_BLESSED) |
513 | encode_str (enc, "null", 4, 0); |
512 | encode_str (enc, "null", 4, 0); |
514 | else |
513 | else |
515 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
514 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
… | |
… | |
526 | encode_hv (enc, (HV *)sv); |
525 | encode_hv (enc, (HV *)sv); |
527 | else if (svt == SVt_PVAV) |
526 | else if (svt == SVt_PVAV) |
528 | encode_av (enc, (AV *)sv); |
527 | encode_av (enc, (AV *)sv); |
529 | else if (svt < SVt_PVAV) |
528 | else if (svt < SVt_PVAV) |
530 | { |
529 | { |
531 | if (SvNIOK (sv) && SvIV (sv) == 0) |
530 | STRLEN len = 0; |
|
|
531 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
532 | |
|
|
533 | if (len == 1 && *pv == '1') |
|
|
534 | encode_str (enc, "true", 4, 0); |
|
|
535 | else if (len == 1 && *pv == '0') |
532 | encode_str (enc, "false", 5, 0); |
536 | encode_str (enc, "false", 5, 0); |
533 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
534 | encode_str (enc, "true", 4, 0); |
|
|
535 | else |
537 | else |
536 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
538 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
537 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
539 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
538 | } |
540 | } |
539 | else |
541 | else |
… | |
… | |
1008 | } |
1010 | } |
1009 | |
1011 | |
1010 | static SV * |
1012 | static SV * |
1011 | decode_hv (dec_t *dec) |
1013 | decode_hv (dec_t *dec) |
1012 | { |
1014 | { |
|
|
1015 | SV *sv; |
1013 | HV *hv = newHV (); |
1016 | HV *hv = newHV (); |
1014 | |
1017 | |
1015 | DEC_INC_DEPTH; |
1018 | DEC_INC_DEPTH; |
1016 | decode_ws (dec); |
1019 | decode_ws (dec); |
1017 | |
1020 | |
… | |
… | |
1091 | |
1094 | |
1092 | ++dec->cur; |
1095 | ++dec->cur; |
1093 | } |
1096 | } |
1094 | |
1097 | |
1095 | DEC_DEC_DEPTH; |
1098 | DEC_DEC_DEPTH; |
1096 | return newRV_noinc ((SV *)hv); |
1099 | sv = newRV_noinc ((SV *)hv); |
|
|
1100 | |
|
|
1101 | // check filter callbacks |
|
|
1102 | if (dec->json.flags & F_HOOK) |
|
|
1103 | { |
|
|
1104 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1105 | { |
|
|
1106 | HE *cb, *he; |
|
|
1107 | |
|
|
1108 | hv_iterinit (hv); |
|
|
1109 | he = hv_iternext (hv); |
|
|
1110 | hv_iterinit (hv); |
|
|
1111 | |
|
|
1112 | // the next line creates a mortal sv each time its called. |
|
|
1113 | // might want to optimise this for common cases. |
|
|
1114 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1115 | |
|
|
1116 | if (cb) |
|
|
1117 | { |
|
|
1118 | int count; |
|
|
1119 | ENTER; SAVETMPS; |
|
|
1120 | |
|
|
1121 | dSP; PUSHMARK (SP); |
|
|
1122 | XPUSHs (HeVAL (he)); |
|
|
1123 | |
|
|
1124 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1125 | |
|
|
1126 | if (count == 1) |
|
|
1127 | { |
|
|
1128 | sv = newSVsv (POPs); |
|
|
1129 | FREETMPS; LEAVE; |
|
|
1130 | return sv; |
|
|
1131 | } |
|
|
1132 | |
|
|
1133 | FREETMPS; LEAVE; |
|
|
1134 | } |
|
|
1135 | } |
|
|
1136 | |
|
|
1137 | if (dec->json.cb_object) |
|
|
1138 | { |
|
|
1139 | int count; |
|
|
1140 | ENTER; SAVETMPS; |
|
|
1141 | |
|
|
1142 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1143 | XPUSHs (sv_2mortal (sv)); |
|
|
1144 | |
|
|
1145 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1146 | |
|
|
1147 | if (count == 1) |
|
|
1148 | { |
|
|
1149 | sv = newSVsv (POPs); |
|
|
1150 | FREETMPS; LEAVE; |
|
|
1151 | return sv; |
|
|
1152 | } |
|
|
1153 | |
|
|
1154 | SvREFCNT_inc (sv); |
|
|
1155 | FREETMPS; LEAVE; |
|
|
1156 | } |
|
|
1157 | } |
|
|
1158 | |
|
|
1159 | return sv; |
1097 | |
1160 | |
1098 | fail: |
1161 | fail: |
1099 | SvREFCNT_dec (hv); |
1162 | SvREFCNT_dec (hv); |
1100 | DEC_DEC_DEPTH; |
1163 | DEC_DEC_DEPTH; |
1101 | return 0; |
1164 | return 0; |
… | |
… | |
1187 | dec.end = SvEND (string); |
1250 | dec.end = SvEND (string); |
1188 | dec.err = 0; |
1251 | dec.err = 0; |
1189 | dec.depth = 0; |
1252 | dec.depth = 0; |
1190 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1253 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1191 | |
1254 | |
|
|
1255 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1256 | dec.json.flags |= F_HOOK; |
|
|
1257 | |
1192 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1258 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1193 | sv = decode_sv (&dec); |
1259 | sv = decode_sv (&dec); |
1194 | |
1260 | |
1195 | if (!(offset_return || !sv)) |
1261 | if (!(offset_return || !sv)) |
1196 | { |
1262 | { |
… | |
… | |
1270 | void new (char *klass) |
1336 | void new (char *klass) |
1271 | PPCODE: |
1337 | PPCODE: |
1272 | { |
1338 | { |
1273 | SV *pv = NEWSV (0, sizeof (JSON)); |
1339 | SV *pv = NEWSV (0, sizeof (JSON)); |
1274 | SvPOK_only (pv); |
1340 | SvPOK_only (pv); |
1275 | Zero (SvPVX (pv), 1, sizeof (JSON)); |
1341 | Zero (SvPVX (pv), 1, JSON); |
1276 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1342 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1277 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
1343 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
1278 | } |
1344 | } |
1279 | |
1345 | |
1280 | void ascii (JSON *self, int enable = 1) |
1346 | void ascii (JSON *self, int enable = 1) |
… | |
… | |
1330 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1396 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1331 | |
1397 | |
1332 | XPUSHs (ST (0)); |
1398 | XPUSHs (ST (0)); |
1333 | } |
1399 | } |
1334 | |
1400 | |
|
|
1401 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
|
|
1402 | PPCODE: |
|
|
1403 | { |
|
|
1404 | SvREFCNT_dec (self->cb_object); |
|
|
1405 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1406 | |
|
|
1407 | XPUSHs (ST (0)); |
|
|
1408 | } |
|
|
1409 | |
|
|
1410 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1411 | PPCODE: |
|
|
1412 | { |
|
|
1413 | if (!self->cb_sk_object) |
|
|
1414 | self->cb_sk_object = newHV (); |
|
|
1415 | |
|
|
1416 | if (SvOK (cb)) |
|
|
1417 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1418 | else |
|
|
1419 | { |
|
|
1420 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1421 | |
|
|
1422 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1423 | { |
|
|
1424 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1425 | self->cb_sk_object = 0; |
|
|
1426 | } |
|
|
1427 | } |
|
|
1428 | |
|
|
1429 | XPUSHs (ST (0)); |
|
|
1430 | } |
|
|
1431 | |
1335 | void encode (JSON *self, SV *scalar) |
1432 | void encode (JSON *self, SV *scalar) |
1336 | PPCODE: |
1433 | PPCODE: |
1337 | XPUSHs (encode_json (scalar, self)); |
1434 | XPUSHs (encode_json (scalar, self)); |
1338 | |
1435 | |
1339 | void decode (JSON *self, SV *jsonstr) |
1436 | void decode (JSON *self, SV *jsonstr) |
… | |
… | |
1347 | EXTEND (SP, 2); |
1444 | EXTEND (SP, 2); |
1348 | PUSHs (decode_json (jsonstr, self, &offset)); |
1445 | PUSHs (decode_json (jsonstr, self, &offset)); |
1349 | PUSHs (sv_2mortal (newSVuv (offset))); |
1446 | PUSHs (sv_2mortal (newSVuv (offset))); |
1350 | } |
1447 | } |
1351 | |
1448 | |
|
|
1449 | void DESTROY (JSON *self) |
|
|
1450 | CODE: |
|
|
1451 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1452 | SvREFCNT_dec (self->cb_object); |
|
|
1453 | |
1352 | PROTOTYPES: ENABLE |
1454 | PROTOTYPES: ENABLE |
1353 | |
1455 | |
1354 | void to_json (SV *scalar) |
1456 | void to_json (SV *scalar) |
1355 | PPCODE: |
1457 | PPCODE: |
1356 | { |
1458 | { |