… | |
… | |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
60 | |
60 | |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
62 | static SV *json_true, *json_false; |
62 | static SV *json_true, *json_false; |
63 | |
63 | |
|
|
64 | typedef struct json { |
|
|
65 | U32 flags; |
|
|
66 | } JSON__XS; |
|
|
67 | |
64 | ///////////////////////////////////////////////////////////////////////////// |
68 | ///////////////////////////////////////////////////////////////////////////// |
65 | // utility functions |
69 | // utility functions |
66 | |
70 | |
67 | static UV * |
71 | static UV * |
68 | SvJSON (SV *sv) |
72 | SvJSON (SV *sv) |
… | |
… | |
116 | typedef struct |
120 | typedef struct |
117 | { |
121 | { |
118 | char *cur; // SvPVX (sv) + current output position |
122 | char *cur; // SvPVX (sv) + current output position |
119 | char *end; // SvEND (sv) |
123 | char *end; // SvEND (sv) |
120 | SV *sv; // result scalar |
124 | SV *sv; // result scalar |
121 | U32 flags; // F_* |
125 | struct json json; |
122 | U32 indent; // indentation level |
126 | U32 indent; // indentation level |
123 | U32 maxdepth; // max. indentation/recursion level |
127 | U32 maxdepth; // max. indentation/recursion level |
124 | } enc_t; |
128 | } enc_t; |
125 | |
129 | |
126 | inline void |
130 | inline void |
… | |
… | |
200 | } |
204 | } |
201 | |
205 | |
202 | if (uch > 0x10FFFFUL) |
206 | if (uch > 0x10FFFFUL) |
203 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
207 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
204 | |
208 | |
205 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
209 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
206 | { |
210 | { |
207 | if (uch > 0xFFFFUL) |
211 | if (uch > 0xFFFFUL) |
208 | { |
212 | { |
209 | need (enc, len += 11); |
213 | need (enc, len += 11); |
210 | sprintf (enc->cur, "\\u%04x\\u%04x", |
214 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
224 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
228 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
225 | } |
229 | } |
226 | |
230 | |
227 | str += clen; |
231 | str += clen; |
228 | } |
232 | } |
229 | else if (enc->flags & F_LATIN1) |
233 | else if (enc->json.flags & F_LATIN1) |
230 | { |
234 | { |
231 | *enc->cur++ = uch; |
235 | *enc->cur++ = uch; |
232 | str += clen; |
236 | str += clen; |
233 | } |
237 | } |
234 | else if (is_utf8) |
238 | else if (is_utf8) |
… | |
… | |
255 | } |
259 | } |
256 | |
260 | |
257 | inline void |
261 | inline void |
258 | encode_indent (enc_t *enc) |
262 | encode_indent (enc_t *enc) |
259 | { |
263 | { |
260 | if (enc->flags & F_INDENT) |
264 | if (enc->json.flags & F_INDENT) |
261 | { |
265 | { |
262 | int spaces = enc->indent * INDENT_STEP; |
266 | int spaces = enc->indent * INDENT_STEP; |
263 | |
267 | |
264 | need (enc, spaces); |
268 | need (enc, spaces); |
265 | memset (enc->cur, ' ', spaces); |
269 | memset (enc->cur, ' ', spaces); |
… | |
… | |
275 | } |
279 | } |
276 | |
280 | |
277 | inline void |
281 | inline void |
278 | encode_nl (enc_t *enc) |
282 | encode_nl (enc_t *enc) |
279 | { |
283 | { |
280 | if (enc->flags & F_INDENT) |
284 | if (enc->json.flags & F_INDENT) |
281 | { |
285 | { |
282 | need (enc, 1); |
286 | need (enc, 1); |
283 | encode_ch (enc, '\n'); |
287 | encode_ch (enc, '\n'); |
284 | } |
288 | } |
285 | } |
289 | } |
… | |
… | |
287 | inline void |
291 | inline void |
288 | encode_comma (enc_t *enc) |
292 | encode_comma (enc_t *enc) |
289 | { |
293 | { |
290 | encode_ch (enc, ','); |
294 | encode_ch (enc, ','); |
291 | |
295 | |
292 | if (enc->flags & F_INDENT) |
296 | if (enc->json.flags & F_INDENT) |
293 | encode_nl (enc); |
297 | encode_nl (enc); |
294 | else if (enc->flags & F_SPACE_AFTER) |
298 | else if (enc->json.flags & F_SPACE_AFTER) |
295 | encode_space (enc); |
299 | encode_space (enc); |
296 | } |
300 | } |
297 | |
301 | |
298 | static void encode_sv (enc_t *enc, SV *sv); |
302 | static void encode_sv (enc_t *enc, SV *sv); |
299 | |
303 | |
… | |
… | |
342 | else |
346 | else |
343 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
347 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
344 | |
348 | |
345 | encode_ch (enc, '"'); |
349 | encode_ch (enc, '"'); |
346 | |
350 | |
347 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
351 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
348 | encode_ch (enc, ':'); |
352 | encode_ch (enc, ':'); |
349 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
353 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
350 | encode_sv (enc, HeVAL (he)); |
354 | encode_sv (enc, HeVAL (he)); |
351 | } |
355 | } |
352 | |
356 | |
353 | // compare hash entries, used when all keys are bytestrings |
357 | // compare hash entries, used when all keys are bytestrings |
354 | static int |
358 | static int |
… | |
… | |
389 | { |
393 | { |
390 | // for canonical output we have to sort by keys first |
394 | // for canonical output we have to sort by keys first |
391 | // actually, this is mostly due to the stupid so-called |
395 | // actually, this is mostly due to the stupid so-called |
392 | // security workaround added somewhere in 5.8.x. |
396 | // security workaround added somewhere in 5.8.x. |
393 | // that randomises hash orderings |
397 | // that randomises hash orderings |
394 | if (enc->flags & F_CANONICAL) |
398 | if (enc->json.flags & F_CANONICAL) |
395 | { |
399 | { |
396 | int fast = 1; |
400 | int fast = 1; |
397 | HE *he; |
401 | HE *he; |
398 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
402 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
399 | HE **hes = _alloca (count * sizeof (HE)); |
403 | HE **hes = _alloca (count * sizeof (HE)); |
… | |
… | |
488 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
492 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
489 | { |
493 | { |
490 | // not yet |
494 | // not yet |
491 | } |
495 | } |
492 | #endif |
496 | #endif |
493 | if (enc->flags & F_CONV_BLESSED) |
497 | if (enc->json.flags & F_CONV_BLESSED) |
494 | { |
498 | { |
495 | // 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 |
496 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
497 | |
501 | |
498 | if (to_json) |
502 | if (to_json) |
… | |
… | |
512 | encode_sv (enc, POPs); |
516 | encode_sv (enc, POPs); |
513 | |
517 | |
514 | FREETMPS; |
518 | FREETMPS; |
515 | LEAVE; |
519 | LEAVE; |
516 | } |
520 | } |
517 | else if (enc->flags & F_ALLOW_BLESSED) |
521 | else if (enc->json.flags & F_ALLOW_BLESSED) |
518 | encode_str (enc, "null", 4, 0); |
522 | encode_str (enc, "null", 4, 0); |
519 | else |
523 | else |
520 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
524 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
521 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
525 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
522 | } |
526 | } |
523 | else if (enc->flags & F_ALLOW_BLESSED) |
527 | else if (enc->json.flags & F_ALLOW_BLESSED) |
524 | encode_str (enc, "null", 4, 0); |
528 | encode_str (enc, "null", 4, 0); |
525 | else |
529 | else |
526 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
530 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
527 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
531 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
528 | } |
532 | } |
… | |
… | |
614 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
618 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
615 | SvPV_nolen (sv), SvFLAGS (sv)); |
619 | SvPV_nolen (sv), SvFLAGS (sv)); |
616 | } |
620 | } |
617 | |
621 | |
618 | static SV * |
622 | static SV * |
619 | encode_json (SV *scalar, U32 flags) |
623 | encode_json (SV *scalar, struct json *json) |
620 | { |
624 | { |
621 | enc_t enc; |
625 | enc_t enc; |
622 | |
626 | |
623 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
627 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
624 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
628 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
625 | |
629 | |
626 | enc.flags = flags; |
630 | enc.json = *json; |
627 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
631 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
628 | enc.cur = SvPVX (enc.sv); |
632 | enc.cur = SvPVX (enc.sv); |
629 | enc.end = SvEND (enc.sv); |
633 | enc.end = SvEND (enc.sv); |
630 | enc.indent = 0; |
634 | enc.indent = 0; |
631 | enc.maxdepth = DEC_DEPTH (flags); |
635 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
632 | |
636 | |
633 | SvPOK_only (enc.sv); |
637 | SvPOK_only (enc.sv); |
634 | encode_sv (&enc, scalar); |
638 | encode_sv (&enc, scalar); |
635 | |
639 | |
636 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
640 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
637 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
641 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
638 | |
642 | |
639 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
643 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
640 | SvUTF8_on (enc.sv); |
644 | SvUTF8_on (enc.sv); |
641 | |
645 | |
642 | if (enc.flags & F_SHRINK) |
646 | if (enc.json.flags & F_SHRINK) |
643 | shrink (enc.sv); |
647 | shrink (enc.sv); |
644 | |
648 | |
645 | return enc.sv; |
649 | return enc.sv; |
646 | } |
650 | } |
647 | |
651 | |
… | |
… | |
652 | typedef struct |
656 | typedef struct |
653 | { |
657 | { |
654 | char *cur; // current parser pointer |
658 | char *cur; // current parser pointer |
655 | char *end; // end of input string |
659 | char *end; // end of input string |
656 | const char *err; // parse error, if != 0 |
660 | const char *err; // parse error, if != 0 |
657 | U32 flags; // F_* |
661 | struct json json; |
658 | U32 depth; // recursion depth |
662 | U32 depth; // recursion depth |
659 | U32 maxdepth; // recursion depth limit |
663 | U32 maxdepth; // recursion depth limit |
660 | } dec_t; |
664 | } dec_t; |
661 | |
665 | |
662 | inline void |
666 | inline void |
… | |
… | |
1026 | for (;;) |
1030 | for (;;) |
1027 | { |
1031 | { |
1028 | decode_ws (dec); EXPECT_CH ('"'); |
1032 | decode_ws (dec); EXPECT_CH ('"'); |
1029 | |
1033 | |
1030 | // heuristic: assume that |
1034 | // heuristic: assume that |
1031 | // a) decode_str + hv_store_ent are abysmally slow |
1035 | // a) decode_str + hv_store_ent are abysmally slow. |
1032 | // b) most hash keys are short, simple ascii text |
1036 | // b) most hash keys are short, simple ascii text. |
1033 | // so try to "fast-match" such strings to avoid |
1037 | // => try to "fast-match" such strings to avoid |
1034 | // the overhead of hv_store_ent. |
1038 | // the overhead of decode_str + hv_store_ent. |
1035 | { |
1039 | { |
1036 | SV *value; |
1040 | SV *value; |
1037 | char *p = dec->cur; |
1041 | char *p = dec->cur; |
1038 | char *e = p + 24; // only try up to 24 bytes |
1042 | char *e = p + 24; // only try up to 24 bytes |
1039 | |
1043 | |
1040 | for (;;) |
1044 | for (;;) |
1041 | { |
1045 | { |
|
|
1046 | // the >= 0x80 is true on most architectures |
1042 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1047 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1043 | { |
1048 | { |
1044 | // slow path, back up and use decode_str |
1049 | // slow path, back up and use decode_str |
1045 | SV *key = decode_str (dec); |
1050 | SV *key = decode_str (dec); |
1046 | if (!key) |
1051 | if (!key) |
… | |
… | |
1164 | fail: |
1169 | fail: |
1165 | return 0; |
1170 | return 0; |
1166 | } |
1171 | } |
1167 | |
1172 | |
1168 | static SV * |
1173 | static SV * |
1169 | decode_json (SV *string, U32 flags, UV *offset_return) |
1174 | decode_json (SV *string, struct json *json, UV *offset_return) |
1170 | { |
1175 | { |
1171 | dec_t dec; |
1176 | dec_t dec; |
1172 | UV offset; |
1177 | UV offset; |
1173 | SV *sv; |
1178 | SV *sv; |
1174 | |
1179 | |
1175 | SvGETMAGIC (string); |
1180 | SvGETMAGIC (string); |
1176 | SvUPGRADE (string, SVt_PV); |
1181 | SvUPGRADE (string, SVt_PV); |
1177 | |
1182 | |
1178 | if (flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (flags)) |
1183 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
1179 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1184 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1180 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (flags)); |
1185 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
1181 | |
1186 | |
1182 | if (flags & F_UTF8) |
1187 | if (json->flags & F_UTF8) |
1183 | sv_utf8_downgrade (string, 0); |
1188 | sv_utf8_downgrade (string, 0); |
1184 | else |
1189 | else |
1185 | sv_utf8_upgrade (string); |
1190 | sv_utf8_upgrade (string); |
1186 | |
1191 | |
1187 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1192 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1188 | |
1193 | |
1189 | dec.flags = flags; |
1194 | dec.json = *json; |
1190 | dec.cur = SvPVX (string); |
1195 | dec.cur = SvPVX (string); |
1191 | dec.end = SvEND (string); |
1196 | dec.end = SvEND (string); |
1192 | dec.err = 0; |
1197 | dec.err = 0; |
1193 | dec.depth = 0; |
1198 | dec.depth = 0; |
1194 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1199 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1195 | |
1200 | |
1196 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1201 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1197 | sv = decode_sv (&dec); |
1202 | sv = decode_sv (&dec); |
1198 | |
1203 | |
1199 | if (!(offset_return || !sv)) |
1204 | if (!(offset_return || !sv)) |
… | |
… | |
1209 | } |
1214 | } |
1210 | } |
1215 | } |
1211 | |
1216 | |
1212 | if (offset_return || !sv) |
1217 | if (offset_return || !sv) |
1213 | { |
1218 | { |
1214 | offset = dec.flags & F_UTF8 |
1219 | offset = dec.json.flags & F_UTF8 |
1215 | ? dec.cur - SvPVX (string) |
1220 | ? dec.cur - SvPVX (string) |
1216 | : utf8_distance (dec.cur, SvPVX (string)); |
1221 | : utf8_distance (dec.cur, SvPVX (string)); |
1217 | |
1222 | |
1218 | if (offset_return) |
1223 | if (offset_return) |
1219 | *offset_return = offset; |
1224 | *offset_return = offset; |
… | |
… | |
1238 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1243 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1239 | } |
1244 | } |
1240 | |
1245 | |
1241 | sv = sv_2mortal (sv); |
1246 | sv = sv_2mortal (sv); |
1242 | |
1247 | |
1243 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1248 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1244 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1249 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1245 | |
1250 | |
1246 | return sv; |
1251 | return sv; |
1247 | } |
1252 | } |
1248 | |
1253 | |
… | |
… | |
1341 | OUTPUT: |
1346 | OUTPUT: |
1342 | RETVAL |
1347 | RETVAL |
1343 | |
1348 | |
1344 | void encode (SV *self, SV *scalar) |
1349 | void encode (SV *self, SV *scalar) |
1345 | PPCODE: |
1350 | PPCODE: |
|
|
1351 | { |
|
|
1352 | struct json json = { *SvJSON (self) }; |
1346 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1353 | XPUSHs (encode_json (scalar, &json)); |
|
|
1354 | } |
1347 | |
1355 | |
1348 | void decode (SV *self, SV *jsonstr) |
1356 | void decode (SV *self, SV *jsonstr) |
1349 | PPCODE: |
1357 | PPCODE: |
|
|
1358 | { |
|
|
1359 | struct json json = { *SvJSON (self) }; |
1350 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1360 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1361 | } |
1351 | |
1362 | |
1352 | void decode_prefix (SV *self, SV *jsonstr) |
1363 | void decode_prefix (SV *self, SV *jsonstr) |
1353 | PPCODE: |
1364 | PPCODE: |
1354 | { |
1365 | { |
1355 | UV offset; |
1366 | UV offset; |
|
|
1367 | struct json json = { *SvJSON (self) }; |
1356 | EXTEND (SP, 2); |
1368 | EXTEND (SP, 2); |
1357 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1369 | PUSHs (decode_json (jsonstr, &json, &offset)); |
1358 | PUSHs (sv_2mortal (newSVuv (offset))); |
1370 | PUSHs (sv_2mortal (newSVuv (offset))); |
1359 | } |
1371 | } |
1360 | |
1372 | |
1361 | PROTOTYPES: ENABLE |
1373 | PROTOTYPES: ENABLE |
1362 | |
1374 | |
1363 | void to_json (SV *scalar) |
1375 | void to_json (SV *scalar) |
1364 | ALIAS: |
|
|
1365 | objToJson = 0 |
|
|
1366 | PPCODE: |
1376 | PPCODE: |
|
|
1377 | { |
|
|
1378 | struct json json = { F_DEFAULT | F_UTF8 }; |
1367 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1379 | XPUSHs (encode_json (scalar, &json)); |
|
|
1380 | } |
1368 | |
1381 | |
1369 | void from_json (SV *jsonstr) |
1382 | void from_json (SV *jsonstr) |
1370 | ALIAS: |
|
|
1371 | jsonToObj = 0 |
|
|
1372 | PPCODE: |
1383 | PPCODE: |
|
|
1384 | { |
|
|
1385 | struct json json = { F_DEFAULT | F_UTF8 }; |
1373 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1386 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1387 | } |
1374 | |
1388 | |