… | |
… | |
13 | #define F_SPACE_BEFORE 0x00000010 |
13 | #define F_SPACE_BEFORE 0x00000010 |
14 | #define F_SPACE_AFTER 0x00000020 |
14 | #define F_SPACE_AFTER 0x00000020 |
15 | #define F_ALLOW_NONREF 0x00000080 |
15 | #define F_ALLOW_NONREF 0x00000080 |
16 | #define F_SHRINK 0x00000100 |
16 | #define F_SHRINK 0x00000100 |
17 | |
17 | |
|
|
18 | // F_SKIPINVALID? |
|
|
19 | // F_EXECCODEREF? |
|
|
20 | // F_SELFCONVERT? |
|
|
21 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
22 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
23 | #define F_DEFAULT 0 |
20 | |
24 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
25 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | #define INDENT_STEP 3 // spaces per indentation level |
26 | #define INDENT_STEP 3 // spaces per indentation level |
23 | |
27 | |
24 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
28 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
25 | #define SHORT_STRING_LEN 256 // special-case strings of up to this size |
29 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
26 | |
30 | |
27 | #define SB do { |
31 | #define SB do { |
28 | #define SE } while (0) |
32 | #define SE } while (0) |
29 | |
33 | |
30 | static HV *json_stash; // JSON::XS:: |
34 | static HV *json_stash; // JSON::XS:: |
… | |
… | |
51 | SvPV_shrink_to_cur (sv); |
55 | SvPV_shrink_to_cur (sv); |
52 | #elif defined (SvPV_renew) |
56 | #elif defined (SvPV_renew) |
53 | SvPV_renew (sv, SvCUR (sv) + 1); |
57 | SvPV_renew (sv, SvCUR (sv) + 1); |
54 | #endif |
58 | #endif |
55 | } |
59 | } |
|
|
60 | } |
|
|
61 | |
|
|
62 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
63 | // case of an error. |
|
|
64 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
65 | // but use the very good perl function to parse anything else. |
|
|
66 | // note that we never call this function for a ascii codepoints |
|
|
67 | static UV |
|
|
68 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
69 | { |
|
|
70 | if (s[0] > 0xdf || s[0] < 0xc2) |
|
|
71 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
72 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
73 | { |
|
|
74 | *clen = 2; |
|
|
75 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
76 | } |
|
|
77 | else |
|
|
78 | return (UV)-1; |
56 | } |
79 | } |
57 | |
80 | |
58 | ///////////////////////////////////////////////////////////////////////////// |
81 | ///////////////////////////////////////////////////////////////////////////// |
59 | // encoder |
82 | // encoder |
60 | |
83 | |
… | |
… | |
133 | STRLEN clen; |
156 | STRLEN clen; |
134 | UV uch; |
157 | UV uch; |
135 | |
158 | |
136 | if (is_utf8) |
159 | if (is_utf8) |
137 | { |
160 | { |
138 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
161 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
162 | uch = decode_utf8 (str, end - str, &clen); |
139 | if (clen == (STRLEN)-1) |
163 | if (clen == (STRLEN)-1) |
140 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
164 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
141 | } |
165 | } |
142 | else |
166 | else |
143 | { |
167 | { |
… | |
… | |
619 | else if (ch >= 0x80) |
643 | else if (ch >= 0x80) |
620 | { |
644 | { |
621 | --dec->cur; |
645 | --dec->cur; |
622 | |
646 | |
623 | STRLEN clen; |
647 | STRLEN clen; |
624 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
648 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
625 | if (clen == (STRLEN)-1) |
649 | if (clen == (STRLEN)-1) |
626 | ERR ("malformed UTF-8 character in JSON string"); |
650 | ERR ("malformed UTF-8 character in JSON string"); |
627 | |
651 | |
628 | do |
652 | do |
629 | { |
653 | { |
… | |
… | |
1017 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1041 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1018 | |
1042 | |
1019 | PROTOTYPES: ENABLE |
1043 | PROTOTYPES: ENABLE |
1020 | |
1044 | |
1021 | void to_json (SV *scalar) |
1045 | void to_json (SV *scalar) |
|
|
1046 | ALIAS: |
|
|
1047 | objToJson = 0 |
1022 | PPCODE: |
1048 | PPCODE: |
1023 | XPUSHs (encode_json (scalar, F_UTF8)); |
1049 | XPUSHs (encode_json (scalar, F_UTF8)); |
1024 | |
1050 | |
1025 | void from_json (SV *jsonstr) |
1051 | void from_json (SV *jsonstr) |
|
|
1052 | ALIAS: |
|
|
1053 | jsonToObj = 0 |
1026 | PPCODE: |
1054 | PPCODE: |
1027 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1055 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1028 | |
1056 | |