ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.6 by root, Fri Mar 23 15:10:55 2007 UTC vs.
Revision 1.9 by root, Fri Mar 23 17:40:29 2007 UTC

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
59static void
60shrink (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
61static void 70static void
124 133
125 if (is_utf8) 134 if (is_utf8)
126 { 135 {
127 uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); 136 uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
128 if (clen == (STRLEN)-1) 137 if (clen == (STRLEN)-1)
129 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);
130 } 139 }
131 else 140 else
132 { 141 {
133 uch = ch; 142 uch = ch;
134 clen = 1; 143 clen = 1;
135 } 144 }
145
146 if (uch > 0x10FFFFUL)
147 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
136 148
137 if (uch < 0x80 || enc->flags & F_ASCII) 149 if (uch < 0x80 || enc->flags & F_ASCII)
138 { 150 {
139 if (uch > 0xFFFFUL) 151 if (uch > 0xFFFFUL)
140 { 152 {
167 } 179 }
168 while (--clen); 180 while (--clen);
169 } 181 }
170 else 182 else
171 { 183 {
172 need (enc, 10); // never more than 11 bytes needed 184 need (enc, len += 10); // never more than 11 bytes needed
173 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 185 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
174 ++str; 186 ++str;
175 } 187 }
176 } 188 }
177 } 189 }
308 320
309 if (fast) 321 if (fast)
310 qsort (hes, count, sizeof (HE *), he_cmp_fast); 322 qsort (hes, count, sizeof (HE *), he_cmp_fast);
311 else 323 else
312 { 324 {
313 // hack to disable "use bytes" 325 // hack to forcefully disable "use bytes"
314 COP *oldcop = PL_curcop, cop; 326 COP cop = *PL_curcop;
315 cop.op_private = 0; 327 cop.op_private = 0;
328
329 ENTER;
330 SAVETMPS;
331
332 SAVEVPTR (PL_curcop);
316 PL_curcop = &cop; 333 PL_curcop = &cop;
317 334
318 SAVETMPS;
319 qsort (hes, count, sizeof (HE *), he_cmp_slow); 335 qsort (hes, count, sizeof (HE *), he_cmp_slow);
336
320 FREETMPS; 337 FREETMPS;
321 338 LEAVE;
322 PL_curcop = oldcop;
323 } 339 }
324 340
325 for (i = 0; i < count; ++i) 341 for (i = 0; i < count; ++i)
326 { 342 {
327 INDENT; 343 INDENT;
383 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) 399 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv))
384 : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); 400 : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv));
385 } 401 }
386 else if (SvROK (sv)) 402 else if (SvROK (sv))
387 { 403 {
404 SV *rv = SvRV (sv);
405
388 if (!--enc->max_recurse) 406 if (!--enc->max_recurse)
389 croak ("data structure too deep (hit recursion limit)"); 407 croak ("data structure too deep (hit recursion limit)");
390 408
391 sv = SvRV (sv);
392
393 switch (SvTYPE (sv)) 409 switch (SvTYPE (rv))
394 { 410 {
395 case SVt_PVAV: encode_av (enc, (AV *)sv); break; 411 case SVt_PVAV: encode_av (enc, (AV *)rv); break;
396 case SVt_PVHV: encode_hv (enc, (HV *)sv); break; 412 case SVt_PVHV: encode_hv (enc, (HV *)rv); break;
397 413
398 default: 414 default:
399 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));
400 } 417 }
401 } 418 }
402 else if (!SvOK (sv)) 419 else if (!SvOK (sv))
403 encode_str (enc, "null", 4, 0); 420 encode_str (enc, "null", 4, 0);
404 else 421 else
405 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));
406} 424}
407 425
408static SV * 426static SV *
409encode_json (SV *scalar, UV flags) 427encode_json (SV *scalar, UV flags)
410{ 428{
411 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 429 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar))
412 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)");
413 431
414 enc_t enc; 432 enc_t enc;
415 enc.flags = flags; 433 enc.flags = flags;
416 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 434 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
417 enc.cur = SvPVX (enc.sv); 435 enc.cur = SvPVX (enc.sv);
425 if (!(flags & (F_ASCII | F_UTF8))) 443 if (!(flags & (F_ASCII | F_UTF8)))
426 SvUTF8_on (enc.sv); 444 SvUTF8_on (enc.sv);
427 445
428 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 446 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
429 447
430#ifdef SvPV_shrink_to_cur
431 if (enc.flags & F_SHRINK) 448 if (enc.flags & F_SHRINK)
432 SvPV_shrink_to_cur (enc.sv); 449 shrink (enc.sv);
433#endif 450
434 return enc.sv; 451 return enc.sv;
435} 452}
436 453
437///////////////////////////////////////////////////////////////////////////// 454/////////////////////////////////////////////////////////////////////////////
438 455
576 else if (ch >= 0x80) 593 else if (ch >= 0x80)
577 { 594 {
578 STRLEN clen; 595 STRLEN clen;
579 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);
580 if (clen == (STRLEN)-1) 597 if (clen == (STRLEN)-1)
581 ERR ("malformed UTF-8 character in string, cannot convert to JSON"); 598 ERR ("malformed UTF-8 character in JSON string");
582 599
583 APPEND_GROW (clen); 600 APPEND_GROW (clen);
584 do 601 do
585 { 602 {
586 *cur++ = *dec->cur++; 603 *cur++ = *dec->cur++;
603 *SvEND (sv) = 0; 620 *SvEND (sv) = 0;
604 621
605 if (utf8) 622 if (utf8)
606 SvUTF8_on (sv); 623 SvUTF8_on (sv);
607 624
608#ifdef SvPV_shrink_to_cur
609 if (dec->flags & F_SHRINK) 625 if (dec->flags & F_SHRINK)
610 SvPV_shrink_to_cur (sv); 626 shrink (sv);
611#endif
612 627
613 return sv; 628 return sv;
614 629
615fail: 630fail:
616 SvREFCNT_dec (sv); 631 SvREFCNT_dec (sv);
838 ERR ("'null' expected"); 853 ERR ("'null' expected");
839 854
840 break; 855 break;
841 856
842 default: 857 default:
843 ERR ("malformed json string"); 858 ERR ("malformed json string, neither array, object, number, string or atom");
844 break; 859 break;
845 } 860 }
846 861
847fail: 862fail:
848 return 0; 863 return 0;
868 883
869 sv = decode_sv (&dec); 884 sv = decode_sv (&dec);
870 885
871 if (!sv) 886 if (!sv)
872 { 887 {
888 IV offset = dec.flags & F_UTF8
889 ? dec.cur - SvPVX (string)
873 IV offset = utf8_distance (dec.cur, SvPVX (string)); 890 : utf8_distance (dec.cur, SvPVX (string));
874 SV *uni = sv_newmortal (); 891 SV *uni = sv_newmortal ();
892
875 // horrible hack to silence warning inside pv_uni_display 893 // horrible hack to silence warning inside pv_uni_display
876 COP cop; 894 COP cop = *PL_curcop;
877 memset (&cop, 0, sizeof (cop));
878 cop.cop_warnings = pWARN_NONE; 895 cop.cop_warnings = pWARN_NONE;
896 ENTER;
879 SAVEVPTR (PL_curcop); 897 SAVEVPTR (PL_curcop);
880 PL_curcop = &cop; 898 PL_curcop = &cop;
881
882 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
883 croak ("%s, at character offset %d (%s)", 902 croak ("%s, at character offset %d (%s)",
884 dec.err, 903 dec.err,
885 (int)offset, 904 (int)offset,
886 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 905 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
887 } 906 }
888 907
889 sv = sv_2mortal (sv); 908 sv = sv_2mortal (sv);
890 909
891 if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) 910 if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv))
892 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)");
893 912
894 return sv; 913 return sv;
895} 914}
896 915
897MODULE = JSON::XS PACKAGE = JSON::XS 916MODULE = JSON::XS PACKAGE = JSON::XS

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines