… | |
… | |
15 | // guarentees, though. if it breaks, you get to keep the pieces. |
15 | // guarentees, though. if it breaks, you get to keep the pieces. |
16 | #ifndef UTF8_MAXBYTES |
16 | #ifndef UTF8_MAXBYTES |
17 | # define UTF8_MAXBYTES 13 |
17 | # define UTF8_MAXBYTES 13 |
18 | #endif |
18 | #endif |
19 | |
19 | |
20 | #define F_ASCII 0x00000001UL |
20 | #define F_ASCII 0x00000001UL |
21 | #define F_LATIN1 0x00000002UL |
21 | #define F_LATIN1 0x00000002UL |
22 | #define F_UTF8 0x00000004UL |
22 | #define F_UTF8 0x00000004UL |
23 | #define F_INDENT 0x00000008UL |
23 | #define F_INDENT 0x00000008UL |
24 | #define F_CANONICAL 0x00000010UL |
24 | #define F_CANONICAL 0x00000010UL |
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 |
|
|
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
29 | #define F_MAXDEPTH 0xf8000000UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
30 | #define S_MAXDEPTH 27 |
32 | #define S_MAXDEPTH 27 |
|
|
33 | #define F_MAXSIZE 0x01f00000UL |
|
|
34 | #define S_MAXSIZE 20 |
31 | |
35 | |
32 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
33 | |
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
34 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
35 | // F_BLESSED? <=> { $__class__$ => } |
|
|
36 | |
38 | |
37 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
38 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
40 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
39 | |
41 | |
40 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
… | |
… | |
54 | #endif |
56 | #endif |
55 | |
57 | |
56 | #define expect_false(expr) expect ((expr) != 0, 0) |
58 | #define expect_false(expr) expect ((expr) != 0, 0) |
57 | #define expect_true(expr) expect ((expr) != 0, 1) |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
58 | |
60 | |
59 | static HV *json_stash; // JSON::XS:: |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
60 | static SV *json_true, *json_false; |
62 | static SV *json_true, *json_false; |
|
|
63 | |
|
|
64 | typedef struct json { |
|
|
65 | U32 flags; |
|
|
66 | } JSON__XS; |
61 | |
67 | |
62 | ///////////////////////////////////////////////////////////////////////////// |
68 | ///////////////////////////////////////////////////////////////////////////// |
63 | // utility functions |
69 | // utility functions |
64 | |
70 | |
65 | static UV * |
71 | static UV * |
… | |
… | |
114 | typedef struct |
120 | typedef struct |
115 | { |
121 | { |
116 | char *cur; // SvPVX (sv) + current output position |
122 | char *cur; // SvPVX (sv) + current output position |
117 | char *end; // SvEND (sv) |
123 | char *end; // SvEND (sv) |
118 | SV *sv; // result scalar |
124 | SV *sv; // result scalar |
119 | U32 flags; // F_* |
125 | struct json json; |
120 | U32 indent; // indentation level |
126 | U32 indent; // indentation level |
121 | U32 maxdepth; // max. indentation/recursion level |
127 | U32 maxdepth; // max. indentation/recursion level |
122 | } enc_t; |
128 | } enc_t; |
123 | |
129 | |
124 | inline void |
130 | inline void |
… | |
… | |
198 | } |
204 | } |
199 | |
205 | |
200 | if (uch > 0x10FFFFUL) |
206 | if (uch > 0x10FFFFUL) |
201 | 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); |
202 | |
208 | |
203 | 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)) |
204 | { |
210 | { |
205 | if (uch > 0xFFFFUL) |
211 | if (uch > 0xFFFFUL) |
206 | { |
212 | { |
207 | need (enc, len += 11); |
213 | need (enc, len += 11); |
208 | sprintf (enc->cur, "\\u%04x\\u%04x", |
214 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
222 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
228 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
223 | } |
229 | } |
224 | |
230 | |
225 | str += clen; |
231 | str += clen; |
226 | } |
232 | } |
227 | else if (enc->flags & F_LATIN1) |
233 | else if (enc->json.flags & F_LATIN1) |
228 | { |
234 | { |
229 | *enc->cur++ = uch; |
235 | *enc->cur++ = uch; |
230 | str += clen; |
236 | str += clen; |
231 | } |
237 | } |
232 | else if (is_utf8) |
238 | else if (is_utf8) |
… | |
… | |
253 | } |
259 | } |
254 | |
260 | |
255 | inline void |
261 | inline void |
256 | encode_indent (enc_t *enc) |
262 | encode_indent (enc_t *enc) |
257 | { |
263 | { |
258 | if (enc->flags & F_INDENT) |
264 | if (enc->json.flags & F_INDENT) |
259 | { |
265 | { |
260 | int spaces = enc->indent * INDENT_STEP; |
266 | int spaces = enc->indent * INDENT_STEP; |
261 | |
267 | |
262 | need (enc, spaces); |
268 | need (enc, spaces); |
263 | memset (enc->cur, ' ', spaces); |
269 | memset (enc->cur, ' ', spaces); |
… | |
… | |
273 | } |
279 | } |
274 | |
280 | |
275 | inline void |
281 | inline void |
276 | encode_nl (enc_t *enc) |
282 | encode_nl (enc_t *enc) |
277 | { |
283 | { |
278 | if (enc->flags & F_INDENT) |
284 | if (enc->json.flags & F_INDENT) |
279 | { |
285 | { |
280 | need (enc, 1); |
286 | need (enc, 1); |
281 | encode_ch (enc, '\n'); |
287 | encode_ch (enc, '\n'); |
282 | } |
288 | } |
283 | } |
289 | } |
… | |
… | |
285 | inline void |
291 | inline void |
286 | encode_comma (enc_t *enc) |
292 | encode_comma (enc_t *enc) |
287 | { |
293 | { |
288 | encode_ch (enc, ','); |
294 | encode_ch (enc, ','); |
289 | |
295 | |
290 | if (enc->flags & F_INDENT) |
296 | if (enc->json.flags & F_INDENT) |
291 | encode_nl (enc); |
297 | encode_nl (enc); |
292 | else if (enc->flags & F_SPACE_AFTER) |
298 | else if (enc->json.flags & F_SPACE_AFTER) |
293 | encode_space (enc); |
299 | encode_space (enc); |
294 | } |
300 | } |
295 | |
301 | |
296 | static void encode_sv (enc_t *enc, SV *sv); |
302 | static void encode_sv (enc_t *enc, SV *sv); |
297 | |
303 | |
… | |
… | |
340 | else |
346 | else |
341 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
347 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
342 | |
348 | |
343 | encode_ch (enc, '"'); |
349 | encode_ch (enc, '"'); |
344 | |
350 | |
345 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
351 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
346 | encode_ch (enc, ':'); |
352 | encode_ch (enc, ':'); |
347 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
353 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
348 | encode_sv (enc, HeVAL (he)); |
354 | encode_sv (enc, HeVAL (he)); |
349 | } |
355 | } |
350 | |
356 | |
351 | // compare hash entries, used when all keys are bytestrings |
357 | // compare hash entries, used when all keys are bytestrings |
352 | static int |
358 | static int |
… | |
… | |
387 | { |
393 | { |
388 | // for canonical output we have to sort by keys first |
394 | // for canonical output we have to sort by keys first |
389 | // actually, this is mostly due to the stupid so-called |
395 | // actually, this is mostly due to the stupid so-called |
390 | // security workaround added somewhere in 5.8.x. |
396 | // security workaround added somewhere in 5.8.x. |
391 | // that randomises hash orderings |
397 | // that randomises hash orderings |
392 | if (enc->flags & F_CANONICAL) |
398 | if (enc->json.flags & F_CANONICAL) |
393 | { |
399 | { |
394 | int fast = 1; |
400 | int fast = 1; |
395 | HE *he; |
401 | HE *he; |
396 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
402 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
397 | HE **hes = _alloca (count * sizeof (HE)); |
403 | HE **hes = _alloca (count * sizeof (HE)); |
… | |
… | |
469 | svtype svt; |
475 | svtype svt; |
470 | |
476 | |
471 | SvGETMAGIC (sv); |
477 | SvGETMAGIC (sv); |
472 | svt = SvTYPE (sv); |
478 | svt = SvTYPE (sv); |
473 | |
479 | |
|
|
480 | if (expect_false (SvOBJECT (sv))) |
|
|
481 | { |
|
|
482 | if (SvSTASH (sv) == json_boolean_stash) |
|
|
483 | { |
|
|
484 | if (SvIV (sv) == 0) |
|
|
485 | encode_str (enc, "false", 5, 0); |
|
|
486 | else |
|
|
487 | encode_str (enc, "true", 4, 0); |
|
|
488 | } |
|
|
489 | else |
|
|
490 | { |
|
|
491 | #if 0 |
|
|
492 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
493 | { |
|
|
494 | // not yet |
|
|
495 | } |
|
|
496 | #endif |
|
|
497 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
498 | { |
|
|
499 | // we re-bless the reference to get overload and other niceties right |
|
|
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
|
|
501 | |
|
|
502 | if (to_json) |
|
|
503 | { |
|
|
504 | dSP; |
|
|
505 | ENTER; |
|
|
506 | SAVETMPS; |
|
|
507 | PUSHMARK (SP); |
|
|
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
509 | |
|
|
510 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
|
|
511 | // check anyways. |
|
|
512 | PUTBACK; |
|
|
513 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
|
|
514 | SPAGAIN; |
|
|
515 | |
|
|
516 | encode_sv (enc, POPs); |
|
|
517 | |
|
|
518 | FREETMPS; |
|
|
519 | LEAVE; |
|
|
520 | } |
|
|
521 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
522 | encode_str (enc, "null", 4, 0); |
|
|
523 | else |
|
|
524 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
525 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
526 | } |
|
|
527 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
528 | encode_str (enc, "null", 4, 0); |
|
|
529 | else |
|
|
530 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
531 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
532 | } |
|
|
533 | } |
474 | if (svt == SVt_PVHV) |
534 | else if (svt == SVt_PVHV) |
475 | encode_hv (enc, (HV *)sv); |
535 | encode_hv (enc, (HV *)sv); |
476 | else if (svt == SVt_PVAV) |
536 | else if (svt == SVt_PVAV) |
477 | encode_av (enc, (AV *)sv); |
537 | encode_av (enc, (AV *)sv); |
478 | else if (svt < SVt_PVAV) |
538 | else if (svt < SVt_PVAV) |
479 | { |
539 | { |
… | |
… | |
558 | 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", |
559 | SvPV_nolen (sv), SvFLAGS (sv)); |
619 | SvPV_nolen (sv), SvFLAGS (sv)); |
560 | } |
620 | } |
561 | |
621 | |
562 | static SV * |
622 | static SV * |
563 | encode_json (SV *scalar, U32 flags) |
623 | encode_json (SV *scalar, struct json *json) |
564 | { |
624 | { |
565 | enc_t enc; |
625 | enc_t enc; |
566 | |
626 | |
567 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
627 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
568 | 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)"); |
569 | |
629 | |
570 | enc.flags = flags; |
630 | enc.json = *json; |
571 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
631 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
572 | enc.cur = SvPVX (enc.sv); |
632 | enc.cur = SvPVX (enc.sv); |
573 | enc.end = SvEND (enc.sv); |
633 | enc.end = SvEND (enc.sv); |
574 | enc.indent = 0; |
634 | enc.indent = 0; |
575 | enc.maxdepth = DEC_DEPTH (flags); |
635 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
576 | |
636 | |
577 | SvPOK_only (enc.sv); |
637 | SvPOK_only (enc.sv); |
578 | encode_sv (&enc, scalar); |
638 | encode_sv (&enc, scalar); |
579 | |
639 | |
580 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
640 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
581 | *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 |
582 | |
642 | |
583 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
643 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
584 | SvUTF8_on (enc.sv); |
644 | SvUTF8_on (enc.sv); |
585 | |
645 | |
586 | if (enc.flags & F_SHRINK) |
646 | if (enc.json.flags & F_SHRINK) |
587 | shrink (enc.sv); |
647 | shrink (enc.sv); |
588 | |
648 | |
589 | return enc.sv; |
649 | return enc.sv; |
590 | } |
650 | } |
591 | |
651 | |
… | |
… | |
596 | typedef struct |
656 | typedef struct |
597 | { |
657 | { |
598 | char *cur; // current parser pointer |
658 | char *cur; // current parser pointer |
599 | char *end; // end of input string |
659 | char *end; // end of input string |
600 | const char *err; // parse error, if != 0 |
660 | const char *err; // parse error, if != 0 |
601 | U32 flags; // F_* |
661 | struct json json; |
602 | U32 depth; // recursion depth |
662 | U32 depth; // recursion depth |
603 | U32 maxdepth; // recursion depth limit |
663 | U32 maxdepth; // recursion depth limit |
604 | } dec_t; |
664 | } dec_t; |
605 | |
665 | |
606 | inline void |
666 | inline void |
… | |
… | |
967 | if (*dec->cur == '}') |
1027 | if (*dec->cur == '}') |
968 | ++dec->cur; |
1028 | ++dec->cur; |
969 | else |
1029 | else |
970 | for (;;) |
1030 | for (;;) |
971 | { |
1031 | { |
972 | SV *key, *value; |
|
|
973 | |
|
|
974 | decode_ws (dec); EXPECT_CH ('"'); |
1032 | decode_ws (dec); EXPECT_CH ('"'); |
975 | |
1033 | |
976 | key = decode_str (dec); |
1034 | // heuristic: assume that |
977 | if (!key) |
1035 | // a) decode_str + hv_store_ent are abysmally slow. |
978 | goto fail; |
1036 | // b) most hash keys are short, simple ascii text. |
|
|
1037 | // => try to "fast-match" such strings to avoid |
|
|
1038 | // the overhead of decode_str + hv_store_ent. |
|
|
1039 | { |
|
|
1040 | SV *value; |
|
|
1041 | char *p = dec->cur; |
|
|
1042 | char *e = p + 24; // only try up to 24 bytes |
979 | |
1043 | |
980 | decode_ws (dec); EXPECT_CH (':'); |
1044 | for (;;) |
981 | |
|
|
982 | value = decode_sv (dec); |
|
|
983 | if (!value) |
|
|
984 | { |
1045 | { |
|
|
1046 | // the >= 0x80 is true on most architectures |
|
|
1047 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1048 | { |
|
|
1049 | // slow path, back up and use decode_str |
|
|
1050 | SV *key = decode_str (dec); |
|
|
1051 | if (!key) |
|
|
1052 | goto fail; |
|
|
1053 | |
|
|
1054 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1055 | |
|
|
1056 | value = decode_sv (dec); |
|
|
1057 | if (!value) |
|
|
1058 | { |
|
|
1059 | SvREFCNT_dec (key); |
|
|
1060 | goto fail; |
|
|
1061 | } |
|
|
1062 | |
|
|
1063 | hv_store_ent (hv, key, value, 0); |
985 | SvREFCNT_dec (key); |
1064 | SvREFCNT_dec (key); |
|
|
1065 | |
|
|
1066 | break; |
|
|
1067 | } |
|
|
1068 | else if (*p == '"') |
|
|
1069 | { |
|
|
1070 | // fast path, got a simple key |
|
|
1071 | char *key = dec->cur; |
|
|
1072 | int len = p - key; |
|
|
1073 | dec->cur = p + 1; |
|
|
1074 | |
|
|
1075 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1076 | |
|
|
1077 | value = decode_sv (dec); |
|
|
1078 | if (!value) |
986 | goto fail; |
1079 | goto fail; |
|
|
1080 | |
|
|
1081 | hv_store (hv, key, len, value, 0); |
|
|
1082 | |
|
|
1083 | break; |
|
|
1084 | } |
|
|
1085 | |
|
|
1086 | ++p; |
987 | } |
1087 | } |
988 | |
1088 | } |
989 | hv_store_ent (hv, key, value, 0); |
|
|
990 | SvREFCNT_dec (key); |
|
|
991 | |
1089 | |
992 | decode_ws (dec); |
1090 | decode_ws (dec); |
993 | |
1091 | |
994 | if (*dec->cur == '}') |
1092 | if (*dec->cur == '}') |
995 | { |
1093 | { |
… | |
… | |
1071 | fail: |
1169 | fail: |
1072 | return 0; |
1170 | return 0; |
1073 | } |
1171 | } |
1074 | |
1172 | |
1075 | static SV * |
1173 | static SV * |
1076 | decode_json (SV *string, U32 flags, UV *offset_return) |
1174 | decode_json (SV *string, struct json *json, UV *offset_return) |
1077 | { |
1175 | { |
1078 | dec_t dec; |
1176 | dec_t dec; |
1079 | UV offset; |
1177 | UV offset; |
1080 | SV *sv; |
1178 | SV *sv; |
1081 | |
1179 | |
1082 | SvGETMAGIC (string); |
1180 | SvGETMAGIC (string); |
1083 | SvUPGRADE (string, SVt_PV); |
1181 | SvUPGRADE (string, SVt_PV); |
1084 | |
1182 | |
|
|
1183 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
|
|
1184 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1185 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
|
|
1186 | |
1085 | if (flags & F_UTF8) |
1187 | if (json->flags & F_UTF8) |
1086 | sv_utf8_downgrade (string, 0); |
1188 | sv_utf8_downgrade (string, 0); |
1087 | else |
1189 | else |
1088 | sv_utf8_upgrade (string); |
1190 | sv_utf8_upgrade (string); |
1089 | |
1191 | |
1090 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1192 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1091 | |
1193 | |
1092 | dec.flags = flags; |
1194 | dec.json = *json; |
1093 | dec.cur = SvPVX (string); |
1195 | dec.cur = SvPVX (string); |
1094 | dec.end = SvEND (string); |
1196 | dec.end = SvEND (string); |
1095 | dec.err = 0; |
1197 | dec.err = 0; |
1096 | dec.depth = 0; |
1198 | dec.depth = 0; |
1097 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1199 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1098 | |
1200 | |
1099 | *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 |
1100 | sv = decode_sv (&dec); |
1202 | sv = decode_sv (&dec); |
1101 | |
1203 | |
1102 | if (!(offset_return || !sv)) |
1204 | if (!(offset_return || !sv)) |
… | |
… | |
1112 | } |
1214 | } |
1113 | } |
1215 | } |
1114 | |
1216 | |
1115 | if (offset_return || !sv) |
1217 | if (offset_return || !sv) |
1116 | { |
1218 | { |
1117 | offset = dec.flags & F_UTF8 |
1219 | offset = dec.json.flags & F_UTF8 |
1118 | ? dec.cur - SvPVX (string) |
1220 | ? dec.cur - SvPVX (string) |
1119 | : utf8_distance (dec.cur, SvPVX (string)); |
1221 | : utf8_distance (dec.cur, SvPVX (string)); |
1120 | |
1222 | |
1121 | if (offset_return) |
1223 | if (offset_return) |
1122 | *offset_return = offset; |
1224 | *offset_return = offset; |
… | |
… | |
1141 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1243 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1142 | } |
1244 | } |
1143 | |
1245 | |
1144 | sv = sv_2mortal (sv); |
1246 | sv = sv_2mortal (sv); |
1145 | |
1247 | |
1146 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1248 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1147 | 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)"); |
1148 | |
1250 | |
1149 | return sv; |
1251 | return sv; |
1150 | } |
1252 | } |
1151 | |
1253 | |
… | |
… | |
1163 | i >= '0' && i <= '9' ? i - '0' |
1265 | i >= '0' && i <= '9' ? i - '0' |
1164 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1266 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1165 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1267 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1166 | : -1; |
1268 | : -1; |
1167 | |
1269 | |
1168 | json_stash = gv_stashpv ("JSON::XS", 1); |
1270 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1271 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1169 | |
1272 | |
1170 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
1273 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
1171 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1274 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1172 | } |
1275 | } |
1173 | |
1276 | |
… | |
… | |
1179 | OUTPUT: |
1282 | OUTPUT: |
1180 | RETVAL |
1283 | RETVAL |
1181 | |
1284 | |
1182 | SV *ascii (SV *self, int enable = 1) |
1285 | SV *ascii (SV *self, int enable = 1) |
1183 | ALIAS: |
1286 | ALIAS: |
1184 | ascii = F_ASCII |
1287 | ascii = F_ASCII |
1185 | latin1 = F_LATIN1 |
1288 | latin1 = F_LATIN1 |
1186 | utf8 = F_UTF8 |
1289 | utf8 = F_UTF8 |
1187 | indent = F_INDENT |
1290 | indent = F_INDENT |
1188 | canonical = F_CANONICAL |
1291 | canonical = F_CANONICAL |
1189 | space_before = F_SPACE_BEFORE |
1292 | space_before = F_SPACE_BEFORE |
1190 | space_after = F_SPACE_AFTER |
1293 | space_after = F_SPACE_AFTER |
1191 | pretty = F_PRETTY |
1294 | pretty = F_PRETTY |
1192 | allow_nonref = F_ALLOW_NONREF |
1295 | allow_nonref = F_ALLOW_NONREF |
1193 | shrink = F_SHRINK |
1296 | shrink = F_SHRINK |
|
|
1297 | allow_blessed = F_ALLOW_BLESSED |
|
|
1298 | convert_blessed = F_CONV_BLESSED |
1194 | CODE: |
1299 | CODE: |
1195 | { |
1300 | { |
1196 | UV *uv = SvJSON (self); |
1301 | UV *uv = SvJSON (self); |
1197 | if (enable) |
1302 | if (enable) |
1198 | *uv |= ix; |
1303 | *uv |= ix; |
… | |
… | |
1220 | RETVAL = newSVsv (self); |
1325 | RETVAL = newSVsv (self); |
1221 | } |
1326 | } |
1222 | OUTPUT: |
1327 | OUTPUT: |
1223 | RETVAL |
1328 | RETVAL |
1224 | |
1329 | |
|
|
1330 | SV *max_size (SV *self, UV max_size = 0) |
|
|
1331 | CODE: |
|
|
1332 | { |
|
|
1333 | UV *uv = SvJSON (self); |
|
|
1334 | UV log2 = 0; |
|
|
1335 | |
|
|
1336 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1337 | if (max_size == 1) max_size = 2; |
|
|
1338 | |
|
|
1339 | while ((1UL << log2) < max_size) |
|
|
1340 | ++log2; |
|
|
1341 | |
|
|
1342 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1343 | |
|
|
1344 | RETVAL = newSVsv (self); |
|
|
1345 | } |
|
|
1346 | OUTPUT: |
|
|
1347 | RETVAL |
|
|
1348 | |
1225 | void encode (SV *self, SV *scalar) |
1349 | void encode (SV *self, SV *scalar) |
1226 | PPCODE: |
1350 | PPCODE: |
|
|
1351 | { |
|
|
1352 | struct json json = { *SvJSON (self) }; |
1227 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1353 | XPUSHs (encode_json (scalar, &json)); |
|
|
1354 | } |
1228 | |
1355 | |
1229 | void decode (SV *self, SV *jsonstr) |
1356 | void decode (SV *self, SV *jsonstr) |
1230 | PPCODE: |
1357 | PPCODE: |
|
|
1358 | { |
|
|
1359 | struct json json = { *SvJSON (self) }; |
1231 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1360 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1361 | } |
1232 | |
1362 | |
1233 | void decode_prefix (SV *self, SV *jsonstr) |
1363 | void decode_prefix (SV *self, SV *jsonstr) |
1234 | PPCODE: |
1364 | PPCODE: |
1235 | { |
1365 | { |
1236 | UV offset; |
1366 | UV offset; |
|
|
1367 | struct json json = { *SvJSON (self) }; |
1237 | EXTEND (SP, 2); |
1368 | EXTEND (SP, 2); |
1238 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1369 | PUSHs (decode_json (jsonstr, &json, &offset)); |
1239 | PUSHs (sv_2mortal (newSVuv (offset))); |
1370 | PUSHs (sv_2mortal (newSVuv (offset))); |
1240 | } |
1371 | } |
1241 | |
1372 | |
1242 | PROTOTYPES: ENABLE |
1373 | PROTOTYPES: ENABLE |
1243 | |
1374 | |
1244 | void to_json (SV *scalar) |
1375 | void to_json (SV *scalar) |
1245 | ALIAS: |
|
|
1246 | objToJson = 0 |
|
|
1247 | PPCODE: |
1376 | PPCODE: |
|
|
1377 | { |
|
|
1378 | struct json json = { F_DEFAULT | F_UTF8 }; |
1248 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1379 | XPUSHs (encode_json (scalar, &json)); |
|
|
1380 | } |
1249 | |
1381 | |
1250 | void from_json (SV *jsonstr) |
1382 | void from_json (SV *jsonstr) |
1251 | ALIAS: |
|
|
1252 | jsonToObj = 0 |
|
|
1253 | PPCODE: |
1383 | PPCODE: |
|
|
1384 | { |
|
|
1385 | struct json json = { F_DEFAULT | F_UTF8 }; |
1254 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1386 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1387 | } |
1255 | |
1388 | |