… | |
… | |
133 | |
133 | |
134 | if (is_utf8) |
134 | if (is_utf8) |
135 | { |
135 | { |
136 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
136 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
137 | if (clen == (STRLEN)-1) |
137 | if (clen == (STRLEN)-1) |
138 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
138 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
139 | } |
139 | } |
140 | else |
140 | else |
141 | { |
141 | { |
142 | uch = ch; |
142 | uch = ch; |
143 | clen = 1; |
143 | clen = 1; |
144 | } |
144 | } |
|
|
145 | |
|
|
146 | if (uch > 0x10FFFFUL) |
|
|
147 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
145 | |
148 | |
146 | if (uch < 0x80 || enc->flags & F_ASCII) |
149 | if (uch < 0x80 || enc->flags & F_ASCII) |
147 | { |
150 | { |
148 | if (uch > 0xFFFFUL) |
151 | if (uch > 0xFFFFUL) |
149 | { |
152 | { |
… | |
… | |
396 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
399 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
397 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
400 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
398 | } |
401 | } |
399 | else if (SvROK (sv)) |
402 | else if (SvROK (sv)) |
400 | { |
403 | { |
|
|
404 | SV *rv = SvRV (sv); |
|
|
405 | |
401 | if (!--enc->max_recurse) |
406 | if (!--enc->max_recurse) |
402 | croak ("data structure too deep (hit recursion limit)"); |
407 | croak ("data structure too deep (hit recursion limit)"); |
403 | |
408 | |
404 | sv = SvRV (sv); |
|
|
405 | |
|
|
406 | switch (SvTYPE (sv)) |
409 | switch (SvTYPE (rv)) |
407 | { |
410 | { |
408 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
411 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
409 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
412 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
410 | |
413 | |
411 | default: |
414 | default: |
412 | 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)); |
413 | } |
417 | } |
414 | } |
418 | } |
415 | else if (!SvOK (sv)) |
419 | else if (!SvOK (sv)) |
416 | encode_str (enc, "null", 4, 0); |
420 | encode_str (enc, "null", 4, 0); |
417 | else |
421 | else |
418 | 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)); |
419 | } |
424 | } |
420 | |
425 | |
421 | static SV * |
426 | static SV * |
422 | encode_json (SV *scalar, UV flags) |
427 | encode_json (SV *scalar, UV flags) |
423 | { |
428 | { |
424 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
429 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
425 | 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)"); |
426 | |
431 | |
427 | enc_t enc; |
432 | enc_t enc; |
428 | enc.flags = flags; |
433 | enc.flags = flags; |
429 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
434 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
430 | enc.cur = SvPVX (enc.sv); |
435 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
901 | } |
906 | } |
902 | |
907 | |
903 | sv = sv_2mortal (sv); |
908 | sv = sv_2mortal (sv); |
904 | |
909 | |
905 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
910 | 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)"); |
911 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
907 | |
912 | |
908 | return sv; |
913 | return sv; |
909 | } |
914 | } |
910 | |
915 | |
911 | MODULE = JSON::XS PACKAGE = JSON::XS |
916 | MODULE = JSON::XS PACKAGE = JSON::XS |