… | |
… | |
52 | { |
52 | { |
53 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
53 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
54 | croak ("object is not of type JSON::XS"); |
54 | croak ("object is not of type JSON::XS"); |
55 | |
55 | |
56 | return &SvUVX (SvRV (sv)); |
56 | return &SvUVX (SvRV (sv)); |
|
|
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 |
57 | } |
66 | } |
58 | |
67 | |
59 | ///////////////////////////////////////////////////////////////////////////// |
68 | ///////////////////////////////////////////////////////////////////////////// |
60 | |
69 | |
61 | static void |
70 | static void |
… | |
… | |
167 | } |
176 | } |
168 | while (--clen); |
177 | while (--clen); |
169 | } |
178 | } |
170 | else |
179 | else |
171 | { |
180 | { |
172 | need (enc, 10); // never more than 11 bytes needed |
181 | need (enc, len += 10); // never more than 11 bytes needed |
173 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
182 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
174 | ++str; |
183 | ++str; |
175 | } |
184 | } |
176 | } |
185 | } |
177 | } |
186 | } |
… | |
… | |
308 | |
317 | |
309 | if (fast) |
318 | if (fast) |
310 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
319 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
311 | else |
320 | else |
312 | { |
321 | { |
313 | // hack to disable "use bytes" |
322 | // hack to forcefully disable "use bytes" |
314 | COP *oldcop = PL_curcop, cop; |
323 | COP cop = *PL_curcop; |
315 | cop.op_private = 0; |
324 | cop.op_private = 0; |
|
|
325 | |
|
|
326 | ENTER; |
|
|
327 | SAVETMPS; |
|
|
328 | |
|
|
329 | SAVEVPTR (PL_curcop); |
316 | PL_curcop = &cop; |
330 | PL_curcop = &cop; |
317 | |
331 | |
318 | SAVETMPS; |
|
|
319 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
332 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
333 | |
320 | FREETMPS; |
334 | FREETMPS; |
321 | |
335 | LEAVE; |
322 | PL_curcop = oldcop; |
|
|
323 | } |
336 | } |
324 | |
337 | |
325 | for (i = 0; i < count; ++i) |
338 | for (i = 0; i < count; ++i) |
326 | { |
339 | { |
327 | INDENT; |
340 | INDENT; |
… | |
… | |
425 | if (!(flags & (F_ASCII | F_UTF8))) |
438 | if (!(flags & (F_ASCII | F_UTF8))) |
426 | SvUTF8_on (enc.sv); |
439 | SvUTF8_on (enc.sv); |
427 | |
440 | |
428 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
441 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
429 | |
442 | |
430 | #ifdef SvPV_shrink_to_cur |
|
|
431 | if (enc.flags & F_SHRINK) |
443 | if (enc.flags & F_SHRINK) |
432 | SvPV_shrink_to_cur (enc.sv); |
444 | shrink (enc.sv); |
433 | #endif |
445 | |
434 | return enc.sv; |
446 | return enc.sv; |
435 | } |
447 | } |
436 | |
448 | |
437 | ///////////////////////////////////////////////////////////////////////////// |
449 | ///////////////////////////////////////////////////////////////////////////// |
438 | |
450 | |
… | |
… | |
576 | else if (ch >= 0x80) |
588 | else if (ch >= 0x80) |
577 | { |
589 | { |
578 | STRLEN clen; |
590 | STRLEN clen; |
579 | 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); |
580 | if (clen == (STRLEN)-1) |
592 | if (clen == (STRLEN)-1) |
581 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
593 | ERR ("malformed UTF-8 character in JSON string"); |
582 | |
594 | |
583 | APPEND_GROW (clen); |
595 | APPEND_GROW (clen); |
584 | do |
596 | do |
585 | { |
597 | { |
586 | *cur++ = *dec->cur++; |
598 | *cur++ = *dec->cur++; |
… | |
… | |
603 | *SvEND (sv) = 0; |
615 | *SvEND (sv) = 0; |
604 | |
616 | |
605 | if (utf8) |
617 | if (utf8) |
606 | SvUTF8_on (sv); |
618 | SvUTF8_on (sv); |
607 | |
619 | |
608 | #ifdef SvPV_shrink_to_cur |
|
|
609 | if (dec->flags & F_SHRINK) |
620 | if (dec->flags & F_SHRINK) |
610 | SvPV_shrink_to_cur (sv); |
621 | shrink (sv); |
611 | #endif |
|
|
612 | |
622 | |
613 | return sv; |
623 | return sv; |
614 | |
624 | |
615 | fail: |
625 | fail: |
616 | SvREFCNT_dec (sv); |
626 | SvREFCNT_dec (sv); |
… | |
… | |
838 | ERR ("'null' expected"); |
848 | ERR ("'null' expected"); |
839 | |
849 | |
840 | break; |
850 | break; |
841 | |
851 | |
842 | default: |
852 | default: |
843 | ERR ("malformed json string"); |
853 | ERR ("malformed json string, neither array, object, number, string or atom"); |
844 | break; |
854 | break; |
845 | } |
855 | } |
846 | |
856 | |
847 | fail: |
857 | fail: |
848 | return 0; |
858 | return 0; |
… | |
… | |
868 | |
878 | |
869 | sv = decode_sv (&dec); |
879 | sv = decode_sv (&dec); |
870 | |
880 | |
871 | if (!sv) |
881 | if (!sv) |
872 | { |
882 | { |
|
|
883 | IV offset = dec.flags & F_UTF8 |
|
|
884 | ? dec.cur - SvPVX (string) |
873 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
885 | : utf8_distance (dec.cur, SvPVX (string)); |
874 | SV *uni = sv_newmortal (); |
886 | SV *uni = sv_newmortal (); |
|
|
887 | |
875 | // horrible hack to silence warning inside pv_uni_display |
888 | // horrible hack to silence warning inside pv_uni_display |
876 | COP cop; |
889 | COP cop = *PL_curcop; |
877 | memset (&cop, 0, sizeof (cop)); |
|
|
878 | cop.cop_warnings = pWARN_NONE; |
890 | cop.cop_warnings = pWARN_NONE; |
|
|
891 | ENTER; |
879 | SAVEVPTR (PL_curcop); |
892 | SAVEVPTR (PL_curcop); |
880 | PL_curcop = &cop; |
893 | PL_curcop = &cop; |
881 | |
|
|
882 | 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 | |
883 | croak ("%s, at character offset %d (%s)", |
897 | croak ("%s, at character offset %d (%s)", |
884 | dec.err, |
898 | dec.err, |
885 | (int)offset, |
899 | (int)offset, |
886 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
900 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
887 | } |
901 | } |