… | |
… | |
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 | } |
… | |
… | |
309 | |
317 | |
310 | if (fast) |
318 | if (fast) |
311 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
319 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
312 | else |
320 | else |
313 | { |
321 | { |
314 | // hack to disable "use bytes" |
322 | // hack to forcefully disable "use bytes" |
315 | COP *oldcop = PL_curcop, cop; |
323 | COP cop = *PL_curcop; |
316 | cop.op_private = 0; |
324 | cop.op_private = 0; |
|
|
325 | |
|
|
326 | ENTER; |
|
|
327 | SAVETMPS; |
|
|
328 | |
|
|
329 | SAVEVPTR (PL_curcop); |
317 | PL_curcop = &cop; |
330 | PL_curcop = &cop; |
318 | |
331 | |
319 | SAVETMPS; |
|
|
320 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
332 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
333 | |
321 | FREETMPS; |
334 | FREETMPS; |
322 | |
335 | LEAVE; |
323 | PL_curcop = oldcop; |
|
|
324 | } |
336 | } |
325 | |
337 | |
326 | for (i = 0; i < count; ++i) |
338 | for (i = 0; i < count; ++i) |
327 | { |
339 | { |
328 | INDENT; |
340 | INDENT; |
… | |
… | |
425 | |
437 | |
426 | if (!(flags & (F_ASCII | F_UTF8))) |
438 | if (!(flags & (F_ASCII | F_UTF8))) |
427 | SvUTF8_on (enc.sv); |
439 | SvUTF8_on (enc.sv); |
428 | |
440 | |
429 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
441 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
442 | |
|
|
443 | if (enc.flags & F_SHRINK) |
|
|
444 | shrink (enc.sv); |
|
|
445 | |
430 | return enc.sv; |
446 | return enc.sv; |
431 | } |
447 | } |
432 | |
448 | |
433 | ///////////////////////////////////////////////////////////////////////////// |
449 | ///////////////////////////////////////////////////////////////////////////// |
434 | |
450 | |
… | |
… | |
572 | else if (ch >= 0x80) |
588 | else if (ch >= 0x80) |
573 | { |
589 | { |
574 | STRLEN clen; |
590 | STRLEN clen; |
575 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
591 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
576 | if (clen == (STRLEN)-1) |
592 | if (clen == (STRLEN)-1) |
577 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
593 | ERR ("malformed UTF-8 character in JSON string"); |
578 | |
594 | |
579 | APPEND_GROW (clen); |
595 | APPEND_GROW (clen); |
580 | do |
596 | do |
581 | { |
597 | { |
582 | *cur++ = *dec->cur++; |
598 | *cur++ = *dec->cur++; |
… | |
… | |
598 | SvPOK_only (sv); |
614 | SvPOK_only (sv); |
599 | *SvEND (sv) = 0; |
615 | *SvEND (sv) = 0; |
600 | |
616 | |
601 | if (utf8) |
617 | if (utf8) |
602 | SvUTF8_on (sv); |
618 | SvUTF8_on (sv); |
|
|
619 | |
|
|
620 | if (dec->flags & F_SHRINK) |
|
|
621 | shrink (sv); |
603 | |
622 | |
604 | return sv; |
623 | return sv; |
605 | |
624 | |
606 | fail: |
625 | fail: |
607 | SvREFCNT_dec (sv); |
626 | SvREFCNT_dec (sv); |
… | |
… | |
829 | ERR ("'null' expected"); |
848 | ERR ("'null' expected"); |
830 | |
849 | |
831 | break; |
850 | break; |
832 | |
851 | |
833 | default: |
852 | default: |
834 | ERR ("malformed json string"); |
853 | ERR ("malformed json string, neither array, object, number, string or atom"); |
835 | break; |
854 | break; |
836 | } |
855 | } |
837 | |
856 | |
838 | fail: |
857 | fail: |
839 | return 0; |
858 | return 0; |
… | |
… | |
859 | |
878 | |
860 | sv = decode_sv (&dec); |
879 | sv = decode_sv (&dec); |
861 | |
880 | |
862 | if (!sv) |
881 | if (!sv) |
863 | { |
882 | { |
|
|
883 | IV offset = dec.flags & F_UTF8 |
|
|
884 | ? dec.cur - SvPVX (string) |
864 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
885 | : utf8_distance (dec.cur, SvPVX (string)); |
865 | SV *uni = sv_newmortal (); |
886 | SV *uni = sv_newmortal (); |
|
|
887 | |
866 | // horrible hack to silence warning inside pv_uni_display |
888 | // horrible hack to silence warning inside pv_uni_display |
867 | COP cop; |
889 | COP cop = *PL_curcop; |
868 | memset (&cop, 0, sizeof (cop)); |
|
|
869 | cop.cop_warnings = pWARN_NONE; |
890 | cop.cop_warnings = pWARN_NONE; |
|
|
891 | ENTER; |
870 | SAVEVPTR (PL_curcop); |
892 | SAVEVPTR (PL_curcop); |
871 | PL_curcop = &cop; |
893 | PL_curcop = &cop; |
872 | |
|
|
873 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
894 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
895 | LEAVE; |
|
|
896 | |
874 | croak ("%s, at character offset %d (%s)", |
897 | croak ("%s, at character offset %d (%s)", |
875 | dec.err, |
898 | dec.err, |
876 | (int)offset, |
899 | (int)offset, |
877 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
900 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
878 | } |
901 | } |
… | |
… | |
910 | CODE: |
933 | CODE: |
911 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
934 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
912 | OUTPUT: |
935 | OUTPUT: |
913 | RETVAL |
936 | RETVAL |
914 | |
937 | |
915 | SV *ascii (SV *self, int enable) |
938 | SV *ascii (SV *self, int enable = 1) |
916 | ALIAS: |
939 | ALIAS: |
917 | ascii = F_ASCII |
940 | ascii = F_ASCII |
918 | utf8 = F_UTF8 |
941 | utf8 = F_UTF8 |
919 | indent = F_INDENT |
942 | indent = F_INDENT |
920 | canonical = F_CANONICAL |
943 | canonical = F_CANONICAL |
921 | space_before = F_SPACE_BEFORE |
944 | space_before = F_SPACE_BEFORE |
922 | space_after = F_SPACE_AFTER |
945 | space_after = F_SPACE_AFTER |
923 | json_rpc = F_JSON_RPC |
946 | json_rpc = F_JSON_RPC |
924 | pretty = F_PRETTY |
947 | pretty = F_PRETTY |
925 | allow_nonref = F_ALLOW_NONREF |
948 | allow_nonref = F_ALLOW_NONREF |
|
|
949 | shrink = F_SHRINK |
926 | CODE: |
950 | CODE: |
927 | { |
951 | { |
928 | UV *uv = SvJSON (self); |
952 | UV *uv = SvJSON (self); |
929 | if (enable) |
953 | if (enable) |
930 | *uv |= ix; |
954 | *uv |= ix; |