… | |
… | |
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 | |
… | |
… | |
104 | if (enc->cur + len >= enc->end) |
108 | if (enc->cur + len >= enc->end) |
105 | { |
109 | { |
106 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
110 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
107 | SvGROW (enc->sv, cur + len + 1); |
111 | SvGROW (enc->sv, cur + len + 1); |
108 | enc->cur = SvPVX (enc->sv) + cur; |
112 | enc->cur = SvPVX (enc->sv) + cur; |
109 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
113 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
110 | } |
114 | } |
111 | } |
115 | } |
112 | |
116 | |
113 | static void |
117 | static void |
114 | encode_ch (enc_t *enc, char ch) |
118 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
211 | } |
215 | } |
212 | while (--clen); |
216 | while (--clen); |
213 | } |
217 | } |
214 | else |
218 | else |
215 | { |
219 | { |
216 | 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 |
217 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
221 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
218 | ++str; |
222 | ++str; |
219 | } |
223 | } |
220 | } |
224 | } |
221 | } |
225 | } |
… | |
… | |
408 | |
412 | |
409 | encode_nl (enc); |
413 | encode_nl (enc); |
410 | } |
414 | } |
411 | else |
415 | else |
412 | { |
416 | { |
413 | SV *sv; |
|
|
414 | HE *he = hv_iternext (hv); |
417 | HE *he = hv_iternext (hv); |
415 | |
418 | |
416 | for (;;) |
419 | for (;;) |
417 | { |
420 | { |
418 | encode_indent (enc); |
421 | encode_indent (enc); |
… | |
… | |
433 | |
436 | |
434 | // encode objects, arrays and special \0=false and \1=true values. |
437 | // encode objects, arrays and special \0=false and \1=true values. |
435 | static void |
438 | static void |
436 | encode_rv (enc_t *enc, SV *sv) |
439 | encode_rv (enc_t *enc, SV *sv) |
437 | { |
440 | { |
|
|
441 | svtype svt; |
|
|
442 | |
438 | SvGETMAGIC (sv); |
443 | SvGETMAGIC (sv); |
439 | |
|
|
440 | svtype svt = SvTYPE (sv); |
444 | svt = SvTYPE (sv); |
441 | |
445 | |
442 | if (svt == SVt_PVHV) |
446 | if (svt == SVt_PVHV) |
443 | encode_hv (enc, (HV *)sv); |
447 | encode_hv (enc, (HV *)sv); |
444 | else if (svt == SVt_PVAV) |
448 | else if (svt == SVt_PVAV) |
445 | encode_av (enc, (AV *)sv); |
449 | encode_av (enc, (AV *)sv); |
… | |
… | |
495 | } |
499 | } |
496 | |
500 | |
497 | static SV * |
501 | static SV * |
498 | encode_json (SV *scalar, U32 flags) |
502 | encode_json (SV *scalar, U32 flags) |
499 | { |
503 | { |
|
|
504 | enc_t enc; |
|
|
505 | |
500 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
506 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
501 | 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)"); |
502 | |
508 | |
503 | enc_t enc; |
|
|
504 | enc.flags = flags; |
509 | enc.flags = flags; |
505 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
510 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
506 | enc.cur = SvPVX (enc.sv); |
511 | enc.cur = SvPVX (enc.sv); |
507 | enc.end = SvEND (enc.sv); |
512 | enc.end = SvEND (enc.sv); |
508 | enc.indent = 0; |
513 | enc.indent = 0; |
… | |
… | |
513 | |
518 | |
514 | if (!(flags & (F_ASCII | F_UTF8))) |
519 | if (!(flags & (F_ASCII | F_UTF8))) |
515 | SvUTF8_on (enc.sv); |
520 | SvUTF8_on (enc.sv); |
516 | |
521 | |
517 | 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 |
518 | |
524 | |
519 | if (enc.flags & F_SHRINK) |
525 | if (enc.flags & F_SHRINK) |
520 | shrink (enc.sv); |
526 | shrink (enc.sv); |
521 | |
527 | |
522 | return enc.sv; |
528 | return enc.sv; |
… | |
… | |
594 | SV *sv = 0; |
600 | SV *sv = 0; |
595 | int utf8 = 0; |
601 | int utf8 = 0; |
596 | |
602 | |
597 | do |
603 | do |
598 | { |
604 | { |
599 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
605 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
600 | char *cur = buf; |
606 | char *cur = buf; |
601 | |
607 | |
602 | do |
608 | do |
603 | { |
609 | { |
604 | unsigned char ch = *(unsigned char *)dec->cur++; |
610 | unsigned char ch = *(unsigned char *)dec->cur++; |
… | |
… | |
670 | } |
676 | } |
671 | else if (ch >= 0x20 && ch <= 0x7f) |
677 | else if (ch >= 0x20 && ch <= 0x7f) |
672 | *cur++ = ch; |
678 | *cur++ = ch; |
673 | else if (ch >= 0x80) |
679 | else if (ch >= 0x80) |
674 | { |
680 | { |
|
|
681 | STRLEN clen; |
|
|
682 | UV uch; |
|
|
683 | |
675 | --dec->cur; |
684 | --dec->cur; |
676 | |
685 | |
677 | STRLEN clen; |
|
|
678 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
686 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
679 | if (clen == (STRLEN)-1) |
687 | if (clen == (STRLEN)-1) |
680 | ERR ("malformed UTF-8 character in JSON string"); |
688 | ERR ("malformed UTF-8 character in JSON string"); |
681 | |
689 | |
682 | do |
690 | do |
683 | *cur++ = *dec->cur++; |
691 | *cur++ = *dec->cur++; |
… | |
… | |
688 | else |
696 | else |
689 | { |
697 | { |
690 | --dec->cur; |
698 | --dec->cur; |
691 | |
699 | |
692 | if (!ch) |
700 | if (!ch) |
693 | ERR ("unexpected end of string while parsing json string"); |
701 | ERR ("unexpected end of string while parsing JSON string"); |
694 | else |
702 | else |
695 | ERR ("invalid character encountered while parsing json string"); |
703 | ERR ("invalid character encountered while parsing JSON string"); |
696 | } |
704 | } |
697 | } |
705 | } |
698 | while (cur < buf + SHORT_STRING_LEN); |
706 | while (cur < buf + SHORT_STRING_LEN); |
699 | |
707 | |
|
|
708 | { |
700 | STRLEN len = cur - buf; |
709 | STRLEN len = cur - buf; |
701 | |
710 | |
702 | if (sv) |
711 | if (sv) |
703 | { |
712 | { |
704 | SvGROW (sv, SvCUR (sv) + len + 1); |
713 | SvGROW (sv, SvCUR (sv) + len + 1); |
705 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
714 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
706 | SvCUR_set (sv, SvCUR (sv) + len); |
715 | SvCUR_set (sv, SvCUR (sv) + len); |
707 | } |
716 | } |
708 | else |
717 | else |
709 | sv = newSVpvn (buf, len); |
718 | sv = newSVpvn (buf, len); |
|
|
719 | } |
710 | } |
720 | } |
711 | while (*dec->cur != '"'); |
721 | while (*dec->cur != '"'); |
712 | |
722 | |
713 | ++dec->cur; |
723 | ++dec->cur; |
714 | |
724 | |
… | |
… | |
958 | ERR ("'null' expected"); |
968 | ERR ("'null' expected"); |
959 | |
969 | |
960 | break; |
970 | break; |
961 | |
971 | |
962 | default: |
972 | default: |
963 | ERR ("malformed json string, neither array, object, number, string or atom"); |
973 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
964 | break; |
974 | break; |
965 | } |
975 | } |
966 | |
976 | |
967 | fail: |
977 | fail: |
968 | return 0; |
978 | return 0; |
969 | } |
979 | } |
970 | |
980 | |
971 | static SV * |
981 | static SV * |
972 | decode_json (SV *string, U32 flags) |
982 | decode_json (SV *string, U32 flags) |
973 | { |
983 | { |
|
|
984 | dec_t dec; |
974 | SV *sv; |
985 | SV *sv; |
975 | |
986 | |
976 | SvUPGRADE (string, SVt_PV); |
987 | SvUPGRADE (string, SVt_PV); |
977 | |
988 | |
978 | if (flags & F_UTF8) |
989 | if (flags & F_UTF8) |
… | |
… | |
980 | else |
991 | else |
981 | sv_utf8_upgrade (string); |
992 | sv_utf8_upgrade (string); |
982 | |
993 | |
983 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
994 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
984 | |
995 | |
985 | dec_t dec; |
|
|
986 | dec.flags = flags; |
996 | dec.flags = flags; |
987 | dec.cur = SvPVX (string); |
997 | dec.cur = SvPVX (string); |
988 | dec.end = SvEND (string); |
998 | dec.end = SvEND (string); |
989 | dec.err = 0; |
999 | dec.err = 0; |
990 | dec.depth = 0; |
1000 | dec.depth = 0; |
… | |
… | |
1074 | RETVAL = newSVsv (self); |
1084 | RETVAL = newSVsv (self); |
1075 | } |
1085 | } |
1076 | OUTPUT: |
1086 | OUTPUT: |
1077 | RETVAL |
1087 | RETVAL |
1078 | |
1088 | |
1079 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
1089 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1080 | CODE: |
1090 | CODE: |
1081 | { |
1091 | { |
1082 | UV *uv = SvJSON (self); |
1092 | UV *uv = SvJSON (self); |
1083 | UV log2 = 0; |
1093 | UV log2 = 0; |
1084 | |
1094 | |