… | |
… | |
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 or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
|
|
139 | } |
|
|
140 | else |
|
|
141 | { |
|
|
142 | uch = ch; |
|
|
143 | clen = 1; |
|
|
144 | } |
|
|
145 | |
|
|
146 | if (uch > 0x10FFFFUL) |
|
|
147 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
148 | |
|
|
149 | if (uch < 0x80 || enc->flags & F_ASCII) |
|
|
150 | { |
|
|
151 | if (uch > 0xFFFFUL) |
|
|
152 | { |
144 | need (enc, len += 11); |
153 | need (enc, len += 11); |
145 | sprintf (enc->cur, "\\u%04x\\u%04x", |
154 | sprintf (enc->cur, "\\u%04x\\u%04x", |
146 | (uch - 0x10000) / 0x400 + 0xD800, |
155 | (uch - 0x10000) / 0x400 + 0xD800, |
147 | (uch - 0x10000) % 0x400 + 0xDC00); |
156 | (uch - 0x10000) % 0x400 + 0xDC00); |
148 | enc->cur += 12; |
157 | enc->cur += 12; |
|
|
158 | } |
|
|
159 | else |
|
|
160 | { |
|
|
161 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
162 | need (enc, len += 5); |
|
|
163 | *enc->cur++ = '\\'; |
|
|
164 | *enc->cur++ = 'u'; |
|
|
165 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
|
|
166 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
|
|
167 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
|
|
168 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
|
|
169 | } |
|
|
170 | |
|
|
171 | str += clen; |
|
|
172 | } |
|
|
173 | else if (is_utf8) |
|
|
174 | { |
|
|
175 | need (enc, len += clen); |
|
|
176 | do |
|
|
177 | { |
|
|
178 | *enc->cur++ = *str++; |
|
|
179 | } |
|
|
180 | while (--clen); |
|
|
181 | } |
|
|
182 | else |
|
|
183 | { |
|
|
184 | need (enc, len += 10); // never more than 11 bytes needed |
|
|
185 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
186 | ++str; |
|
|
187 | } |
149 | } |
188 | } |
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 | } |
189 | } |
179 | } |
190 | } |
180 | |
191 | |
181 | --len; |
192 | --len; |
182 | } |
193 | } |
… | |
… | |
309 | |
320 | |
310 | if (fast) |
321 | if (fast) |
311 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
322 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
312 | else |
323 | else |
313 | { |
324 | { |
314 | // hack to disable "use bytes" |
325 | // hack to forcefully disable "use bytes" |
315 | COP *oldcop = PL_curcop, cop; |
326 | COP cop = *PL_curcop; |
316 | cop.op_private = 0; |
327 | cop.op_private = 0; |
|
|
328 | |
|
|
329 | ENTER; |
|
|
330 | SAVETMPS; |
|
|
331 | |
|
|
332 | SAVEVPTR (PL_curcop); |
317 | PL_curcop = &cop; |
333 | PL_curcop = &cop; |
318 | |
334 | |
319 | SAVETMPS; |
|
|
320 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
335 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
336 | |
321 | FREETMPS; |
337 | FREETMPS; |
322 | |
338 | LEAVE; |
323 | PL_curcop = oldcop; |
|
|
324 | } |
339 | } |
325 | |
340 | |
326 | for (i = 0; i < count; ++i) |
341 | for (i = 0; i < count; ++i) |
327 | { |
342 | { |
328 | INDENT; |
343 | INDENT; |
… | |
… | |
384 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
399 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
385 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
400 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
386 | } |
401 | } |
387 | else if (SvROK (sv)) |
402 | else if (SvROK (sv)) |
388 | { |
403 | { |
|
|
404 | SV *rv = SvRV (sv); |
|
|
405 | |
389 | if (!--enc->max_recurse) |
406 | if (!--enc->max_recurse) |
390 | croak ("data structure too deep (hit recursion limit)"); |
407 | croak ("data structure too deep (hit recursion limit)"); |
391 | |
408 | |
392 | sv = SvRV (sv); |
|
|
393 | |
|
|
394 | switch (SvTYPE (sv)) |
409 | switch (SvTYPE (rv)) |
395 | { |
410 | { |
396 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
411 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
397 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
412 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
398 | |
413 | |
399 | default: |
414 | default: |
400 | croak ("JSON can only represent references to arrays or hashes"); |
415 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
416 | SvPV_nolen (sv)); |
401 | } |
417 | } |
402 | } |
418 | } |
403 | else if (!SvOK (sv)) |
419 | else if (!SvOK (sv)) |
404 | encode_str (enc, "null", 4, 0); |
420 | encode_str (enc, "null", 4, 0); |
405 | else |
421 | else |
406 | croak ("encountered perl type that JSON cannot handle"); |
422 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
|
|
423 | SvPV_nolen (sv), SvFLAGS (sv)); |
407 | } |
424 | } |
408 | |
425 | |
409 | static SV * |
426 | static SV * |
410 | encode_json (SV *scalar, UV flags) |
427 | encode_json (SV *scalar, UV flags) |
411 | { |
428 | { |
412 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
429 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
413 | croak ("hash- or arraref required (not a simple scalar, use allow_nonref to allow this)"); |
430 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
414 | |
431 | |
415 | enc_t enc; |
432 | enc_t enc; |
416 | enc.flags = flags; |
433 | enc.flags = flags; |
417 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
434 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
418 | enc.cur = SvPVX (enc.sv); |
435 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
425 | |
442 | |
426 | if (!(flags & (F_ASCII | F_UTF8))) |
443 | if (!(flags & (F_ASCII | F_UTF8))) |
427 | SvUTF8_on (enc.sv); |
444 | SvUTF8_on (enc.sv); |
428 | |
445 | |
429 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
446 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
447 | |
|
|
448 | if (enc.flags & F_SHRINK) |
|
|
449 | shrink (enc.sv); |
|
|
450 | |
430 | return enc.sv; |
451 | return enc.sv; |
431 | } |
452 | } |
432 | |
453 | |
433 | ///////////////////////////////////////////////////////////////////////////// |
454 | ///////////////////////////////////////////////////////////////////////////// |
434 | |
455 | |
… | |
… | |
572 | else if (ch >= 0x80) |
593 | else if (ch >= 0x80) |
573 | { |
594 | { |
574 | STRLEN clen; |
595 | STRLEN clen; |
575 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
596 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
576 | if (clen == (STRLEN)-1) |
597 | if (clen == (STRLEN)-1) |
577 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
598 | ERR ("malformed UTF-8 character in JSON string"); |
578 | |
599 | |
579 | APPEND_GROW (clen); |
600 | APPEND_GROW (clen); |
580 | do |
601 | do |
581 | { |
602 | { |
582 | *cur++ = *dec->cur++; |
603 | *cur++ = *dec->cur++; |
… | |
… | |
598 | SvPOK_only (sv); |
619 | SvPOK_only (sv); |
599 | *SvEND (sv) = 0; |
620 | *SvEND (sv) = 0; |
600 | |
621 | |
601 | if (utf8) |
622 | if (utf8) |
602 | SvUTF8_on (sv); |
623 | SvUTF8_on (sv); |
|
|
624 | |
|
|
625 | if (dec->flags & F_SHRINK) |
|
|
626 | shrink (sv); |
603 | |
627 | |
604 | return sv; |
628 | return sv; |
605 | |
629 | |
606 | fail: |
630 | fail: |
607 | SvREFCNT_dec (sv); |
631 | SvREFCNT_dec (sv); |
… | |
… | |
829 | ERR ("'null' expected"); |
853 | ERR ("'null' expected"); |
830 | |
854 | |
831 | break; |
855 | break; |
832 | |
856 | |
833 | default: |
857 | default: |
834 | ERR ("malformed json string"); |
858 | ERR ("malformed json string, neither array, object, number, string or atom"); |
835 | break; |
859 | break; |
836 | } |
860 | } |
837 | |
861 | |
838 | fail: |
862 | fail: |
839 | return 0; |
863 | return 0; |
… | |
… | |
859 | |
883 | |
860 | sv = decode_sv (&dec); |
884 | sv = decode_sv (&dec); |
861 | |
885 | |
862 | if (!sv) |
886 | if (!sv) |
863 | { |
887 | { |
|
|
888 | IV offset = dec.flags & F_UTF8 |
|
|
889 | ? dec.cur - SvPVX (string) |
864 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
890 | : utf8_distance (dec.cur, SvPVX (string)); |
865 | SV *uni = sv_newmortal (); |
891 | SV *uni = sv_newmortal (); |
|
|
892 | |
866 | // horrible hack to silence warning inside pv_uni_display |
893 | // horrible hack to silence warning inside pv_uni_display |
867 | COP cop; |
894 | COP cop = *PL_curcop; |
868 | memset (&cop, 0, sizeof (cop)); |
|
|
869 | cop.cop_warnings = pWARN_NONE; |
895 | cop.cop_warnings = pWARN_NONE; |
|
|
896 | ENTER; |
870 | SAVEVPTR (PL_curcop); |
897 | SAVEVPTR (PL_curcop); |
871 | PL_curcop = &cop; |
898 | PL_curcop = &cop; |
872 | |
|
|
873 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
899 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
900 | LEAVE; |
|
|
901 | |
874 | croak ("%s, at character offset %d (%s)", |
902 | croak ("%s, at character offset %d (%s)", |
875 | dec.err, |
903 | dec.err, |
876 | (int)offset, |
904 | (int)offset, |
877 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
905 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
878 | } |
906 | } |
879 | |
907 | |
880 | sv = sv_2mortal (sv); |
908 | sv = sv_2mortal (sv); |
881 | |
909 | |
882 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
910 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
883 | croak ("JSON object or array expected (but number, string, true, false or null found, use allow_nonref to allow this)"); |
911 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
884 | |
912 | |
885 | return sv; |
913 | return sv; |
886 | } |
914 | } |
887 | |
915 | |
888 | MODULE = JSON::XS PACKAGE = JSON::XS |
916 | MODULE = JSON::XS PACKAGE = JSON::XS |
… | |
… | |
910 | CODE: |
938 | CODE: |
911 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
939 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
912 | OUTPUT: |
940 | OUTPUT: |
913 | RETVAL |
941 | RETVAL |
914 | |
942 | |
915 | SV *ascii (SV *self, int enable) |
943 | SV *ascii (SV *self, int enable = 1) |
916 | ALIAS: |
944 | ALIAS: |
917 | ascii = F_ASCII |
945 | ascii = F_ASCII |
918 | utf8 = F_UTF8 |
946 | utf8 = F_UTF8 |
919 | indent = F_INDENT |
947 | indent = F_INDENT |
920 | canonical = F_CANONICAL |
948 | canonical = F_CANONICAL |
921 | space_before = F_SPACE_BEFORE |
949 | space_before = F_SPACE_BEFORE |
922 | space_after = F_SPACE_AFTER |
950 | space_after = F_SPACE_AFTER |
923 | json_rpc = F_JSON_RPC |
951 | json_rpc = F_JSON_RPC |
924 | pretty = F_PRETTY |
952 | pretty = F_PRETTY |
925 | allow_nonref = F_ALLOW_NONREF |
953 | allow_nonref = F_ALLOW_NONREF |
|
|
954 | shrink = F_SHRINK |
926 | CODE: |
955 | CODE: |
927 | { |
956 | { |
928 | UV *uv = SvJSON (self); |
957 | UV *uv = SvJSON (self); |
929 | if (enable) |
958 | if (enable) |
930 | *uv |= ix; |
959 | *uv |= ix; |