… | |
… | |
10 | #define F_UTF8 0x00000002 |
10 | #define F_UTF8 0x00000002 |
11 | #define F_INDENT 0x00000004 |
11 | #define F_INDENT 0x00000004 |
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 |
|
|
16 | #define F_ALLOW_NONREF 0x00000080 |
15 | #define F_ALLOW_NONREF 0x00000080 |
17 | #define F_SHRINK 0x00000100 |
16 | #define F_SHRINK 0x00000100 |
18 | |
17 | |
19 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
20 | #define F_DEFAULT 0 |
19 | #define F_DEFAULT 0 |
… | |
… | |
133 | |
132 | |
134 | if (is_utf8) |
133 | if (is_utf8) |
135 | { |
134 | { |
136 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
135 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
137 | if (clen == (STRLEN)-1) |
136 | if (clen == (STRLEN)-1) |
138 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
137 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
139 | } |
138 | } |
140 | else |
139 | else |
141 | { |
140 | { |
142 | uch = ch; |
141 | uch = ch; |
143 | clen = 1; |
142 | clen = 1; |
144 | } |
143 | } |
145 | |
144 | |
|
|
145 | if (uch > 0x10FFFFUL) |
|
|
146 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
147 | |
146 | if (uch < 0x80 || enc->flags & F_ASCII) |
148 | if (uch < 0x80 || enc->flags & F_ASCII) |
147 | { |
149 | { |
148 | if (uch > 0xFFFFUL) |
150 | if (uch > 0xFFFFUL) |
149 | { |
151 | { |
150 | need (enc, len += 11); |
152 | need (enc, len += 11); |
151 | sprintf (enc->cur, "\\u%04x\\u%04x", |
153 | sprintf (enc->cur, "\\u%04x\\u%04x", |
152 | (uch - 0x10000) / 0x400 + 0xD800, |
154 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
153 | (uch - 0x10000) % 0x400 + 0xDC00); |
155 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
154 | enc->cur += 12; |
156 | enc->cur += 12; |
155 | } |
157 | } |
156 | else |
158 | else |
157 | { |
159 | { |
158 | static char hexdigit [16] = "0123456789abcdef"; |
160 | static char hexdigit [16] = "0123456789abcdef"; |
… | |
… | |
396 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
398 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
397 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
399 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
398 | } |
400 | } |
399 | else if (SvROK (sv)) |
401 | else if (SvROK (sv)) |
400 | { |
402 | { |
|
|
403 | SV *rv = SvRV (sv); |
|
|
404 | |
401 | if (!--enc->max_recurse) |
405 | if (!--enc->max_recurse) |
402 | croak ("data structure too deep (hit recursion limit)"); |
406 | croak ("data structure too deep (hit recursion limit)"); |
403 | |
407 | |
404 | sv = SvRV (sv); |
|
|
405 | |
|
|
406 | switch (SvTYPE (sv)) |
408 | switch (SvTYPE (rv)) |
407 | { |
409 | { |
408 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
410 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
409 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
411 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
410 | |
412 | |
411 | default: |
413 | default: |
412 | croak ("JSON can only represent references to arrays or hashes"); |
414 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
415 | SvPV_nolen (sv)); |
413 | } |
416 | } |
414 | } |
417 | } |
415 | else if (!SvOK (sv)) |
418 | else if (!SvOK (sv)) |
416 | encode_str (enc, "null", 4, 0); |
419 | encode_str (enc, "null", 4, 0); |
417 | else |
420 | else |
418 | croak ("encountered perl type that JSON cannot handle"); |
421 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
|
|
422 | SvPV_nolen (sv), SvFLAGS (sv)); |
419 | } |
423 | } |
420 | |
424 | |
421 | static SV * |
425 | static SV * |
422 | encode_json (SV *scalar, UV flags) |
426 | encode_json (SV *scalar, UV flags) |
423 | { |
427 | { |
424 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
428 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
425 | croak ("hash- or arraref required (not a simple scalar, use allow_nonref to allow this)"); |
429 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
426 | |
430 | |
427 | enc_t enc; |
431 | enc_t enc; |
428 | enc.flags = flags; |
432 | enc.flags = flags; |
429 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
433 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
430 | enc.cur = SvPVX (enc.sv); |
434 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
901 | } |
905 | } |
902 | |
906 | |
903 | sv = sv_2mortal (sv); |
907 | sv = sv_2mortal (sv); |
904 | |
908 | |
905 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
909 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
906 | croak ("JSON object or array expected (but number, string, true, false or null found, use allow_nonref to allow this)"); |
910 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
907 | |
911 | |
908 | return sv; |
912 | return sv; |
909 | } |
913 | } |
910 | |
914 | |
911 | MODULE = JSON::XS PACKAGE = JSON::XS |
915 | MODULE = JSON::XS PACKAGE = JSON::XS |
… | |
… | |
941 | utf8 = F_UTF8 |
945 | utf8 = F_UTF8 |
942 | indent = F_INDENT |
946 | indent = F_INDENT |
943 | canonical = F_CANONICAL |
947 | canonical = F_CANONICAL |
944 | space_before = F_SPACE_BEFORE |
948 | space_before = F_SPACE_BEFORE |
945 | space_after = F_SPACE_AFTER |
949 | space_after = F_SPACE_AFTER |
946 | json_rpc = F_JSON_RPC |
|
|
947 | pretty = F_PRETTY |
950 | pretty = F_PRETTY |
948 | allow_nonref = F_ALLOW_NONREF |
951 | allow_nonref = F_ALLOW_NONREF |
949 | shrink = F_SHRINK |
952 | shrink = F_SHRINK |
950 | CODE: |
953 | CODE: |
951 | { |
954 | { |