… | |
… | |
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 | { |
… | |
… | |
317 | |
320 | |
318 | if (fast) |
321 | if (fast) |
319 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
322 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
320 | else |
323 | else |
321 | { |
324 | { |
322 | // hack to disable "use bytes" |
325 | // hack to forcefully disable "use bytes" |
323 | COP *oldcop = PL_curcop, cop; |
326 | COP cop = *PL_curcop; |
324 | cop.op_private = 0; |
327 | cop.op_private = 0; |
|
|
328 | |
|
|
329 | ENTER; |
|
|
330 | SAVETMPS; |
|
|
331 | |
|
|
332 | SAVEVPTR (PL_curcop); |
325 | PL_curcop = &cop; |
333 | PL_curcop = &cop; |
326 | |
334 | |
327 | SAVETMPS; |
|
|
328 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
335 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
336 | |
329 | FREETMPS; |
337 | FREETMPS; |
330 | |
338 | LEAVE; |
331 | PL_curcop = oldcop; |
|
|
332 | } |
339 | } |
333 | |
340 | |
334 | for (i = 0; i < count; ++i) |
341 | for (i = 0; i < count; ++i) |
335 | { |
342 | { |
336 | INDENT; |
343 | INDENT; |
… | |
… | |
392 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
399 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
393 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
400 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
394 | } |
401 | } |
395 | else if (SvROK (sv)) |
402 | else if (SvROK (sv)) |
396 | { |
403 | { |
|
|
404 | SV *rv = SvRV (sv); |
|
|
405 | |
397 | if (!--enc->max_recurse) |
406 | if (!--enc->max_recurse) |
398 | croak ("data structure too deep (hit recursion limit)"); |
407 | croak ("data structure too deep (hit recursion limit)"); |
399 | |
408 | |
400 | sv = SvRV (sv); |
|
|
401 | |
|
|
402 | switch (SvTYPE (sv)) |
409 | switch (SvTYPE (rv)) |
403 | { |
410 | { |
404 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
411 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
405 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
412 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
406 | |
413 | |
407 | default: |
414 | default: |
408 | 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)); |
409 | } |
417 | } |
410 | } |
418 | } |
411 | else if (!SvOK (sv)) |
419 | else if (!SvOK (sv)) |
412 | encode_str (enc, "null", 4, 0); |
420 | encode_str (enc, "null", 4, 0); |
413 | else |
421 | else |
414 | 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)); |
415 | } |
424 | } |
416 | |
425 | |
417 | static SV * |
426 | static SV * |
418 | encode_json (SV *scalar, UV flags) |
427 | encode_json (SV *scalar, UV flags) |
419 | { |
428 | { |
420 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
429 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
421 | 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)"); |
422 | |
431 | |
423 | enc_t enc; |
432 | enc_t enc; |
424 | enc.flags = flags; |
433 | enc.flags = flags; |
425 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
434 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
426 | enc.cur = SvPVX (enc.sv); |
435 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
878 | { |
887 | { |
879 | IV offset = dec.flags & F_UTF8 |
888 | IV offset = dec.flags & F_UTF8 |
880 | ? dec.cur - SvPVX (string) |
889 | ? dec.cur - SvPVX (string) |
881 | : utf8_distance (dec.cur, SvPVX (string)); |
890 | : utf8_distance (dec.cur, SvPVX (string)); |
882 | SV *uni = sv_newmortal (); |
891 | SV *uni = sv_newmortal (); |
|
|
892 | |
883 | // horrible hack to silence warning inside pv_uni_display |
893 | // horrible hack to silence warning inside pv_uni_display |
884 | COP cop; |
894 | COP cop = *PL_curcop; |
885 | memset (&cop, 0, sizeof (cop)); |
|
|
886 | cop.cop_warnings = pWARN_NONE; |
895 | cop.cop_warnings = pWARN_NONE; |
|
|
896 | ENTER; |
887 | SAVEVPTR (PL_curcop); |
897 | SAVEVPTR (PL_curcop); |
888 | PL_curcop = &cop; |
898 | PL_curcop = &cop; |
889 | |
|
|
890 | 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 | |
891 | croak ("%s, at character offset %d (%s)", |
902 | croak ("%s, at character offset %d (%s)", |
892 | dec.err, |
903 | dec.err, |
893 | (int)offset, |
904 | (int)offset, |
894 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
905 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
895 | } |
906 | } |
896 | |
907 | |
897 | sv = sv_2mortal (sv); |
908 | sv = sv_2mortal (sv); |
898 | |
909 | |
899 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
910 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
900 | 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)"); |
901 | |
912 | |
902 | return sv; |
913 | return sv; |
903 | } |
914 | } |
904 | |
915 | |
905 | MODULE = JSON::XS PACKAGE = JSON::XS |
916 | MODULE = JSON::XS PACKAGE = JSON::XS |