… | |
… | |
12 | #define F_CANONICAL 0x00000008 |
12 | #define F_CANONICAL 0x00000008 |
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_JSON_RPC 0x00000040 |
15 | #define F_JSON_RPC 0x00000040 |
16 | #define F_ALLOW_NONREF 0x00000080 |
16 | #define F_ALLOW_NONREF 0x00000080 |
|
|
17 | #define F_SHRINK 0x00000100 |
17 | |
18 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
20 | #define F_DEFAULT 0 |
20 | |
21 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | #define INIT_SIZE 32 // initial scalar size to be allocated |
… | |
… | |
53 | croak ("object is not of type JSON::XS"); |
54 | croak ("object is not of type JSON::XS"); |
54 | |
55 | |
55 | return &SvUVX (SvRV (sv)); |
56 | return &SvUVX (SvRV (sv)); |
56 | } |
57 | } |
57 | |
58 | |
|
|
59 | static void |
|
|
60 | shrink (SV *sv) |
|
|
61 | { |
|
|
62 | sv_utf8_downgrade (sv, 1); |
|
|
63 | #ifdef SvPV_shrink_to_cur |
|
|
64 | SvPV_shrink_to_cur (sv); |
|
|
65 | #endif |
|
|
66 | } |
|
|
67 | |
58 | ///////////////////////////////////////////////////////////////////////////// |
68 | ///////////////////////////////////////////////////////////////////////////// |
59 | |
69 | |
60 | static void |
70 | static void |
61 | need (enc_t *enc, STRLEN len) |
71 | need (enc_t *enc, STRLEN len) |
62 | { |
72 | { |
… | |
… | |
85 | |
95 | |
86 | while (str < end) |
96 | while (str < end) |
87 | { |
97 | { |
88 | unsigned char ch = *(unsigned char *)str; |
98 | unsigned char ch = *(unsigned char *)str; |
89 | |
99 | |
90 | if (ch == '"') |
100 | if (ch >= 0x20 && ch < 0x80) // most common case |
91 | { |
101 | { |
|
|
102 | if (ch == '"') // but with slow exceptions |
|
|
103 | { |
92 | need (enc, len += 1); |
104 | need (enc, len += 1); |
93 | *enc->cur++ = '\\'; |
105 | *enc->cur++ = '\\'; |
94 | *enc->cur++ = '"'; |
106 | *enc->cur++ = '"'; |
95 | ++str; |
|
|
96 | } |
107 | } |
97 | else if (ch == '\\') |
108 | else if (ch == '\\') |
98 | { |
109 | { |
99 | need (enc, len += 1); |
110 | need (enc, len += 1); |
100 | *enc->cur++ = '\\'; |
111 | *enc->cur++ = '\\'; |
101 | *enc->cur++ = '\\'; |
112 | *enc->cur++ = '\\'; |
102 | ++str; |
|
|
103 | } |
113 | } |
104 | else if (ch >= 0x20 && ch < 0x80) // most common case |
114 | else |
105 | { |
|
|
106 | *enc->cur++ = ch; |
115 | *enc->cur++ = ch; |
107 | ++str; |
116 | |
108 | } |
|
|
109 | else if (ch == '\015') |
|
|
110 | { |
|
|
111 | need (enc, len += 1); |
|
|
112 | *enc->cur++ = '\\'; |
|
|
113 | *enc->cur++ = 'r'; |
|
|
114 | ++str; |
|
|
115 | } |
|
|
116 | else if (ch == '\012') |
|
|
117 | { |
|
|
118 | need (enc, len += 1); |
|
|
119 | *enc->cur++ = '\\'; |
|
|
120 | *enc->cur++ = 'n'; |
|
|
121 | ++str; |
117 | ++str; |
122 | } |
118 | } |
123 | else |
119 | else |
124 | { |
120 | { |
125 | STRLEN clen; |
121 | switch (ch) |
126 | UV uch; |
|
|
127 | |
|
|
128 | if (is_utf8) |
|
|
129 | { |
122 | { |
130 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
123 | case '\010': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break; |
131 | if (clen == (STRLEN)-1) |
124 | case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break; |
132 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
125 | case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; |
133 | } |
126 | case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
134 | else |
127 | case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
135 | { |
|
|
136 | uch = ch; |
|
|
137 | clen = 1; |
|
|
138 | } |
|
|
139 | |
128 | |
140 | if (uch < 0x80 || enc->flags & F_ASCII) |
129 | default: |
141 | { |
|
|
142 | if (uch > 0xFFFFUL) |
|
|
143 | { |
130 | { |
|
|
131 | STRLEN clen; |
|
|
132 | UV uch; |
|
|
133 | |
|
|
134 | if (is_utf8) |
|
|
135 | { |
|
|
136 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
137 | if (clen == (STRLEN)-1) |
|
|
138 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
|
|
139 | } |
|
|
140 | else |
|
|
141 | { |
|
|
142 | uch = ch; |
|
|
143 | clen = 1; |
|
|
144 | } |
|
|
145 | |
|
|
146 | if (uch < 0x80 || enc->flags & F_ASCII) |
|
|
147 | { |
|
|
148 | if (uch > 0xFFFFUL) |
|
|
149 | { |
144 | need (enc, len += 11); |
150 | need (enc, len += 11); |
145 | sprintf (enc->cur, "\\u%04x\\u%04x", |
151 | sprintf (enc->cur, "\\u%04x\\u%04x", |
146 | (uch - 0x10000) / 0x400 + 0xD800, |
152 | (uch - 0x10000) / 0x400 + 0xD800, |
147 | (uch - 0x10000) % 0x400 + 0xDC00); |
153 | (uch - 0x10000) % 0x400 + 0xDC00); |
148 | enc->cur += 12; |
154 | enc->cur += 12; |
|
|
155 | } |
|
|
156 | else |
|
|
157 | { |
|
|
158 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
159 | need (enc, len += 5); |
|
|
160 | *enc->cur++ = '\\'; |
|
|
161 | *enc->cur++ = 'u'; |
|
|
162 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
|
|
163 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
|
|
164 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
|
|
165 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
|
|
166 | } |
|
|
167 | |
|
|
168 | str += clen; |
|
|
169 | } |
|
|
170 | else if (is_utf8) |
|
|
171 | { |
|
|
172 | need (enc, len += clen); |
|
|
173 | do |
|
|
174 | { |
|
|
175 | *enc->cur++ = *str++; |
|
|
176 | } |
|
|
177 | while (--clen); |
|
|
178 | } |
|
|
179 | else |
|
|
180 | { |
|
|
181 | need (enc, len += 10); // never more than 11 bytes needed |
|
|
182 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
183 | ++str; |
|
|
184 | } |
149 | } |
185 | } |
150 | else |
|
|
151 | { |
|
|
152 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
153 | need (enc, len += 5); |
|
|
154 | *enc->cur++ = '\\'; |
|
|
155 | *enc->cur++ = 'u'; |
|
|
156 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
|
|
157 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
|
|
158 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
|
|
159 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
|
|
160 | } |
|
|
161 | |
|
|
162 | str += clen; |
|
|
163 | } |
|
|
164 | else if (is_utf8) |
|
|
165 | { |
|
|
166 | need (enc, len += clen); |
|
|
167 | do |
|
|
168 | { |
|
|
169 | *enc->cur++ = *str++; |
|
|
170 | } |
|
|
171 | while (--clen); |
|
|
172 | } |
|
|
173 | else |
|
|
174 | { |
|
|
175 | need (enc, 10); // never more than 11 bytes needed |
|
|
176 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
177 | ++str; |
|
|
178 | } |
186 | } |
179 | } |
187 | } |
180 | |
188 | |
181 | --len; |
189 | --len; |
182 | } |
190 | } |
… | |
… | |
425 | |
433 | |
426 | if (!(flags & (F_ASCII | F_UTF8))) |
434 | if (!(flags & (F_ASCII | F_UTF8))) |
427 | SvUTF8_on (enc.sv); |
435 | SvUTF8_on (enc.sv); |
428 | |
436 | |
429 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
437 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
438 | |
|
|
439 | if (enc.flags & F_SHRINK) |
|
|
440 | shrink (enc.sv); |
|
|
441 | |
430 | return enc.sv; |
442 | return enc.sv; |
431 | } |
443 | } |
432 | |
444 | |
433 | ///////////////////////////////////////////////////////////////////////////// |
445 | ///////////////////////////////////////////////////////////////////////////// |
434 | |
446 | |
… | |
… | |
572 | else if (ch >= 0x80) |
584 | else if (ch >= 0x80) |
573 | { |
585 | { |
574 | STRLEN clen; |
586 | STRLEN clen; |
575 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
587 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
576 | if (clen == (STRLEN)-1) |
588 | if (clen == (STRLEN)-1) |
577 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
589 | ERR ("malformed UTF-8 character in JSON string"); |
578 | |
590 | |
579 | APPEND_GROW (clen); |
591 | APPEND_GROW (clen); |
580 | do |
592 | do |
581 | { |
593 | { |
582 | *cur++ = *dec->cur++; |
594 | *cur++ = *dec->cur++; |
… | |
… | |
598 | SvPOK_only (sv); |
610 | SvPOK_only (sv); |
599 | *SvEND (sv) = 0; |
611 | *SvEND (sv) = 0; |
600 | |
612 | |
601 | if (utf8) |
613 | if (utf8) |
602 | SvUTF8_on (sv); |
614 | SvUTF8_on (sv); |
|
|
615 | |
|
|
616 | if (dec->flags & F_SHRINK) |
|
|
617 | shrink (sv); |
603 | |
618 | |
604 | return sv; |
619 | return sv; |
605 | |
620 | |
606 | fail: |
621 | fail: |
607 | SvREFCNT_dec (sv); |
622 | SvREFCNT_dec (sv); |
… | |
… | |
829 | ERR ("'null' expected"); |
844 | ERR ("'null' expected"); |
830 | |
845 | |
831 | break; |
846 | break; |
832 | |
847 | |
833 | default: |
848 | default: |
834 | ERR ("malformed json string"); |
849 | ERR ("malformed json string, neither array, object, number, string or atom"); |
835 | break; |
850 | break; |
836 | } |
851 | } |
837 | |
852 | |
838 | fail: |
853 | fail: |
839 | return 0; |
854 | return 0; |
… | |
… | |
859 | |
874 | |
860 | sv = decode_sv (&dec); |
875 | sv = decode_sv (&dec); |
861 | |
876 | |
862 | if (!sv) |
877 | if (!sv) |
863 | { |
878 | { |
|
|
879 | IV offset = dec.flags & F_UTF8 |
|
|
880 | ? dec.cur - SvPVX (string) |
864 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
881 | : utf8_distance (dec.cur, SvPVX (string)); |
865 | SV *uni = sv_newmortal (); |
882 | SV *uni = sv_newmortal (); |
866 | // horrible hack to silence warning inside pv_uni_display |
883 | // horrible hack to silence warning inside pv_uni_display |
867 | COP cop; |
884 | COP cop; |
868 | memset (&cop, 0, sizeof (cop)); |
885 | memset (&cop, 0, sizeof (cop)); |
869 | cop.cop_warnings = pWARN_NONE; |
886 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
910 | CODE: |
927 | CODE: |
911 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
928 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
912 | OUTPUT: |
929 | OUTPUT: |
913 | RETVAL |
930 | RETVAL |
914 | |
931 | |
915 | SV *ascii (SV *self, int enable) |
932 | SV *ascii (SV *self, int enable = 1) |
916 | ALIAS: |
933 | ALIAS: |
917 | ascii = F_ASCII |
934 | ascii = F_ASCII |
918 | utf8 = F_UTF8 |
935 | utf8 = F_UTF8 |
919 | indent = F_INDENT |
936 | indent = F_INDENT |
920 | canonical = F_CANONICAL |
937 | canonical = F_CANONICAL |
921 | space_before = F_SPACE_BEFORE |
938 | space_before = F_SPACE_BEFORE |
922 | space_after = F_SPACE_AFTER |
939 | space_after = F_SPACE_AFTER |
923 | json_rpc = F_JSON_RPC |
940 | json_rpc = F_JSON_RPC |
924 | pretty = F_PRETTY |
941 | pretty = F_PRETTY |
925 | allow_nonref = F_ALLOW_NONREF |
942 | allow_nonref = F_ALLOW_NONREF |
|
|
943 | shrink = F_SHRINK |
926 | CODE: |
944 | CODE: |
927 | { |
945 | { |
928 | UV *uv = SvJSON (self); |
946 | UV *uv = SvJSON (self); |
929 | if (enable) |
947 | if (enable) |
930 | *uv |= ix; |
948 | *uv |= ix; |