… | |
… | |
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 |
10 | #define F_UTF8 0x00000002UL |
15 | #define F_UTF8 0x00000002UL |
11 | #define F_INDENT 0x00000004UL |
16 | #define F_INDENT 0x00000004UL |
12 | #define F_CANONICAL 0x00000008UL |
17 | #define F_CANONICAL 0x00000008UL |
… | |
… | |
21 | |
26 | |
22 | // F_SELFCONVERT? <=> to_json/toJson |
27 | // F_SELFCONVERT? <=> to_json/toJson |
23 | // F_BLESSED? <=> { $__class__$ => } |
28 | // F_BLESSED? <=> { $__class__$ => } |
24 | |
29 | |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
30 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
26 | #define F_DEFAULT (12UL << S_MAXDEPTH) |
31 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
27 | |
32 | |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
33 | #define INIT_SIZE 32 // initial scalar size to be allocated |
29 | #define INDENT_STEP 3 // spaces per indentation level |
34 | #define INDENT_STEP 3 // spaces per indentation level |
30 | |
35 | |
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 |
36 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
33 | |
37 | |
34 | #define SB do { |
38 | #define SB do { |
35 | #define SE } while (0) |
39 | #define SE } while (0) |
36 | |
40 | |
… | |
… | |
76 | { |
80 | { |
77 | *clen = 2; |
81 | *clen = 2; |
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
82 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
79 | } |
83 | } |
80 | else |
84 | else |
|
|
85 | { |
|
|
86 | *clen = (STRLEN)-1; |
81 | return (UV)-1; |
87 | return (UV)-1; |
|
|
88 | } |
82 | } |
89 | } |
83 | |
90 | |
84 | ///////////////////////////////////////////////////////////////////////////// |
91 | ///////////////////////////////////////////////////////////////////////////// |
85 | // encoder |
92 | // encoder |
86 | |
93 | |
… | |
… | |
101 | if (enc->cur + len >= enc->end) |
108 | if (enc->cur + len >= enc->end) |
102 | { |
109 | { |
103 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
110 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
104 | SvGROW (enc->sv, cur + len + 1); |
111 | SvGROW (enc->sv, cur + len + 1); |
105 | enc->cur = SvPVX (enc->sv) + cur; |
112 | enc->cur = SvPVX (enc->sv) + cur; |
106 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
113 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
107 | } |
114 | } |
108 | } |
115 | } |
109 | |
116 | |
110 | static void |
117 | static void |
111 | encode_ch (enc_t *enc, char ch) |
118 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
208 | } |
215 | } |
209 | while (--clen); |
216 | while (--clen); |
210 | } |
217 | } |
211 | else |
218 | else |
212 | { |
219 | { |
213 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
220 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
214 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
221 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
215 | ++str; |
222 | ++str; |
216 | } |
223 | } |
217 | } |
224 | } |
218 | } |
225 | } |
… | |
… | |
405 | |
412 | |
406 | encode_nl (enc); |
413 | encode_nl (enc); |
407 | } |
414 | } |
408 | else |
415 | else |
409 | { |
416 | { |
410 | SV *sv; |
|
|
411 | HE *he = hv_iternext (hv); |
417 | HE *he = hv_iternext (hv); |
412 | |
418 | |
413 | for (;;) |
419 | for (;;) |
414 | { |
420 | { |
415 | encode_indent (enc); |
421 | encode_indent (enc); |
… | |
… | |
430 | |
436 | |
431 | // encode objects, arrays and special \0=false and \1=true values. |
437 | // encode objects, arrays and special \0=false and \1=true values. |
432 | static void |
438 | static void |
433 | encode_rv (enc_t *enc, SV *sv) |
439 | encode_rv (enc_t *enc, SV *sv) |
434 | { |
440 | { |
|
|
441 | svtype svt; |
|
|
442 | |
435 | SvGETMAGIC (sv); |
443 | SvGETMAGIC (sv); |
436 | |
|
|
437 | svtype svt = SvTYPE (sv); |
444 | svt = SvTYPE (sv); |
438 | |
445 | |
439 | if (svt == SVt_PVHV) |
446 | if (svt == SVt_PVHV) |
440 | encode_hv (enc, (HV *)sv); |
447 | encode_hv (enc, (HV *)sv); |
441 | else if (svt == SVt_PVAV) |
448 | else if (svt == SVt_PVAV) |
442 | encode_av (enc, (AV *)sv); |
449 | encode_av (enc, (AV *)sv); |
… | |
… | |
492 | } |
499 | } |
493 | |
500 | |
494 | static SV * |
501 | static SV * |
495 | encode_json (SV *scalar, U32 flags) |
502 | encode_json (SV *scalar, U32 flags) |
496 | { |
503 | { |
|
|
504 | enc_t enc; |
|
|
505 | |
497 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
506 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
498 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
507 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
499 | |
508 | |
500 | enc_t enc; |
|
|
501 | enc.flags = flags; |
509 | enc.flags = flags; |
502 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
510 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
503 | enc.cur = SvPVX (enc.sv); |
511 | enc.cur = SvPVX (enc.sv); |
504 | enc.end = SvEND (enc.sv); |
512 | enc.end = SvEND (enc.sv); |
505 | enc.indent = 0; |
513 | enc.indent = 0; |
… | |
… | |
510 | |
518 | |
511 | if (!(flags & (F_ASCII | F_UTF8))) |
519 | if (!(flags & (F_ASCII | F_UTF8))) |
512 | SvUTF8_on (enc.sv); |
520 | SvUTF8_on (enc.sv); |
513 | |
521 | |
514 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
522 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
523 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
515 | |
524 | |
516 | if (enc.flags & F_SHRINK) |
525 | if (enc.flags & F_SHRINK) |
517 | shrink (enc.sv); |
526 | shrink (enc.sv); |
518 | |
527 | |
519 | return enc.sv; |
528 | return enc.sv; |
… | |
… | |
591 | SV *sv = 0; |
600 | SV *sv = 0; |
592 | int utf8 = 0; |
601 | int utf8 = 0; |
593 | |
602 | |
594 | do |
603 | do |
595 | { |
604 | { |
596 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
605 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
597 | char *cur = buf; |
606 | char *cur = buf; |
598 | |
607 | |
599 | do |
608 | do |
600 | { |
609 | { |
601 | unsigned char ch = *(unsigned char *)dec->cur++; |
610 | unsigned char ch = *(unsigned char *)dec->cur++; |
… | |
… | |
667 | } |
676 | } |
668 | else if (ch >= 0x20 && ch <= 0x7f) |
677 | else if (ch >= 0x20 && ch <= 0x7f) |
669 | *cur++ = ch; |
678 | *cur++ = ch; |
670 | else if (ch >= 0x80) |
679 | else if (ch >= 0x80) |
671 | { |
680 | { |
|
|
681 | STRLEN clen; |
|
|
682 | UV uch; |
|
|
683 | |
672 | --dec->cur; |
684 | --dec->cur; |
673 | |
685 | |
674 | STRLEN clen; |
|
|
675 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
686 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
676 | if (clen == (STRLEN)-1) |
687 | if (clen == (STRLEN)-1) |
677 | ERR ("malformed UTF-8 character in JSON string"); |
688 | ERR ("malformed UTF-8 character in JSON string"); |
678 | |
689 | |
679 | do |
690 | do |
680 | { |
|
|
681 | *cur++ = *dec->cur++; |
691 | *cur++ = *dec->cur++; |
682 | } |
|
|
683 | while (--clen); |
692 | while (--clen); |
684 | |
693 | |
685 | utf8 = 1; |
694 | utf8 = 1; |
686 | } |
695 | } |
687 | else if (!ch) |
|
|
688 | ERR ("unexpected end of string while parsing json string"); |
|
|
689 | else |
696 | else |
|
|
697 | { |
|
|
698 | --dec->cur; |
|
|
699 | |
|
|
700 | if (!ch) |
|
|
701 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
702 | else |
690 | ERR ("invalid character encountered"); |
703 | ERR ("invalid character encountered while parsing JSON string"); |
691 | |
704 | } |
692 | } |
705 | } |
693 | while (cur < buf + SHORT_STRING_LEN); |
706 | while (cur < buf + SHORT_STRING_LEN); |
694 | |
707 | |
|
|
708 | { |
695 | STRLEN len = cur - buf; |
709 | STRLEN len = cur - buf; |
696 | |
710 | |
697 | if (sv) |
711 | if (sv) |
698 | { |
712 | { |
699 | SvGROW (sv, SvCUR (sv) + len + 1); |
713 | SvGROW (sv, SvCUR (sv) + len + 1); |
700 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
714 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
701 | SvCUR_set (sv, SvCUR (sv) + len); |
715 | SvCUR_set (sv, SvCUR (sv) + len); |
702 | } |
716 | } |
703 | else |
717 | else |
704 | sv = newSVpvn (buf, len); |
718 | sv = newSVpvn (buf, len); |
|
|
719 | } |
705 | } |
720 | } |
706 | while (*dec->cur != '"'); |
721 | while (*dec->cur != '"'); |
707 | |
722 | |
708 | ++dec->cur; |
723 | ++dec->cur; |
709 | |
724 | |
… | |
… | |
953 | ERR ("'null' expected"); |
968 | ERR ("'null' expected"); |
954 | |
969 | |
955 | break; |
970 | break; |
956 | |
971 | |
957 | default: |
972 | default: |
958 | ERR ("malformed json string, neither array, object, number, string or atom"); |
973 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
959 | break; |
974 | break; |
960 | } |
975 | } |
961 | |
976 | |
962 | fail: |
977 | fail: |
963 | return 0; |
978 | return 0; |
964 | } |
979 | } |
965 | |
980 | |
966 | static SV * |
981 | static SV * |
967 | decode_json (SV *string, U32 flags) |
982 | decode_json (SV *string, U32 flags) |
968 | { |
983 | { |
|
|
984 | dec_t dec; |
969 | SV *sv; |
985 | SV *sv; |
970 | |
986 | |
971 | SvUPGRADE (string, SVt_PV); |
987 | SvUPGRADE (string, SVt_PV); |
972 | |
988 | |
973 | if (flags & F_UTF8) |
989 | if (flags & F_UTF8) |
… | |
… | |
975 | else |
991 | else |
976 | sv_utf8_upgrade (string); |
992 | sv_utf8_upgrade (string); |
977 | |
993 | |
978 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
994 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
979 | |
995 | |
980 | dec_t dec; |
|
|
981 | dec.flags = flags; |
996 | dec.flags = flags; |
982 | dec.cur = SvPVX (string); |
997 | dec.cur = SvPVX (string); |
983 | dec.end = SvEND (string); |
998 | dec.end = SvEND (string); |
984 | dec.err = 0; |
999 | dec.err = 0; |
985 | dec.depth = 0; |
1000 | dec.depth = 0; |
… | |
… | |
1002 | SAVEVPTR (PL_curcop); |
1017 | SAVEVPTR (PL_curcop); |
1003 | PL_curcop = &cop; |
1018 | PL_curcop = &cop; |
1004 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1019 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1005 | LEAVE; |
1020 | LEAVE; |
1006 | |
1021 | |
1007 | croak ("%s, at character offset %d (%s)", |
1022 | croak ("%s, at character offset %d [\"%s\"]", |
1008 | dec.err, |
1023 | dec.err, |
1009 | (int)offset, |
1024 | (int)offset, |
1010 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1025 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1011 | } |
1026 | } |
1012 | |
1027 | |
… | |
… | |
1069 | RETVAL = newSVsv (self); |
1084 | RETVAL = newSVsv (self); |
1070 | } |
1085 | } |
1071 | OUTPUT: |
1086 | OUTPUT: |
1072 | RETVAL |
1087 | RETVAL |
1073 | |
1088 | |
1074 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
1089 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1075 | CODE: |
1090 | CODE: |
1076 | { |
1091 | { |
1077 | UV *uv = SvJSON (self); |
1092 | UV *uv = SvJSON (self); |
1078 | UV log2 = 0; |
1093 | UV log2 = 0; |
1079 | |
1094 | |