… | |
… | |
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 { |
|
|
65 | U32 flags; |
|
|
66 | } JSON; |
61 | |
67 | |
62 | ///////////////////////////////////////////////////////////////////////////// |
68 | ///////////////////////////////////////////////////////////////////////////// |
63 | // utility functions |
69 | // utility functions |
64 | |
70 | |
65 | static UV * |
71 | inline void |
66 | SvJSON (SV *sv) |
|
|
67 | { |
|
|
68 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
69 | croak ("object is not of type JSON::XS"); |
|
|
70 | |
|
|
71 | return &SvUVX (SvRV (sv)); |
|
|
72 | } |
|
|
73 | |
|
|
74 | static void |
|
|
75 | shrink (SV *sv) |
72 | shrink (SV *sv) |
76 | { |
73 | { |
77 | sv_utf8_downgrade (sv, 1); |
74 | sv_utf8_downgrade (sv, 1); |
78 | if (SvLEN (sv) > SvCUR (sv) + 1) |
75 | if (SvLEN (sv) > SvCUR (sv) + 1) |
79 | { |
76 | { |
… | |
… | |
114 | typedef struct |
111 | typedef struct |
115 | { |
112 | { |
116 | char *cur; // SvPVX (sv) + current output position |
113 | char *cur; // SvPVX (sv) + current output position |
117 | char *end; // SvEND (sv) |
114 | char *end; // SvEND (sv) |
118 | SV *sv; // result scalar |
115 | SV *sv; // result scalar |
119 | U32 flags; // F_* |
116 | JSON json; |
120 | U32 indent; // indentation level |
117 | U32 indent; // indentation level |
121 | U32 maxdepth; // max. indentation/recursion level |
118 | U32 maxdepth; // max. indentation/recursion level |
122 | } enc_t; |
119 | } enc_t; |
123 | |
120 | |
124 | inline void |
121 | inline void |
… | |
… | |
198 | } |
195 | } |
199 | |
196 | |
200 | if (uch > 0x10FFFFUL) |
197 | if (uch > 0x10FFFFUL) |
201 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
198 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
202 | |
199 | |
203 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
200 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
204 | { |
201 | { |
205 | if (uch > 0xFFFFUL) |
202 | if (uch > 0xFFFFUL) |
206 | { |
203 | { |
207 | need (enc, len += 11); |
204 | need (enc, len += 11); |
208 | sprintf (enc->cur, "\\u%04x\\u%04x", |
205 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
222 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
219 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
223 | } |
220 | } |
224 | |
221 | |
225 | str += clen; |
222 | str += clen; |
226 | } |
223 | } |
227 | else if (enc->flags & F_LATIN1) |
224 | else if (enc->json.flags & F_LATIN1) |
228 | { |
225 | { |
229 | *enc->cur++ = uch; |
226 | *enc->cur++ = uch; |
230 | str += clen; |
227 | str += clen; |
231 | } |
228 | } |
232 | else if (is_utf8) |
229 | else if (is_utf8) |
… | |
… | |
253 | } |
250 | } |
254 | |
251 | |
255 | inline void |
252 | inline void |
256 | encode_indent (enc_t *enc) |
253 | encode_indent (enc_t *enc) |
257 | { |
254 | { |
258 | if (enc->flags & F_INDENT) |
255 | if (enc->json.flags & F_INDENT) |
259 | { |
256 | { |
260 | int spaces = enc->indent * INDENT_STEP; |
257 | int spaces = enc->indent * INDENT_STEP; |
261 | |
258 | |
262 | need (enc, spaces); |
259 | need (enc, spaces); |
263 | memset (enc->cur, ' ', spaces); |
260 | memset (enc->cur, ' ', spaces); |
… | |
… | |
273 | } |
270 | } |
274 | |
271 | |
275 | inline void |
272 | inline void |
276 | encode_nl (enc_t *enc) |
273 | encode_nl (enc_t *enc) |
277 | { |
274 | { |
278 | if (enc->flags & F_INDENT) |
275 | if (enc->json.flags & F_INDENT) |
279 | { |
276 | { |
280 | need (enc, 1); |
277 | need (enc, 1); |
281 | encode_ch (enc, '\n'); |
278 | encode_ch (enc, '\n'); |
282 | } |
279 | } |
283 | } |
280 | } |
… | |
… | |
285 | inline void |
282 | inline void |
286 | encode_comma (enc_t *enc) |
283 | encode_comma (enc_t *enc) |
287 | { |
284 | { |
288 | encode_ch (enc, ','); |
285 | encode_ch (enc, ','); |
289 | |
286 | |
290 | if (enc->flags & F_INDENT) |
287 | if (enc->json.flags & F_INDENT) |
291 | encode_nl (enc); |
288 | encode_nl (enc); |
292 | else if (enc->flags & F_SPACE_AFTER) |
289 | else if (enc->json.flags & F_SPACE_AFTER) |
293 | encode_space (enc); |
290 | encode_space (enc); |
294 | } |
291 | } |
295 | |
292 | |
296 | static void encode_sv (enc_t *enc, SV *sv); |
293 | static void encode_sv (enc_t *enc, SV *sv); |
297 | |
294 | |
… | |
… | |
340 | else |
337 | else |
341 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
338 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
342 | |
339 | |
343 | encode_ch (enc, '"'); |
340 | encode_ch (enc, '"'); |
344 | |
341 | |
345 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
342 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
346 | encode_ch (enc, ':'); |
343 | encode_ch (enc, ':'); |
347 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
344 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
348 | encode_sv (enc, HeVAL (he)); |
345 | encode_sv (enc, HeVAL (he)); |
349 | } |
346 | } |
350 | |
347 | |
351 | // compare hash entries, used when all keys are bytestrings |
348 | // compare hash entries, used when all keys are bytestrings |
352 | static int |
349 | static int |
… | |
… | |
387 | { |
384 | { |
388 | // for canonical output we have to sort by keys first |
385 | // for canonical output we have to sort by keys first |
389 | // actually, this is mostly due to the stupid so-called |
386 | // actually, this is mostly due to the stupid so-called |
390 | // security workaround added somewhere in 5.8.x. |
387 | // security workaround added somewhere in 5.8.x. |
391 | // that randomises hash orderings |
388 | // that randomises hash orderings |
392 | if (enc->flags & F_CANONICAL) |
389 | if (enc->json.flags & F_CANONICAL) |
393 | { |
390 | { |
394 | int fast = 1; |
391 | int fast = 1; |
395 | HE *he; |
392 | HE *he; |
396 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
393 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
397 | HE **hes = _alloca (count * sizeof (HE)); |
394 | HE **hes = _alloca (count * sizeof (HE)); |
… | |
… | |
469 | svtype svt; |
466 | svtype svt; |
470 | |
467 | |
471 | SvGETMAGIC (sv); |
468 | SvGETMAGIC (sv); |
472 | svt = SvTYPE (sv); |
469 | svt = SvTYPE (sv); |
473 | |
470 | |
|
|
471 | if (expect_false (SvOBJECT (sv))) |
|
|
472 | { |
|
|
473 | if (SvSTASH (sv) == json_boolean_stash) |
|
|
474 | { |
|
|
475 | if (SvIV (sv) == 0) |
|
|
476 | encode_str (enc, "false", 5, 0); |
|
|
477 | else |
|
|
478 | encode_str (enc, "true", 4, 0); |
|
|
479 | } |
|
|
480 | else |
|
|
481 | { |
|
|
482 | #if 0 |
|
|
483 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
484 | { |
|
|
485 | // not yet |
|
|
486 | } |
|
|
487 | #endif |
|
|
488 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
489 | { |
|
|
490 | // we re-bless the reference to get overload and other niceties right |
|
|
491 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
|
|
492 | |
|
|
493 | if (to_json) |
|
|
494 | { |
|
|
495 | dSP; |
|
|
496 | ENTER; |
|
|
497 | SAVETMPS; |
|
|
498 | PUSHMARK (SP); |
|
|
499 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
500 | |
|
|
501 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
|
|
502 | // check anyways. |
|
|
503 | PUTBACK; |
|
|
504 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
|
|
505 | SPAGAIN; |
|
|
506 | |
|
|
507 | encode_sv (enc, POPs); |
|
|
508 | |
|
|
509 | FREETMPS; |
|
|
510 | LEAVE; |
|
|
511 | } |
|
|
512 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
513 | encode_str (enc, "null", 4, 0); |
|
|
514 | else |
|
|
515 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
516 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
517 | } |
|
|
518 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
519 | encode_str (enc, "null", 4, 0); |
|
|
520 | else |
|
|
521 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
522 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
523 | } |
|
|
524 | } |
474 | if (svt == SVt_PVHV) |
525 | else if (svt == SVt_PVHV) |
475 | encode_hv (enc, (HV *)sv); |
526 | encode_hv (enc, (HV *)sv); |
476 | else if (svt == SVt_PVAV) |
527 | else if (svt == SVt_PVAV) |
477 | encode_av (enc, (AV *)sv); |
528 | encode_av (enc, (AV *)sv); |
478 | else if (svt < SVt_PVAV) |
529 | else if (svt < SVt_PVAV) |
479 | { |
530 | { |
… | |
… | |
558 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
609 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
559 | SvPV_nolen (sv), SvFLAGS (sv)); |
610 | SvPV_nolen (sv), SvFLAGS (sv)); |
560 | } |
611 | } |
561 | |
612 | |
562 | static SV * |
613 | static SV * |
563 | encode_json (SV *scalar, U32 flags) |
614 | encode_json (SV *scalar, JSON *json) |
564 | { |
615 | { |
565 | enc_t enc; |
616 | enc_t enc; |
566 | |
617 | |
567 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
618 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
568 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
619 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
569 | |
620 | |
570 | enc.flags = flags; |
621 | enc.json = *json; |
571 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
622 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
572 | enc.cur = SvPVX (enc.sv); |
623 | enc.cur = SvPVX (enc.sv); |
573 | enc.end = SvEND (enc.sv); |
624 | enc.end = SvEND (enc.sv); |
574 | enc.indent = 0; |
625 | enc.indent = 0; |
575 | enc.maxdepth = DEC_DEPTH (flags); |
626 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
576 | |
627 | |
577 | SvPOK_only (enc.sv); |
628 | SvPOK_only (enc.sv); |
578 | encode_sv (&enc, scalar); |
629 | encode_sv (&enc, scalar); |
579 | |
630 | |
580 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
631 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
581 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
632 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
582 | |
633 | |
583 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
634 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
584 | SvUTF8_on (enc.sv); |
635 | SvUTF8_on (enc.sv); |
585 | |
636 | |
586 | if (enc.flags & F_SHRINK) |
637 | if (enc.json.flags & F_SHRINK) |
587 | shrink (enc.sv); |
638 | shrink (enc.sv); |
588 | |
639 | |
589 | return enc.sv; |
640 | return enc.sv; |
590 | } |
641 | } |
591 | |
642 | |
… | |
… | |
596 | typedef struct |
647 | typedef struct |
597 | { |
648 | { |
598 | char *cur; // current parser pointer |
649 | char *cur; // current parser pointer |
599 | char *end; // end of input string |
650 | char *end; // end of input string |
600 | const char *err; // parse error, if != 0 |
651 | const char *err; // parse error, if != 0 |
601 | U32 flags; // F_* |
652 | JSON json; |
602 | U32 depth; // recursion depth |
653 | U32 depth; // recursion depth |
603 | U32 maxdepth; // recursion depth limit |
654 | U32 maxdepth; // recursion depth limit |
604 | } dec_t; |
655 | } dec_t; |
605 | |
656 | |
606 | inline void |
657 | inline void |
… | |
… | |
967 | if (*dec->cur == '}') |
1018 | if (*dec->cur == '}') |
968 | ++dec->cur; |
1019 | ++dec->cur; |
969 | else |
1020 | else |
970 | for (;;) |
1021 | for (;;) |
971 | { |
1022 | { |
972 | SV *key, *value; |
|
|
973 | |
|
|
974 | decode_ws (dec); EXPECT_CH ('"'); |
1023 | decode_ws (dec); EXPECT_CH ('"'); |
975 | |
1024 | |
976 | key = decode_str (dec); |
1025 | // heuristic: assume that |
977 | if (!key) |
1026 | // a) decode_str + hv_store_ent are abysmally slow. |
978 | goto fail; |
1027 | // b) most hash keys are short, simple ascii text. |
|
|
1028 | // => try to "fast-match" such strings to avoid |
|
|
1029 | // the overhead of decode_str + hv_store_ent. |
|
|
1030 | { |
|
|
1031 | SV *value; |
|
|
1032 | char *p = dec->cur; |
|
|
1033 | char *e = p + 24; // only try up to 24 bytes |
979 | |
1034 | |
980 | decode_ws (dec); EXPECT_CH (':'); |
1035 | for (;;) |
981 | |
|
|
982 | value = decode_sv (dec); |
|
|
983 | if (!value) |
|
|
984 | { |
1036 | { |
|
|
1037 | // the >= 0x80 is true on most architectures |
|
|
1038 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1039 | { |
|
|
1040 | // slow path, back up and use decode_str |
|
|
1041 | SV *key = decode_str (dec); |
|
|
1042 | if (!key) |
|
|
1043 | goto fail; |
|
|
1044 | |
|
|
1045 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1046 | |
|
|
1047 | value = decode_sv (dec); |
|
|
1048 | if (!value) |
|
|
1049 | { |
|
|
1050 | SvREFCNT_dec (key); |
|
|
1051 | goto fail; |
|
|
1052 | } |
|
|
1053 | |
|
|
1054 | hv_store_ent (hv, key, value, 0); |
985 | SvREFCNT_dec (key); |
1055 | SvREFCNT_dec (key); |
|
|
1056 | |
|
|
1057 | break; |
|
|
1058 | } |
|
|
1059 | else if (*p == '"') |
|
|
1060 | { |
|
|
1061 | // fast path, got a simple key |
|
|
1062 | char *key = dec->cur; |
|
|
1063 | int len = p - key; |
|
|
1064 | dec->cur = p + 1; |
|
|
1065 | |
|
|
1066 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1067 | |
|
|
1068 | value = decode_sv (dec); |
|
|
1069 | if (!value) |
986 | goto fail; |
1070 | goto fail; |
|
|
1071 | |
|
|
1072 | hv_store (hv, key, len, value, 0); |
|
|
1073 | |
|
|
1074 | break; |
|
|
1075 | } |
|
|
1076 | |
|
|
1077 | ++p; |
987 | } |
1078 | } |
988 | |
1079 | } |
989 | hv_store_ent (hv, key, value, 0); |
|
|
990 | SvREFCNT_dec (key); |
|
|
991 | |
1080 | |
992 | decode_ws (dec); |
1081 | decode_ws (dec); |
993 | |
1082 | |
994 | if (*dec->cur == '}') |
1083 | if (*dec->cur == '}') |
995 | { |
1084 | { |
… | |
… | |
1071 | fail: |
1160 | fail: |
1072 | return 0; |
1161 | return 0; |
1073 | } |
1162 | } |
1074 | |
1163 | |
1075 | static SV * |
1164 | static SV * |
1076 | decode_json (SV *string, U32 flags, UV *offset_return) |
1165 | decode_json (SV *string, JSON *json, UV *offset_return) |
1077 | { |
1166 | { |
1078 | dec_t dec; |
1167 | dec_t dec; |
1079 | UV offset; |
1168 | UV offset; |
1080 | SV *sv; |
1169 | SV *sv; |
1081 | |
1170 | |
1082 | SvGETMAGIC (string); |
1171 | SvGETMAGIC (string); |
1083 | SvUPGRADE (string, SVt_PV); |
1172 | SvUPGRADE (string, SVt_PV); |
1084 | |
1173 | |
|
|
1174 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
|
|
1175 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1176 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
|
|
1177 | |
1085 | if (flags & F_UTF8) |
1178 | if (json->flags & F_UTF8) |
1086 | sv_utf8_downgrade (string, 0); |
1179 | sv_utf8_downgrade (string, 0); |
1087 | else |
1180 | else |
1088 | sv_utf8_upgrade (string); |
1181 | sv_utf8_upgrade (string); |
1089 | |
1182 | |
1090 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1183 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1091 | |
1184 | |
1092 | dec.flags = flags; |
1185 | dec.json = *json; |
1093 | dec.cur = SvPVX (string); |
1186 | dec.cur = SvPVX (string); |
1094 | dec.end = SvEND (string); |
1187 | dec.end = SvEND (string); |
1095 | dec.err = 0; |
1188 | dec.err = 0; |
1096 | dec.depth = 0; |
1189 | dec.depth = 0; |
1097 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1190 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1098 | |
1191 | |
1099 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1192 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1100 | sv = decode_sv (&dec); |
1193 | sv = decode_sv (&dec); |
1101 | |
1194 | |
1102 | if (!(offset_return || !sv)) |
1195 | if (!(offset_return || !sv)) |
… | |
… | |
1112 | } |
1205 | } |
1113 | } |
1206 | } |
1114 | |
1207 | |
1115 | if (offset_return || !sv) |
1208 | if (offset_return || !sv) |
1116 | { |
1209 | { |
1117 | offset = dec.flags & F_UTF8 |
1210 | offset = dec.json.flags & F_UTF8 |
1118 | ? dec.cur - SvPVX (string) |
1211 | ? dec.cur - SvPVX (string) |
1119 | : utf8_distance (dec.cur, SvPVX (string)); |
1212 | : utf8_distance (dec.cur, SvPVX (string)); |
1120 | |
1213 | |
1121 | if (offset_return) |
1214 | if (offset_return) |
1122 | *offset_return = offset; |
1215 | *offset_return = offset; |
… | |
… | |
1141 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1234 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1142 | } |
1235 | } |
1143 | |
1236 | |
1144 | sv = sv_2mortal (sv); |
1237 | sv = sv_2mortal (sv); |
1145 | |
1238 | |
1146 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1239 | 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)"); |
1240 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1148 | |
1241 | |
1149 | return sv; |
1242 | return sv; |
1150 | } |
1243 | } |
1151 | |
1244 | |
… | |
… | |
1163 | i >= '0' && i <= '9' ? i - '0' |
1256 | i >= '0' && i <= '9' ? i - '0' |
1164 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1257 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1165 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1258 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1166 | : -1; |
1259 | : -1; |
1167 | |
1260 | |
1168 | json_stash = gv_stashpv ("JSON::XS", 1); |
1261 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1262 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1169 | |
1263 | |
1170 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
1264 | 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); |
1265 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1172 | } |
1266 | } |
1173 | |
1267 | |
1174 | PROTOTYPES: DISABLE |
1268 | PROTOTYPES: DISABLE |
1175 | |
1269 | |
1176 | SV *new (char *dummy) |
1270 | void new (char *klass) |
1177 | CODE: |
1271 | PPCODE: |
1178 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1272 | { |
1179 | OUTPUT: |
1273 | SV *pv = NEWSV (0, sizeof (JSON)); |
1180 | RETVAL |
1274 | SvPOK_only (pv); |
|
|
1275 | Zero (SvPVX (pv), 1, sizeof (JSON)); |
|
|
1276 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1277 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
|
|
1278 | } |
1181 | |
1279 | |
1182 | SV *ascii (SV *self, int enable = 1) |
1280 | void ascii (JSON *self, int enable = 1) |
1183 | ALIAS: |
1281 | ALIAS: |
1184 | ascii = F_ASCII |
1282 | ascii = F_ASCII |
1185 | latin1 = F_LATIN1 |
1283 | latin1 = F_LATIN1 |
1186 | utf8 = F_UTF8 |
1284 | utf8 = F_UTF8 |
1187 | indent = F_INDENT |
1285 | indent = F_INDENT |
1188 | canonical = F_CANONICAL |
1286 | canonical = F_CANONICAL |
1189 | space_before = F_SPACE_BEFORE |
1287 | space_before = F_SPACE_BEFORE |
1190 | space_after = F_SPACE_AFTER |
1288 | space_after = F_SPACE_AFTER |
1191 | pretty = F_PRETTY |
1289 | pretty = F_PRETTY |
1192 | allow_nonref = F_ALLOW_NONREF |
1290 | allow_nonref = F_ALLOW_NONREF |
1193 | shrink = F_SHRINK |
1291 | shrink = F_SHRINK |
|
|
1292 | allow_blessed = F_ALLOW_BLESSED |
|
|
1293 | convert_blessed = F_CONV_BLESSED |
1194 | CODE: |
1294 | PPCODE: |
1195 | { |
1295 | { |
1196 | UV *uv = SvJSON (self); |
|
|
1197 | if (enable) |
1296 | if (enable) |
1198 | *uv |= ix; |
1297 | self->flags |= ix; |
1199 | else |
1298 | else |
1200 | *uv &= ~ix; |
1299 | self->flags &= ~ix; |
1201 | |
1300 | |
1202 | RETVAL = newSVsv (self); |
1301 | XPUSHs (ST (0)); |
1203 | } |
1302 | } |
1204 | OUTPUT: |
|
|
1205 | RETVAL |
|
|
1206 | |
1303 | |
1207 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1304 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1208 | CODE: |
1305 | PPCODE: |
1209 | { |
1306 | { |
1210 | UV *uv = SvJSON (self); |
|
|
1211 | UV log2 = 0; |
1307 | UV log2 = 0; |
1212 | |
1308 | |
1213 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1309 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1214 | |
1310 | |
1215 | while ((1UL << log2) < max_depth) |
1311 | while ((1UL << log2) < max_depth) |
1216 | ++log2; |
1312 | ++log2; |
1217 | |
1313 | |
1218 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1314 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1219 | |
1315 | |
1220 | RETVAL = newSVsv (self); |
1316 | XPUSHs (ST (0)); |
1221 | } |
1317 | } |
1222 | OUTPUT: |
|
|
1223 | RETVAL |
|
|
1224 | |
1318 | |
1225 | void encode (SV *self, SV *scalar) |
1319 | void max_size (JSON *self, UV max_size = 0) |
1226 | PPCODE: |
1320 | PPCODE: |
1227 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1321 | { |
|
|
1322 | UV log2 = 0; |
1228 | |
1323 | |
|
|
1324 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1325 | if (max_size == 1) max_size = 2; |
|
|
1326 | |
|
|
1327 | while ((1UL << log2) < max_size) |
|
|
1328 | ++log2; |
|
|
1329 | |
|
|
1330 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1331 | |
|
|
1332 | XPUSHs (ST (0)); |
|
|
1333 | } |
|
|
1334 | |
1229 | void decode (SV *self, SV *jsonstr) |
1335 | void encode (JSON *self, SV *scalar) |
1230 | PPCODE: |
1336 | PPCODE: |
|
|
1337 | XPUSHs (encode_json (scalar, self)); |
|
|
1338 | |
|
|
1339 | void decode (JSON *self, SV *jsonstr) |
|
|
1340 | PPCODE: |
1231 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1341 | XPUSHs (decode_json (jsonstr, self, 0)); |
1232 | |
1342 | |
1233 | void decode_prefix (SV *self, SV *jsonstr) |
1343 | void decode_prefix (JSON *self, SV *jsonstr) |
1234 | PPCODE: |
1344 | PPCODE: |
1235 | { |
1345 | { |
1236 | UV offset; |
1346 | UV offset; |
1237 | EXTEND (SP, 2); |
1347 | EXTEND (SP, 2); |
1238 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1348 | PUSHs (decode_json (jsonstr, self, &offset)); |
1239 | PUSHs (sv_2mortal (newSVuv (offset))); |
1349 | PUSHs (sv_2mortal (newSVuv (offset))); |
1240 | } |
1350 | } |
1241 | |
1351 | |
1242 | PROTOTYPES: ENABLE |
1352 | PROTOTYPES: ENABLE |
1243 | |
1353 | |
1244 | void to_json (SV *scalar) |
1354 | void to_json (SV *scalar) |
1245 | ALIAS: |
|
|
1246 | objToJson = 0 |
|
|
1247 | PPCODE: |
1355 | PPCODE: |
|
|
1356 | { |
|
|
1357 | JSON json = { F_DEFAULT | F_UTF8 }; |
1248 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1358 | XPUSHs (encode_json (scalar, &json)); |
|
|
1359 | } |
1249 | |
1360 | |
1250 | void from_json (SV *jsonstr) |
1361 | void from_json (SV *jsonstr) |
1251 | ALIAS: |
|
|
1252 | jsonToObj = 0 |
|
|
1253 | PPCODE: |
1362 | PPCODE: |
|
|
1363 | { |
|
|
1364 | JSON json = { F_DEFAULT | F_UTF8 }; |
1254 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1365 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1366 | } |
1255 | |
1367 | |