… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include "assert.h" |
6 | #include "string.h" |
6 | #include "string.h" |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
|
|
8 | #include "stdio.h" |
|
|
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
8 | |
13 | |
9 | #define F_ASCII 0x00000001UL |
14 | #define F_ASCII 0x00000001UL |
|
|
15 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002UL |
16 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004UL |
17 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008UL |
18 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010UL |
19 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020UL |
20 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_ALLOW_NONREF 0x00000080UL |
21 | #define F_ALLOW_NONREF 0x00000100UL |
16 | #define F_SHRINK 0x00000100UL |
22 | #define F_SHRINK 0x00000200UL |
17 | #define F_MAXDEPTH 0xf8000000UL |
23 | #define F_MAXDEPTH 0xf8000000UL |
18 | #define S_MAXDEPTH 27 |
24 | #define S_MAXDEPTH 27 |
19 | |
25 | |
20 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
21 | |
27 | |
22 | // F_SELFCONVERT? <=> to_json/toJson |
28 | // F_SELFCONVERT? <=> to_json/toJson |
23 | // F_BLESSED? <=> { $__class__$ => } |
29 | // F_BLESSED? <=> { $__class__$ => } |
24 | |
30 | |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
26 | #define F_DEFAULT (13UL << S_MAXDEPTH) |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
27 | |
33 | |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
29 | #define INDENT_STEP 3 // spaces per indentation level |
35 | #define INDENT_STEP 3 // spaces per indentation level |
30 | |
36 | |
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
32 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
33 | |
38 | |
34 | #define SB do { |
39 | #define SB do { |
35 | #define SE } while (0) |
40 | #define SE } while (0) |
36 | |
41 | |
… | |
… | |
76 | { |
81 | { |
77 | *clen = 2; |
82 | *clen = 2; |
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
83 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
79 | } |
84 | } |
80 | else |
85 | else |
|
|
86 | { |
|
|
87 | *clen = (STRLEN)-1; |
81 | return (UV)-1; |
88 | return (UV)-1; |
|
|
89 | } |
82 | } |
90 | } |
83 | |
91 | |
84 | ///////////////////////////////////////////////////////////////////////////// |
92 | ///////////////////////////////////////////////////////////////////////////// |
85 | // encoder |
93 | // encoder |
86 | |
94 | |
… | |
… | |
101 | if (enc->cur + len >= enc->end) |
109 | if (enc->cur + len >= enc->end) |
102 | { |
110 | { |
103 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
111 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
104 | SvGROW (enc->sv, cur + len + 1); |
112 | SvGROW (enc->sv, cur + len + 1); |
105 | enc->cur = SvPVX (enc->sv) + cur; |
113 | enc->cur = SvPVX (enc->sv) + cur; |
106 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
114 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
107 | } |
115 | } |
108 | } |
116 | } |
109 | |
117 | |
110 | static void |
118 | static void |
111 | encode_ch (enc_t *enc, char ch) |
119 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
173 | } |
181 | } |
174 | |
182 | |
175 | if (uch > 0x10FFFFUL) |
183 | if (uch > 0x10FFFFUL) |
176 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
184 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
177 | |
185 | |
178 | if (uch < 0x80 || enc->flags & F_ASCII) |
186 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
179 | { |
187 | { |
180 | if (uch > 0xFFFFUL) |
188 | if (uch > 0xFFFFUL) |
181 | { |
189 | { |
182 | need (enc, len += 11); |
190 | need (enc, len += 11); |
183 | sprintf (enc->cur, "\\u%04x\\u%04x", |
191 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
197 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
205 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
198 | } |
206 | } |
199 | |
207 | |
200 | str += clen; |
208 | str += clen; |
201 | } |
209 | } |
|
|
210 | else if (enc->flags & F_LATIN1) |
|
|
211 | { |
|
|
212 | *enc->cur++ = uch; |
|
|
213 | str += clen; |
|
|
214 | } |
202 | else if (is_utf8) |
215 | else if (is_utf8) |
203 | { |
216 | { |
204 | need (enc, len += clen); |
217 | need (enc, len += clen); |
205 | do |
218 | do |
206 | { |
219 | { |
… | |
… | |
208 | } |
221 | } |
209 | while (--clen); |
222 | while (--clen); |
210 | } |
223 | } |
211 | else |
224 | else |
212 | { |
225 | { |
213 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
226 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
214 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
227 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
215 | ++str; |
228 | ++str; |
216 | } |
229 | } |
217 | } |
230 | } |
218 | } |
231 | } |
… | |
… | |
268 | static void |
281 | static void |
269 | encode_av (enc_t *enc, AV *av) |
282 | encode_av (enc_t *enc, AV *av) |
270 | { |
283 | { |
271 | int i, len = av_len (av); |
284 | int i, len = av_len (av); |
272 | |
285 | |
|
|
286 | if (enc->indent >= enc->maxdepth) |
|
|
287 | croak ("data structure too deep (hit recursion limit)"); |
|
|
288 | |
273 | encode_ch (enc, '['); encode_nl (enc); |
289 | encode_ch (enc, '['); encode_nl (enc); |
274 | ++enc->indent; |
290 | ++enc->indent; |
275 | |
291 | |
276 | for (i = 0; i <= len; ++i) |
292 | for (i = 0; i <= len; ++i) |
277 | { |
293 | { |
… | |
… | |
342 | |
358 | |
343 | static void |
359 | static void |
344 | encode_hv (enc_t *enc, HV *hv) |
360 | encode_hv (enc_t *enc, HV *hv) |
345 | { |
361 | { |
346 | int count, i; |
362 | int count, i; |
|
|
363 | |
|
|
364 | if (enc->indent >= enc->maxdepth) |
|
|
365 | croak ("data structure too deep (hit recursion limit)"); |
347 | |
366 | |
348 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
367 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
349 | |
368 | |
350 | if ((count = hv_iterinit (hv))) |
369 | if ((count = hv_iterinit (hv))) |
351 | { |
370 | { |
… | |
… | |
399 | |
418 | |
400 | encode_nl (enc); |
419 | encode_nl (enc); |
401 | } |
420 | } |
402 | else |
421 | else |
403 | { |
422 | { |
404 | SV *sv; |
|
|
405 | HE *he = hv_iternext (hv); |
423 | HE *he = hv_iternext (hv); |
406 | |
424 | |
407 | for (;;) |
425 | for (;;) |
408 | { |
426 | { |
409 | encode_indent (enc); |
427 | encode_indent (enc); |
… | |
… | |
418 | encode_nl (enc); |
436 | encode_nl (enc); |
419 | } |
437 | } |
420 | } |
438 | } |
421 | |
439 | |
422 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
440 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
441 | } |
|
|
442 | |
|
|
443 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
444 | static void |
|
|
445 | encode_rv (enc_t *enc, SV *sv) |
|
|
446 | { |
|
|
447 | svtype svt; |
|
|
448 | |
|
|
449 | SvGETMAGIC (sv); |
|
|
450 | svt = SvTYPE (sv); |
|
|
451 | |
|
|
452 | if (svt == SVt_PVHV) |
|
|
453 | encode_hv (enc, (HV *)sv); |
|
|
454 | else if (svt == SVt_PVAV) |
|
|
455 | encode_av (enc, (AV *)sv); |
|
|
456 | else if (svt < SVt_PVAV) |
|
|
457 | { |
|
|
458 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
459 | encode_str (enc, "false", 5, 0); |
|
|
460 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
461 | encode_str (enc, "true", 4, 0); |
|
|
462 | else |
|
|
463 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
464 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
465 | } |
|
|
466 | else |
|
|
467 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
468 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
423 | } |
469 | } |
424 | |
470 | |
425 | static void |
471 | static void |
426 | encode_sv (enc_t *enc, SV *sv) |
472 | encode_sv (enc_t *enc, SV *sv) |
427 | { |
473 | { |
… | |
… | |
448 | SvIsUV(sv) |
494 | SvIsUV(sv) |
449 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
495 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
450 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
496 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
451 | } |
497 | } |
452 | else if (SvROK (sv)) |
498 | else if (SvROK (sv)) |
453 | { |
499 | encode_rv (enc, SvRV (sv)); |
454 | SV *rv = SvRV (sv); |
|
|
455 | |
|
|
456 | if (enc->indent >= enc->maxdepth) |
|
|
457 | croak ("data structure too deep (hit recursion limit)"); |
|
|
458 | |
|
|
459 | switch (SvTYPE (rv)) |
|
|
460 | { |
|
|
461 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
462 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
463 | |
|
|
464 | default: |
|
|
465 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
466 | SvPV_nolen (sv)); |
|
|
467 | } |
|
|
468 | } |
|
|
469 | else if (!SvOK (sv)) |
500 | else if (!SvOK (sv)) |
470 | encode_str (enc, "null", 4, 0); |
501 | encode_str (enc, "null", 4, 0); |
471 | else |
502 | else |
472 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
503 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
473 | SvPV_nolen (sv), SvFLAGS (sv)); |
504 | SvPV_nolen (sv), SvFLAGS (sv)); |
474 | } |
505 | } |
475 | |
506 | |
476 | static SV * |
507 | static SV * |
477 | encode_json (SV *scalar, U32 flags) |
508 | encode_json (SV *scalar, U32 flags) |
478 | { |
509 | { |
|
|
510 | enc_t enc; |
|
|
511 | |
479 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
512 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
480 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
513 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
481 | |
514 | |
482 | enc_t enc; |
|
|
483 | enc.flags = flags; |
515 | enc.flags = flags; |
484 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
516 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
485 | enc.cur = SvPVX (enc.sv); |
517 | enc.cur = SvPVX (enc.sv); |
486 | enc.end = SvEND (enc.sv); |
518 | enc.end = SvEND (enc.sv); |
487 | enc.indent = 0; |
519 | enc.indent = 0; |
488 | enc.maxdepth = DEC_DEPTH (flags); |
520 | enc.maxdepth = DEC_DEPTH (flags); |
489 | |
521 | |
490 | SvPOK_only (enc.sv); |
522 | SvPOK_only (enc.sv); |
491 | encode_sv (&enc, scalar); |
523 | encode_sv (&enc, scalar); |
492 | |
524 | |
|
|
525 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
526 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
527 | |
493 | if (!(flags & (F_ASCII | F_UTF8))) |
528 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
494 | SvUTF8_on (enc.sv); |
529 | SvUTF8_on (enc.sv); |
495 | |
|
|
496 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
497 | |
530 | |
498 | if (enc.flags & F_SHRINK) |
531 | if (enc.flags & F_SHRINK) |
499 | shrink (enc.sv); |
532 | shrink (enc.sv); |
500 | |
533 | |
501 | return enc.sv; |
534 | return enc.sv; |
… | |
… | |
573 | SV *sv = 0; |
606 | SV *sv = 0; |
574 | int utf8 = 0; |
607 | int utf8 = 0; |
575 | |
608 | |
576 | do |
609 | do |
577 | { |
610 | { |
578 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
611 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
579 | char *cur = buf; |
612 | char *cur = buf; |
580 | |
613 | |
581 | do |
614 | do |
582 | { |
615 | { |
583 | unsigned char ch = *(unsigned char *)dec->cur++; |
616 | unsigned char ch = *(unsigned char *)dec->cur++; |
… | |
… | |
649 | } |
682 | } |
650 | else if (ch >= 0x20 && ch <= 0x7f) |
683 | else if (ch >= 0x20 && ch <= 0x7f) |
651 | *cur++ = ch; |
684 | *cur++ = ch; |
652 | else if (ch >= 0x80) |
685 | else if (ch >= 0x80) |
653 | { |
686 | { |
|
|
687 | STRLEN clen; |
|
|
688 | UV uch; |
|
|
689 | |
654 | --dec->cur; |
690 | --dec->cur; |
655 | |
691 | |
656 | STRLEN clen; |
|
|
657 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
692 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
658 | if (clen == (STRLEN)-1) |
693 | if (clen == (STRLEN)-1) |
659 | ERR ("malformed UTF-8 character in JSON string"); |
694 | ERR ("malformed UTF-8 character in JSON string"); |
660 | |
695 | |
661 | do |
696 | do |
662 | { |
|
|
663 | *cur++ = *dec->cur++; |
697 | *cur++ = *dec->cur++; |
664 | } |
|
|
665 | while (--clen); |
698 | while (--clen); |
666 | |
699 | |
667 | utf8 = 1; |
700 | utf8 = 1; |
668 | } |
701 | } |
669 | else if (!ch) |
|
|
670 | ERR ("unexpected end of string while parsing json string"); |
|
|
671 | else |
702 | else |
|
|
703 | { |
|
|
704 | --dec->cur; |
|
|
705 | |
|
|
706 | if (!ch) |
|
|
707 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
708 | else |
672 | ERR ("invalid character encountered"); |
709 | ERR ("invalid character encountered while parsing JSON string"); |
673 | |
710 | } |
674 | } |
711 | } |
675 | while (cur < buf + SHORT_STRING_LEN); |
712 | while (cur < buf + SHORT_STRING_LEN); |
676 | |
713 | |
|
|
714 | { |
677 | STRLEN len = cur - buf; |
715 | STRLEN len = cur - buf; |
678 | |
716 | |
679 | if (sv) |
717 | if (sv) |
680 | { |
718 | { |
681 | SvGROW (sv, SvCUR (sv) + len + 1); |
719 | SvGROW (sv, SvCUR (sv) + len + 1); |
682 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
720 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
683 | SvCUR_set (sv, SvCUR (sv) + len); |
721 | SvCUR_set (sv, SvCUR (sv) + len); |
684 | } |
722 | } |
685 | else |
723 | else |
686 | sv = newSVpvn (buf, len); |
724 | sv = newSVpvn (buf, len); |
|
|
725 | } |
687 | } |
726 | } |
688 | while (*dec->cur != '"'); |
727 | while (*dec->cur != '"'); |
689 | |
728 | |
690 | ++dec->cur; |
729 | ++dec->cur; |
691 | |
730 | |
… | |
… | |
935 | ERR ("'null' expected"); |
974 | ERR ("'null' expected"); |
936 | |
975 | |
937 | break; |
976 | break; |
938 | |
977 | |
939 | default: |
978 | default: |
940 | ERR ("malformed json string, neither array, object, number, string or atom"); |
979 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
941 | break; |
980 | break; |
942 | } |
981 | } |
943 | |
982 | |
944 | fail: |
983 | fail: |
945 | return 0; |
984 | return 0; |
946 | } |
985 | } |
947 | |
986 | |
948 | static SV * |
987 | static SV * |
949 | decode_json (SV *string, U32 flags) |
988 | decode_json (SV *string, U32 flags) |
950 | { |
989 | { |
|
|
990 | dec_t dec; |
951 | SV *sv; |
991 | SV *sv; |
|
|
992 | |
|
|
993 | SvGETMAGIC (string); |
|
|
994 | SvUPGRADE (string, SVt_PV); |
952 | |
995 | |
953 | if (flags & F_UTF8) |
996 | if (flags & F_UTF8) |
954 | sv_utf8_downgrade (string, 0); |
997 | sv_utf8_downgrade (string, 0); |
955 | else |
998 | else |
956 | sv_utf8_upgrade (string); |
999 | sv_utf8_upgrade (string); |
957 | |
1000 | |
958 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1001 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
959 | |
1002 | |
960 | dec_t dec; |
|
|
961 | dec.flags = flags; |
1003 | dec.flags = flags; |
962 | dec.cur = SvPVX (string); |
1004 | dec.cur = SvPVX (string); |
963 | dec.end = SvEND (string); |
1005 | dec.end = SvEND (string); |
964 | dec.err = 0; |
1006 | dec.err = 0; |
965 | dec.depth = 0; |
1007 | dec.depth = 0; |
… | |
… | |
982 | SAVEVPTR (PL_curcop); |
1024 | SAVEVPTR (PL_curcop); |
983 | PL_curcop = &cop; |
1025 | PL_curcop = &cop; |
984 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1026 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
985 | LEAVE; |
1027 | LEAVE; |
986 | |
1028 | |
987 | croak ("%s, at character offset %d (%s)", |
1029 | croak ("%s, at character offset %d [\"%s\"]", |
988 | dec.err, |
1030 | dec.err, |
989 | (int)offset, |
1031 | (int)offset, |
990 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1032 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
991 | } |
1033 | } |
992 | |
1034 | |
… | |
… | |
1028 | RETVAL |
1070 | RETVAL |
1029 | |
1071 | |
1030 | SV *ascii (SV *self, int enable = 1) |
1072 | SV *ascii (SV *self, int enable = 1) |
1031 | ALIAS: |
1073 | ALIAS: |
1032 | ascii = F_ASCII |
1074 | ascii = F_ASCII |
|
|
1075 | latin1 = F_LATIN1 |
1033 | utf8 = F_UTF8 |
1076 | utf8 = F_UTF8 |
1034 | indent = F_INDENT |
1077 | indent = F_INDENT |
1035 | canonical = F_CANONICAL |
1078 | canonical = F_CANONICAL |
1036 | space_before = F_SPACE_BEFORE |
1079 | space_before = F_SPACE_BEFORE |
1037 | space_after = F_SPACE_AFTER |
1080 | space_after = F_SPACE_AFTER |
… | |
… | |
1049 | RETVAL = newSVsv (self); |
1092 | RETVAL = newSVsv (self); |
1050 | } |
1093 | } |
1051 | OUTPUT: |
1094 | OUTPUT: |
1052 | RETVAL |
1095 | RETVAL |
1053 | |
1096 | |
1054 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
1097 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1055 | CODE: |
1098 | CODE: |
1056 | { |
1099 | { |
1057 | UV *uv = SvJSON (self); |
1100 | UV *uv = SvJSON (self); |
1058 | UV log2 = 0; |
1101 | UV log2 = 0; |
1059 | |
1102 | |