… | |
… | |
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 |
36 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
… | |
… | |
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) |
… | |
… | |
405 | |
413 | |
406 | encode_nl (enc); |
414 | encode_nl (enc); |
407 | } |
415 | } |
408 | else |
416 | else |
409 | { |
417 | { |
410 | SV *sv; |
|
|
411 | HE *he = hv_iternext (hv); |
418 | HE *he = hv_iternext (hv); |
412 | |
419 | |
413 | for (;;) |
420 | for (;;) |
414 | { |
421 | { |
415 | encode_indent (enc); |
422 | encode_indent (enc); |
… | |
… | |
430 | |
437 | |
431 | // encode objects, arrays and special \0=false and \1=true values. |
438 | // encode objects, arrays and special \0=false and \1=true values. |
432 | static void |
439 | static void |
433 | encode_rv (enc_t *enc, SV *sv) |
440 | encode_rv (enc_t *enc, SV *sv) |
434 | { |
441 | { |
|
|
442 | svtype svt; |
|
|
443 | |
435 | SvGETMAGIC (sv); |
444 | SvGETMAGIC (sv); |
436 | |
|
|
437 | svtype svt = SvTYPE (sv); |
445 | svt = SvTYPE (sv); |
438 | |
446 | |
439 | if (svt == SVt_PVHV) |
447 | if (svt == SVt_PVHV) |
440 | encode_hv (enc, (HV *)sv); |
448 | encode_hv (enc, (HV *)sv); |
441 | else if (svt == SVt_PVAV) |
449 | else if (svt == SVt_PVAV) |
442 | encode_av (enc, (AV *)sv); |
450 | encode_av (enc, (AV *)sv); |
… | |
… | |
492 | } |
500 | } |
493 | |
501 | |
494 | static SV * |
502 | static SV * |
495 | encode_json (SV *scalar, U32 flags) |
503 | encode_json (SV *scalar, U32 flags) |
496 | { |
504 | { |
|
|
505 | enc_t enc; |
|
|
506 | |
497 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
507 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
498 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
508 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
499 | |
509 | |
500 | enc_t enc; |
|
|
501 | enc.flags = flags; |
510 | enc.flags = flags; |
502 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
511 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
503 | enc.cur = SvPVX (enc.sv); |
512 | enc.cur = SvPVX (enc.sv); |
504 | enc.end = SvEND (enc.sv); |
513 | enc.end = SvEND (enc.sv); |
505 | enc.indent = 0; |
514 | enc.indent = 0; |
… | |
… | |
510 | |
519 | |
511 | if (!(flags & (F_ASCII | F_UTF8))) |
520 | if (!(flags & (F_ASCII | F_UTF8))) |
512 | SvUTF8_on (enc.sv); |
521 | SvUTF8_on (enc.sv); |
513 | |
522 | |
514 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
523 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
524 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
515 | |
525 | |
516 | if (enc.flags & F_SHRINK) |
526 | if (enc.flags & F_SHRINK) |
517 | shrink (enc.sv); |
527 | shrink (enc.sv); |
518 | |
528 | |
519 | return enc.sv; |
529 | return enc.sv; |
… | |
… | |
667 | } |
677 | } |
668 | else if (ch >= 0x20 && ch <= 0x7f) |
678 | else if (ch >= 0x20 && ch <= 0x7f) |
669 | *cur++ = ch; |
679 | *cur++ = ch; |
670 | else if (ch >= 0x80) |
680 | else if (ch >= 0x80) |
671 | { |
681 | { |
|
|
682 | STRLEN clen; |
|
|
683 | UV uch; |
|
|
684 | |
672 | --dec->cur; |
685 | --dec->cur; |
673 | |
686 | |
674 | STRLEN clen; |
|
|
675 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
687 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
676 | if (clen == (STRLEN)-1) |
688 | if (clen == (STRLEN)-1) |
677 | ERR ("malformed UTF-8 character in JSON string"); |
689 | ERR ("malformed UTF-8 character in JSON string"); |
678 | |
690 | |
679 | do |
691 | do |
680 | { |
|
|
681 | *cur++ = *dec->cur++; |
692 | *cur++ = *dec->cur++; |
682 | } |
|
|
683 | while (--clen); |
693 | while (--clen); |
684 | |
694 | |
685 | utf8 = 1; |
695 | utf8 = 1; |
686 | } |
696 | } |
687 | else if (!ch) |
|
|
688 | ERR ("unexpected end of string while parsing json string"); |
|
|
689 | else |
697 | else |
|
|
698 | { |
|
|
699 | --dec->cur; |
|
|
700 | |
|
|
701 | if (!ch) |
|
|
702 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
703 | else |
690 | ERR ("invalid character encountered"); |
704 | ERR ("invalid character encountered while parsing JSON string"); |
691 | |
705 | } |
692 | } |
706 | } |
693 | while (cur < buf + SHORT_STRING_LEN); |
707 | while (cur < buf + SHORT_STRING_LEN); |
694 | |
708 | |
|
|
709 | { |
695 | STRLEN len = cur - buf; |
710 | STRLEN len = cur - buf; |
696 | |
711 | |
697 | if (sv) |
712 | if (sv) |
698 | { |
713 | { |
699 | SvGROW (sv, SvCUR (sv) + len + 1); |
714 | SvGROW (sv, SvCUR (sv) + len + 1); |
700 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
715 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
701 | SvCUR_set (sv, SvCUR (sv) + len); |
716 | SvCUR_set (sv, SvCUR (sv) + len); |
702 | } |
717 | } |
703 | else |
718 | else |
704 | sv = newSVpvn (buf, len); |
719 | sv = newSVpvn (buf, len); |
|
|
720 | } |
705 | } |
721 | } |
706 | while (*dec->cur != '"'); |
722 | while (*dec->cur != '"'); |
707 | |
723 | |
708 | ++dec->cur; |
724 | ++dec->cur; |
709 | |
725 | |
… | |
… | |
953 | ERR ("'null' expected"); |
969 | ERR ("'null' expected"); |
954 | |
970 | |
955 | break; |
971 | break; |
956 | |
972 | |
957 | default: |
973 | default: |
958 | ERR ("malformed json string, neither array, object, number, string or atom"); |
974 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
959 | break; |
975 | break; |
960 | } |
976 | } |
961 | |
977 | |
962 | fail: |
978 | fail: |
963 | return 0; |
979 | return 0; |
964 | } |
980 | } |
965 | |
981 | |
966 | static SV * |
982 | static SV * |
967 | decode_json (SV *string, U32 flags) |
983 | decode_json (SV *string, U32 flags) |
968 | { |
984 | { |
|
|
985 | dec_t dec; |
969 | SV *sv; |
986 | SV *sv; |
970 | |
987 | |
971 | SvUPGRADE (string, SVt_PV); |
988 | SvUPGRADE (string, SVt_PV); |
972 | |
989 | |
973 | if (flags & F_UTF8) |
990 | if (flags & F_UTF8) |
… | |
… | |
975 | else |
992 | else |
976 | sv_utf8_upgrade (string); |
993 | sv_utf8_upgrade (string); |
977 | |
994 | |
978 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
995 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
979 | |
996 | |
980 | dec_t dec; |
|
|
981 | dec.flags = flags; |
997 | dec.flags = flags; |
982 | dec.cur = SvPVX (string); |
998 | dec.cur = SvPVX (string); |
983 | dec.end = SvEND (string); |
999 | dec.end = SvEND (string); |
984 | dec.err = 0; |
1000 | dec.err = 0; |
985 | dec.depth = 0; |
1001 | dec.depth = 0; |
… | |
… | |
1002 | SAVEVPTR (PL_curcop); |
1018 | SAVEVPTR (PL_curcop); |
1003 | PL_curcop = &cop; |
1019 | PL_curcop = &cop; |
1004 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1020 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1005 | LEAVE; |
1021 | LEAVE; |
1006 | |
1022 | |
1007 | croak ("%s, at character offset %d (%s)", |
1023 | croak ("%s, at character offset %d [\"%s\"]", |
1008 | dec.err, |
1024 | dec.err, |
1009 | (int)offset, |
1025 | (int)offset, |
1010 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1026 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1011 | } |
1027 | } |
1012 | |
1028 | |
… | |
… | |
1069 | RETVAL = newSVsv (self); |
1085 | RETVAL = newSVsv (self); |
1070 | } |
1086 | } |
1071 | OUTPUT: |
1087 | OUTPUT: |
1072 | RETVAL |
1088 | RETVAL |
1073 | |
1089 | |
1074 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
1090 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1075 | CODE: |
1091 | CODE: |
1076 | { |
1092 | { |
1077 | UV *uv = SvJSON (self); |
1093 | UV *uv = SvJSON (self); |
1078 | UV log2 = 0; |
1094 | UV log2 = 0; |
1079 | |
1095 | |