… | |
… | |
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 |
… | |
… | |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
60 | #define expect_true(expr) expect ((expr) != 0, 1) |
60 | |
61 | |
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 json { |
65 | typedef struct { |
65 | U32 flags; |
66 | U32 flags; |
|
|
67 | SV *cb_object, *cb_sk_object; |
66 | } JSON__XS; |
68 | } JSON; |
67 | |
69 | |
68 | ///////////////////////////////////////////////////////////////////////////// |
70 | ///////////////////////////////////////////////////////////////////////////// |
69 | // utility functions |
71 | // utility functions |
70 | |
72 | |
71 | static UV * |
73 | inline void |
72 | SvJSON (SV *sv) |
|
|
73 | { |
|
|
74 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
75 | croak ("object is not of type JSON::XS"); |
|
|
76 | |
|
|
77 | return &SvUVX (SvRV (sv)); |
|
|
78 | } |
|
|
79 | |
|
|
80 | static void |
|
|
81 | shrink (SV *sv) |
74 | shrink (SV *sv) |
82 | { |
75 | { |
83 | sv_utf8_downgrade (sv, 1); |
76 | sv_utf8_downgrade (sv, 1); |
84 | if (SvLEN (sv) > SvCUR (sv) + 1) |
77 | if (SvLEN (sv) > SvCUR (sv) + 1) |
85 | { |
78 | { |
… | |
… | |
120 | typedef struct |
113 | typedef struct |
121 | { |
114 | { |
122 | char *cur; // SvPVX (sv) + current output position |
115 | char *cur; // SvPVX (sv) + current output position |
123 | char *end; // SvEND (sv) |
116 | char *end; // SvEND (sv) |
124 | SV *sv; // result scalar |
117 | SV *sv; // result scalar |
125 | struct json json; |
118 | JSON json; |
126 | U32 indent; // indentation level |
119 | U32 indent; // indentation level |
127 | U32 maxdepth; // max. indentation/recursion level |
120 | U32 maxdepth; // max. indentation/recursion level |
128 | } enc_t; |
121 | } enc_t; |
129 | |
122 | |
130 | inline void |
123 | inline void |
… | |
… | |
499 | // we re-bless the reference to get overload and other niceties right |
492 | // we re-bless the reference to get overload and other niceties right |
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
493 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
501 | |
494 | |
502 | if (to_json) |
495 | if (to_json) |
503 | { |
496 | { |
504 | dSP; |
497 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
505 | ENTER; |
|
|
506 | SAVETMPS; |
|
|
507 | PUSHMARK (SP); |
|
|
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
498 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
509 | |
499 | |
510 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
500 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
511 | // check anyways. |
501 | // check anyways. |
512 | PUTBACK; |
502 | PUTBACK; |
513 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
503 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
514 | SPAGAIN; |
504 | SPAGAIN; |
515 | |
505 | |
516 | encode_sv (enc, POPs); |
506 | encode_sv (enc, POPs); |
517 | |
507 | |
518 | FREETMPS; |
508 | FREETMPS; LEAVE; |
519 | LEAVE; |
|
|
520 | } |
509 | } |
521 | else if (enc->json.flags & F_ALLOW_BLESSED) |
510 | else if (enc->json.flags & F_ALLOW_BLESSED) |
522 | encode_str (enc, "null", 4, 0); |
511 | encode_str (enc, "null", 4, 0); |
523 | else |
512 | else |
524 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
513 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
… | |
… | |
618 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
607 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
619 | SvPV_nolen (sv), SvFLAGS (sv)); |
608 | SvPV_nolen (sv), SvFLAGS (sv)); |
620 | } |
609 | } |
621 | |
610 | |
622 | static SV * |
611 | static SV * |
623 | encode_json (SV *scalar, struct json *json) |
612 | encode_json (SV *scalar, JSON *json) |
624 | { |
613 | { |
625 | enc_t enc; |
614 | enc_t enc; |
626 | |
615 | |
627 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
616 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
628 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
617 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
… | |
… | |
656 | typedef struct |
645 | typedef struct |
657 | { |
646 | { |
658 | char *cur; // current parser pointer |
647 | char *cur; // current parser pointer |
659 | char *end; // end of input string |
648 | char *end; // end of input string |
660 | const char *err; // parse error, if != 0 |
649 | const char *err; // parse error, if != 0 |
661 | struct json json; |
650 | JSON json; |
662 | U32 depth; // recursion depth |
651 | U32 depth; // recursion depth |
663 | U32 maxdepth; // recursion depth limit |
652 | U32 maxdepth; // recursion depth limit |
664 | } dec_t; |
653 | } dec_t; |
665 | |
654 | |
666 | inline void |
655 | inline void |
… | |
… | |
1017 | } |
1006 | } |
1018 | |
1007 | |
1019 | static SV * |
1008 | static SV * |
1020 | decode_hv (dec_t *dec) |
1009 | decode_hv (dec_t *dec) |
1021 | { |
1010 | { |
|
|
1011 | SV *sv; |
1022 | HV *hv = newHV (); |
1012 | HV *hv = newHV (); |
1023 | |
1013 | |
1024 | DEC_INC_DEPTH; |
1014 | DEC_INC_DEPTH; |
1025 | decode_ws (dec); |
1015 | decode_ws (dec); |
1026 | |
1016 | |
… | |
… | |
1100 | |
1090 | |
1101 | ++dec->cur; |
1091 | ++dec->cur; |
1102 | } |
1092 | } |
1103 | |
1093 | |
1104 | DEC_DEC_DEPTH; |
1094 | DEC_DEC_DEPTH; |
|
|
1095 | sv = newRV_noinc ((SV *)hv); |
|
|
1096 | |
|
|
1097 | // check filter callbacks |
|
|
1098 | if (dec->json.flags & F_HOOK) |
|
|
1099 | { |
|
|
1100 | ENTER; SAVETMPS; |
|
|
1101 | |
|
|
1102 | if (HvKEYS (hv) == 1 && dec->json.cb_sk_object) |
|
|
1103 | { |
|
|
1104 | int count; |
|
|
1105 | |
|
|
1106 | dSP; PUSHMARK (SP); |
|
|
1107 | XPUSHs (sv_2mortal (sv)); |
|
|
1108 | |
|
|
1109 | PUTBACK; count = call_sv (dec->json.cb_sk_object, G_ARRAY); SPAGAIN; |
|
|
1110 | |
|
|
1111 | if (count == 1) |
|
|
1112 | sv = newSVsv (POPs); |
|
|
1113 | else |
|
|
1114 | SvREFCNT_inc (sv); |
|
|
1115 | } |
|
|
1116 | |
|
|
1117 | if (dec->json.cb_object) |
|
|
1118 | { |
|
|
1119 | int count; |
|
|
1120 | |
|
|
1121 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1122 | XPUSHs (sv_2mortal (sv)); |
|
|
1123 | |
|
|
1124 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1125 | |
|
|
1126 | if (count == 1) |
|
|
1127 | sv = newSVsv (POPs); |
|
|
1128 | else |
|
|
1129 | SvREFCNT_inc (sv); |
|
|
1130 | } |
|
|
1131 | |
|
|
1132 | FREETMPS; LEAVE; |
|
|
1133 | } |
|
|
1134 | |
1105 | return newRV_noinc ((SV *)hv); |
1135 | return newRV_noinc ((SV *)hv); |
1106 | |
1136 | |
1107 | fail: |
1137 | fail: |
1108 | SvREFCNT_dec (hv); |
1138 | SvREFCNT_dec (hv); |
1109 | DEC_DEC_DEPTH; |
1139 | DEC_DEC_DEPTH; |
… | |
… | |
1169 | fail: |
1199 | fail: |
1170 | return 0; |
1200 | return 0; |
1171 | } |
1201 | } |
1172 | |
1202 | |
1173 | static SV * |
1203 | static SV * |
1174 | decode_json (SV *string, struct json *json, UV *offset_return) |
1204 | decode_json (SV *string, JSON *json, UV *offset_return) |
1175 | { |
1205 | { |
1176 | dec_t dec; |
1206 | dec_t dec; |
1177 | UV offset; |
1207 | UV offset; |
1178 | SV *sv; |
1208 | SV *sv; |
1179 | |
1209 | |
… | |
… | |
1195 | dec.cur = SvPVX (string); |
1225 | dec.cur = SvPVX (string); |
1196 | dec.end = SvEND (string); |
1226 | dec.end = SvEND (string); |
1197 | dec.err = 0; |
1227 | dec.err = 0; |
1198 | dec.depth = 0; |
1228 | dec.depth = 0; |
1199 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1229 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
|
|
1230 | |
|
|
1231 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1232 | dec.json.flags |= F_HOOK; |
1200 | |
1233 | |
1201 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1234 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1202 | sv = decode_sv (&dec); |
1235 | sv = decode_sv (&dec); |
1203 | |
1236 | |
1204 | if (!(offset_return || !sv)) |
1237 | if (!(offset_return || !sv)) |
… | |
… | |
1274 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1307 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1275 | } |
1308 | } |
1276 | |
1309 | |
1277 | PROTOTYPES: DISABLE |
1310 | PROTOTYPES: DISABLE |
1278 | |
1311 | |
1279 | SV *new (char *dummy) |
1312 | void new (char *klass) |
1280 | CODE: |
1313 | PPCODE: |
1281 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1314 | { |
1282 | OUTPUT: |
1315 | SV *pv = NEWSV (0, sizeof (JSON)); |
1283 | RETVAL |
1316 | SvPOK_only (pv); |
|
|
1317 | Zero (SvPVX (pv), 1, JSON); |
|
|
1318 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1319 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
|
|
1320 | } |
1284 | |
1321 | |
1285 | SV *ascii (SV *self, int enable = 1) |
1322 | void ascii (JSON *self, int enable = 1) |
1286 | ALIAS: |
1323 | ALIAS: |
1287 | ascii = F_ASCII |
1324 | ascii = F_ASCII |
1288 | latin1 = F_LATIN1 |
1325 | latin1 = F_LATIN1 |
1289 | utf8 = F_UTF8 |
1326 | utf8 = F_UTF8 |
1290 | indent = F_INDENT |
1327 | indent = F_INDENT |
… | |
… | |
1294 | pretty = F_PRETTY |
1331 | pretty = F_PRETTY |
1295 | allow_nonref = F_ALLOW_NONREF |
1332 | allow_nonref = F_ALLOW_NONREF |
1296 | shrink = F_SHRINK |
1333 | shrink = F_SHRINK |
1297 | allow_blessed = F_ALLOW_BLESSED |
1334 | allow_blessed = F_ALLOW_BLESSED |
1298 | convert_blessed = F_CONV_BLESSED |
1335 | convert_blessed = F_CONV_BLESSED |
1299 | CODE: |
1336 | PPCODE: |
1300 | { |
1337 | { |
1301 | UV *uv = SvJSON (self); |
|
|
1302 | if (enable) |
1338 | if (enable) |
1303 | *uv |= ix; |
1339 | self->flags |= ix; |
1304 | else |
1340 | else |
1305 | *uv &= ~ix; |
1341 | self->flags &= ~ix; |
1306 | |
1342 | |
1307 | RETVAL = newSVsv (self); |
1343 | XPUSHs (ST (0)); |
1308 | } |
1344 | } |
1309 | OUTPUT: |
|
|
1310 | RETVAL |
|
|
1311 | |
1345 | |
1312 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1346 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1313 | CODE: |
1347 | PPCODE: |
1314 | { |
1348 | { |
1315 | UV *uv = SvJSON (self); |
|
|
1316 | UV log2 = 0; |
1349 | UV log2 = 0; |
1317 | |
1350 | |
1318 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1351 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1319 | |
1352 | |
1320 | while ((1UL << log2) < max_depth) |
1353 | while ((1UL << log2) < max_depth) |
1321 | ++log2; |
1354 | ++log2; |
1322 | |
1355 | |
1323 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1356 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1324 | |
1357 | |
1325 | RETVAL = newSVsv (self); |
1358 | XPUSHs (ST (0)); |
1326 | } |
1359 | } |
1327 | OUTPUT: |
|
|
1328 | RETVAL |
|
|
1329 | |
1360 | |
1330 | SV *max_size (SV *self, UV max_size = 0) |
1361 | void max_size (JSON *self, UV max_size = 0) |
1331 | CODE: |
1362 | PPCODE: |
1332 | { |
1363 | { |
1333 | UV *uv = SvJSON (self); |
|
|
1334 | UV log2 = 0; |
1364 | UV log2 = 0; |
1335 | |
1365 | |
1336 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1366 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1337 | if (max_size == 1) max_size = 2; |
1367 | if (max_size == 1) max_size = 2; |
1338 | |
1368 | |
1339 | while ((1UL << log2) < max_size) |
1369 | while ((1UL << log2) < max_size) |
1340 | ++log2; |
1370 | ++log2; |
1341 | |
1371 | |
1342 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1372 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1343 | |
1373 | |
1344 | RETVAL = newSVsv (self); |
1374 | XPUSHs (ST (0)); |
1345 | } |
1375 | } |
1346 | OUTPUT: |
|
|
1347 | RETVAL |
|
|
1348 | |
1376 | |
1349 | void encode (SV *self, SV *scalar) |
1377 | void filter_json_objects (JSON *self, SV *cb = &PL_sv_undef) |
|
|
1378 | ALIAS: |
|
|
1379 | filter_sk_json_objects = 1 |
1350 | PPCODE: |
1380 | PPCODE: |
1351 | { |
1381 | { |
1352 | struct json json = { *SvJSON (self) }; |
1382 | if (!SvOK (cb)) |
1353 | XPUSHs (encode_json (scalar, &json)); |
1383 | cb = 0; |
1354 | } |
|
|
1355 | |
1384 | |
|
|
1385 | switch (ix) |
|
|
1386 | { |
|
|
1387 | case 0: self->cb_object = cb; break; |
|
|
1388 | case 1: self->cb_sk_object = cb; break; |
|
|
1389 | } |
|
|
1390 | |
|
|
1391 | XPUSHs (ST (0)); |
|
|
1392 | } |
|
|
1393 | |
1356 | void decode (SV *self, SV *jsonstr) |
1394 | void encode (JSON *self, SV *scalar) |
1357 | PPCODE: |
1395 | PPCODE: |
1358 | { |
|
|
1359 | struct json json = { *SvJSON (self) }; |
|
|
1360 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1396 | XPUSHs (encode_json (scalar, self)); |
1361 | } |
|
|
1362 | |
1397 | |
1363 | void decode_prefix (SV *self, SV *jsonstr) |
1398 | void decode (JSON *self, SV *jsonstr) |
1364 | PPCODE: |
1399 | PPCODE: |
|
|
1400 | XPUSHs (decode_json (jsonstr, self, 0)); |
|
|
1401 | |
|
|
1402 | void decode_prefix (JSON *self, SV *jsonstr) |
|
|
1403 | PPCODE: |
1365 | { |
1404 | { |
1366 | UV offset; |
1405 | UV offset; |
1367 | struct json json = { *SvJSON (self) }; |
|
|
1368 | EXTEND (SP, 2); |
1406 | EXTEND (SP, 2); |
1369 | PUSHs (decode_json (jsonstr, &json, &offset)); |
1407 | PUSHs (decode_json (jsonstr, self, &offset)); |
1370 | PUSHs (sv_2mortal (newSVuv (offset))); |
1408 | PUSHs (sv_2mortal (newSVuv (offset))); |
1371 | } |
1409 | } |
1372 | |
1410 | |
1373 | PROTOTYPES: ENABLE |
1411 | PROTOTYPES: ENABLE |
1374 | |
1412 | |
1375 | void to_json (SV *scalar) |
1413 | void to_json (SV *scalar) |
1376 | PPCODE: |
1414 | PPCODE: |
1377 | { |
1415 | { |
1378 | struct json json = { F_DEFAULT | F_UTF8 }; |
1416 | JSON json = { F_DEFAULT | F_UTF8 }; |
1379 | XPUSHs (encode_json (scalar, &json)); |
1417 | XPUSHs (encode_json (scalar, &json)); |
1380 | } |
1418 | } |
1381 | |
1419 | |
1382 | void from_json (SV *jsonstr) |
1420 | void from_json (SV *jsonstr) |
1383 | PPCODE: |
1421 | PPCODE: |
1384 | { |
1422 | { |
1385 | struct json json = { F_DEFAULT | F_UTF8 }; |
1423 | JSON json = { F_DEFAULT | F_UTF8 }; |
1386 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1424 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1387 | } |
1425 | } |
1388 | |
1426 | |