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.4 by root, Thu Mar 22 21:13:58 2007 UTC vs.
Revision 1.11 by root, Sat Mar 24 19:42:14 2007 UTC

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
16#define F_SHRINK 0x00000100
17 17
18#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 18#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
19#define F_DEFAULT 0 19#define F_DEFAULT 0
20 20
21#define INIT_SIZE 32 // initial scalar size to be allocated 21#define INIT_SIZE 32 // initial scalar size to be allocated
53 croak ("object is not of type JSON::XS"); 53 croak ("object is not of type JSON::XS");
54 54
55 return &SvUVX (SvRV (sv)); 55 return &SvUVX (SvRV (sv));
56} 56}
57 57
58static void
59shrink (SV *sv)
60{
61 sv_utf8_downgrade (sv, 1);
62#ifdef SvPV_shrink_to_cur
63 SvPV_shrink_to_cur (sv);
64#endif
65}
66
58///////////////////////////////////////////////////////////////////////////// 67/////////////////////////////////////////////////////////////////////////////
59 68
60static void 69static void
61need (enc_t *enc, STRLEN len) 70need (enc_t *enc, STRLEN len)
62{ 71{
85 94
86 while (str < end) 95 while (str < end)
87 { 96 {
88 unsigned char ch = *(unsigned char *)str; 97 unsigned char ch = *(unsigned char *)str;
89 98
90 if (ch == '"') 99 if (ch >= 0x20 && ch < 0x80) // most common case
91 { 100 {
101 if (ch == '"') // but with slow exceptions
102 {
92 need (enc, len += 1); 103 need (enc, len += 1);
93 *enc->cur++ = '\\'; 104 *enc->cur++ = '\\';
94 *enc->cur++ = '"'; 105 *enc->cur++ = '"';
95 ++str;
96 } 106 }
97 else if (ch == '\\') 107 else if (ch == '\\')
98 { 108 {
99 need (enc, len += 1); 109 need (enc, len += 1);
100 *enc->cur++ = '\\'; 110 *enc->cur++ = '\\';
101 *enc->cur++ = '\\'; 111 *enc->cur++ = '\\';
102 ++str;
103 } 112 }
104 else if (ch >= 0x20 && ch < 0x80) // most common case 113 else
105 {
106 *enc->cur++ = ch; 114 *enc->cur++ = ch;
107 ++str; 115
108 }
109 else if (ch == '\015')
110 {
111 need (enc, len += 1);
112 *enc->cur++ = '\\';
113 *enc->cur++ = 'r';
114 ++str;
115 }
116 else if (ch == '\012')
117 {
118 need (enc, len += 1);
119 *enc->cur++ = '\\';
120 *enc->cur++ = 'n';
121 ++str; 116 ++str;
122 } 117 }
123 else 118 else
124 { 119 {
125 STRLEN clen; 120 switch (ch)
126 UV uch;
127
128 if (is_utf8)
129 { 121 {
130 uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); 122 case '\010': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break;
131 if (clen < 0) 123 case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break;
132 croak ("malformed UTF-8 character in string, cannot convert to JSON"); 124 case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break;
133 } 125 case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break;
134 else 126 case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break;
135 {
136 uch = ch;
137 clen = 1;
138 }
139 127
140 if (uch < 0x80 || enc->flags & F_ASCII) 128 default:
141 {
142 if (uch > 0xFFFFUL)
143 { 129 {
130 STRLEN clen;
131 UV uch;
132
133 if (is_utf8)
134 {
135 uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
136 if (clen == (STRLEN)-1)
137 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str);
138 }
139 else
140 {
141 uch = ch;
142 clen = 1;
143 }
144
145 if (uch > 0x10FFFFUL)
146 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
147
148 if (uch < 0x80 || enc->flags & F_ASCII)
149 {
150 if (uch > 0xFFFFUL)
151 {
144 need (enc, len += 11); 152 need (enc, len += 11);
145 sprintf (enc->cur, "\\u%04x\\u%04x", 153 sprintf (enc->cur, "\\u%04x\\u%04x",
146 (uch - 0x10000) / 0x400 + 0xD800, 154 (int)((uch - 0x10000) / 0x400 + 0xD800),
147 (uch - 0x10000) % 0x400 + 0xDC00); 155 (int)((uch - 0x10000) % 0x400 + 0xDC00));
148 enc->cur += 12; 156 enc->cur += 12;
157 }
158 else
159 {
160 static char hexdigit [16] = "0123456789abcdef";
161 need (enc, len += 5);
162 *enc->cur++ = '\\';
163 *enc->cur++ = 'u';
164 *enc->cur++ = hexdigit [ uch >> 12 ];
165 *enc->cur++ = hexdigit [(uch >> 8) & 15];
166 *enc->cur++ = hexdigit [(uch >> 4) & 15];
167 *enc->cur++ = hexdigit [(uch >> 0) & 15];
168 }
169
170 str += clen;
171 }
172 else if (is_utf8)
173 {
174 need (enc, len += clen);
175 do
176 {
177 *enc->cur++ = *str++;
178 }
179 while (--clen);
180 }
181 else
182 {
183 need (enc, len += 10); // never more than 11 bytes needed
184 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
185 ++str;
186 }
149 } 187 }
150 else
151 {
152 static char hexdigit [16] = "0123456789abcdef";
153 need (enc, len += 5);
154 *enc->cur++ = '\\';
155 *enc->cur++ = 'u';
156 *enc->cur++ = hexdigit [ uch >> 12 ];
157 *enc->cur++ = hexdigit [(uch >> 8) & 15];
158 *enc->cur++ = hexdigit [(uch >> 4) & 15];
159 *enc->cur++ = hexdigit [(uch >> 0) & 15];
160 }
161
162 str += clen;
163 }
164 else if (is_utf8)
165 {
166 need (enc, len += clen);
167 while (clen--)
168 *enc->cur++ = *str++;
169 }
170 else
171 {
172 need (enc, 10); // never more than 11 bytes needed
173 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
174 ++str;
175 } 188 }
176 } 189 }
177 190
178 --len; 191 --len;
179 } 192 }
261 HE *b = *(HE **)b_; 274 HE *b = *(HE **)b_;
262 275
263 STRLEN la = HeKLEN (a); 276 STRLEN la = HeKLEN (a);
264 STRLEN lb = HeKLEN (b); 277 STRLEN lb = HeKLEN (b);
265 278
266 if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 279 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb)))
267 cmp = la < lb ? -1 : la == lb ? 0 : 1; 280 cmp = la - lb;
268 281
269 return cmp; 282 return cmp;
270} 283}
271 284
272// compare hash entries, used when some keys are sv's or utf-x 285// compare hash entries, used when some keys are sv's or utf-x
306 319
307 if (fast) 320 if (fast)
308 qsort (hes, count, sizeof (HE *), he_cmp_fast); 321 qsort (hes, count, sizeof (HE *), he_cmp_fast);
309 else 322 else
310 { 323 {
311 // hack to disable "use bytes" 324 // hack to forcefully disable "use bytes"
312 COP *oldcop = PL_curcop, cop; 325 COP cop = *PL_curcop;
313 cop.op_private = 0; 326 cop.op_private = 0;
327
328 ENTER;
329 SAVETMPS;
330
331 SAVEVPTR (PL_curcop);
314 PL_curcop = &cop; 332 PL_curcop = &cop;
315 333
316 SAVETMPS;
317 qsort (hes, count, sizeof (HE *), he_cmp_slow); 334 qsort (hes, count, sizeof (HE *), he_cmp_slow);
335
318 FREETMPS; 336 FREETMPS;
319 337 LEAVE;
320 PL_curcop = oldcop;
321 } 338 }
322 339
323 for (i = 0; i < count; ++i) 340 for (i = 0; i < count; ++i)
324 { 341 {
325 INDENT; 342 INDENT;
381 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) 398 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv))
382 : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); 399 : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv));
383 } 400 }
384 else if (SvROK (sv)) 401 else if (SvROK (sv))
385 { 402 {
403 SV *rv = SvRV (sv);
404
386 if (!--enc->max_recurse) 405 if (!--enc->max_recurse)
387 croak ("data structure too deep (hit recursion limit)"); 406 croak ("data structure too deep (hit recursion limit)");
388 407
389 sv = SvRV (sv);
390
391 switch (SvTYPE (sv)) 408 switch (SvTYPE (rv))
392 { 409 {
393 case SVt_PVAV: encode_av (enc, (AV *)sv); break; 410 case SVt_PVAV: encode_av (enc, (AV *)rv); break;
394 case SVt_PVHV: encode_hv (enc, (HV *)sv); break; 411 case SVt_PVHV: encode_hv (enc, (HV *)rv); break;
395 412
396 default: 413 default:
397 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));
398 } 416 }
399 } 417 }
400 else if (!SvOK (sv)) 418 else if (!SvOK (sv))
401 encode_str (enc, "null", 4, 0); 419 encode_str (enc, "null", 4, 0);
402 else 420 else
403 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));
404} 423}
405 424
406static SV * 425static SV *
407encode_json (SV *scalar, UV flags) 426encode_json (SV *scalar, UV flags)
408{ 427{
409 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 428 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar))
410 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)");
411 430
412 enc_t enc; 431 enc_t enc;
413 enc.flags = flags; 432 enc.flags = flags;
414 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 433 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
415 enc.cur = SvPVX (enc.sv); 434 enc.cur = SvPVX (enc.sv);
422 441
423 if (!(flags & (F_ASCII | F_UTF8))) 442 if (!(flags & (F_ASCII | F_UTF8)))
424 SvUTF8_on (enc.sv); 443 SvUTF8_on (enc.sv);
425 444
426 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 445 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
446
447 if (enc.flags & F_SHRINK)
448 shrink (enc.sv);
449
427 return enc.sv; 450 return enc.sv;
428} 451}
429 452
430///////////////////////////////////////////////////////////////////////////// 453/////////////////////////////////////////////////////////////////////////////
431 454
529 552
530 // possibly a surrogate pair 553 // possibly a surrogate pair
531 if (hi >= 0xd800 && hi < 0xdc00) 554 if (hi >= 0xd800 && hi < 0xdc00)
532 { 555 {
533 if (dec->cur [0] != '\\' || dec->cur [1] != 'u') 556 if (dec->cur [0] != '\\' || dec->cur [1] != 'u')
534 ERR ("illegal surrogate character"); 557 ERR ("missing low surrogate character in surrogate pair");
535 558
536 dec->cur += 2; 559 dec->cur += 2;
537 560
538 lo = decode_4hex (dec); 561 lo = decode_4hex (dec);
539 if (lo == (UV)-1) 562 if (lo == (UV)-1)
542 if (lo < 0xdc00 || lo >= 0xe000) 565 if (lo < 0xdc00 || lo >= 0xe000)
543 ERR ("surrogate pair expected"); 566 ERR ("surrogate pair expected");
544 567
545 hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; 568 hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000;
546 } 569 }
547 else if (lo >= 0xdc00 && lo < 0xe000) 570 else if (hi >= 0xdc00 && hi < 0xe000)
548 ERR ("illegal surrogate character"); 571 ERR ("missing high surrogate character in surrogate pair");
549 572
550 if (hi >= 0x80) 573 if (hi >= 0x80)
551 { 574 {
552 utf8 = 1; 575 utf8 = 1;
553 576
556 } 579 }
557 else 580 else
558 APPEND_CH (hi); 581 APPEND_CH (hi);
559 } 582 }
560 break; 583 break;
584
585 default:
586 --dec->cur;
587 ERR ("illegal backslash escape sequence in string");
561 } 588 }
562 } 589 }
563 else if (ch >= 0x20 && ch <= 0x7f) 590 else if (ch >= 0x20 && ch <= 0x7f)
564 APPEND_CH (*dec->cur++); 591 APPEND_CH (*dec->cur++);
565 else if (ch >= 0x80) 592 else if (ch >= 0x80)
566 { 593 {
567 STRLEN clen; 594 STRLEN clen;
568 UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); 595 UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY);
569 if (clen < 0) 596 if (clen == (STRLEN)-1)
570 ERR ("malformed UTF-8 character in string, cannot convert to JSON"); 597 ERR ("malformed UTF-8 character in JSON string");
571 598
572 APPEND_GROW (clen); 599 APPEND_GROW (clen);
573 memcpy (cur, dec->cur, clen); 600 do
574 cur += clen; 601 {
575 dec->cur += clen; 602 *cur++ = *dec->cur++;
603 }
604 while (--clen);
605
606 utf8 = 1;
576 } 607 }
608 else if (dec->cur == dec->end)
609 ERR ("unexpected end of string while parsing json string");
577 else 610 else
578 ERR ("invalid character encountered"); 611 ERR ("invalid character encountered");
579 } 612 }
580 613
581 ++dec->cur; 614 ++dec->cur;
585 SvPOK_only (sv); 618 SvPOK_only (sv);
586 *SvEND (sv) = 0; 619 *SvEND (sv) = 0;
587 620
588 if (utf8) 621 if (utf8)
589 SvUTF8_on (sv); 622 SvUTF8_on (sv);
623
624 if (dec->flags & F_SHRINK)
625 shrink (sv);
590 626
591 return sv; 627 return sv;
592 628
593fail: 629fail:
594 SvREFCNT_dec (sv); 630 SvREFCNT_dec (sv);
609 { 645 {
610 ++dec->cur; 646 ++dec->cur;
611 if (*dec->cur >= '0' && *dec->cur <= '9') 647 if (*dec->cur >= '0' && *dec->cur <= '9')
612 ERR ("malformed number (leading zero must not be followed by another digit)"); 648 ERR ("malformed number (leading zero must not be followed by another digit)");
613 } 649 }
614 650 else if (*dec->cur < '0' || *dec->cur > '9')
615 // int 651 ERR ("malformed number (no digits after initial minus)");
652 else
653 do
654 {
655 ++dec->cur;
656 }
616 while (*dec->cur >= '0' && *dec->cur <= '9') 657 while (*dec->cur >= '0' && *dec->cur <= '9');
617 ++dec->cur;
618 658
619 // [frac] 659 // [frac]
620 if (*dec->cur == '.') 660 if (*dec->cur == '.')
621 { 661 {
622 is_nv = 1; 662 ++dec->cur;
663
664 if (*dec->cur < '0' || *dec->cur > '9')
665 ERR ("malformed number (no digits after decimal point)");
623 666
624 do 667 do
625 { 668 {
626 ++dec->cur; 669 ++dec->cur;
627 } 670 }
628 while (*dec->cur >= '0' && *dec->cur <= '9'); 671 while (*dec->cur >= '0' && *dec->cur <= '9');
672
673 is_nv = 1;
629 } 674 }
630 675
631 // [exp] 676 // [exp]
632 if (*dec->cur == 'e' || *dec->cur == 'E') 677 if (*dec->cur == 'e' || *dec->cur == 'E')
633 { 678 {
634 is_nv = 1;
635
636 ++dec->cur; 679 ++dec->cur;
680
637 if (*dec->cur == '-' || *dec->cur == '+') 681 if (*dec->cur == '-' || *dec->cur == '+')
638 ++dec->cur; 682 ++dec->cur;
639 683
684 if (*dec->cur < '0' || *dec->cur > '9')
685 ERR ("malformed number (no digits after exp sign)");
686
687 do
688 {
689 ++dec->cur;
690 }
640 while (*dec->cur >= '0' && *dec->cur <= '9') 691 while (*dec->cur >= '0' && *dec->cur <= '9');
641 ++dec->cur; 692
693 is_nv = 1;
642 } 694 }
643 695
644 if (!is_nv) 696 if (!is_nv)
645 { 697 {
646 UV uv; 698 UV uv;
664static SV * 716static SV *
665decode_av (dec_t *dec) 717decode_av (dec_t *dec)
666{ 718{
667 AV *av = newAV (); 719 AV *av = newAV ();
668 720
721 WS;
722 if (*dec->cur == ']')
723 ++dec->cur;
724 else
669 for (;;) 725 for (;;)
670 { 726 {
671 SV *value; 727 SV *value;
672 728
673 value = decode_sv (dec); 729 value = decode_sv (dec);
674 if (!value) 730 if (!value)
675 goto fail; 731 goto fail;
676 732
677 av_push (av, value); 733 av_push (av, value);
678 734
679 WS; 735 WS;
680 736
681 if (*dec->cur == ']') 737 if (*dec->cur == ']')
682 { 738 {
683 ++dec->cur; 739 ++dec->cur;
684 break; 740 break;
741 }
685 } 742
686
687 if (*dec->cur != ',') 743 if (*dec->cur != ',')
688 ERR (", or ] expected while parsing array"); 744 ERR (", or ] expected while parsing array");
689 745
690 ++dec->cur; 746 ++dec->cur;
691 } 747 }
692 748
693 return newRV_noinc ((SV *)av); 749 return newRV_noinc ((SV *)av);
694 750
695fail: 751fail:
696 SvREFCNT_dec (av); 752 SvREFCNT_dec (av);
700static SV * 756static SV *
701decode_hv (dec_t *dec) 757decode_hv (dec_t *dec)
702{ 758{
703 HV *hv = newHV (); 759 HV *hv = newHV ();
704 760
761 WS;
762 if (*dec->cur == '}')
763 ++dec->cur;
764 else
705 for (;;) 765 for (;;)
706 { 766 {
707 SV *key, *value; 767 SV *key, *value;
708 768
709 WS; EXPECT_CH ('"'); 769 WS; EXPECT_CH ('"');
710 770
711 key = decode_str (dec); 771 key = decode_str (dec);
712 if (!key) 772 if (!key)
713 goto fail;
714
715 WS; EXPECT_CH (':');
716
717 value = decode_sv (dec);
718 if (!value)
719 {
720 SvREFCNT_dec (key);
721 goto fail; 773 goto fail;
774
775 WS; EXPECT_CH (':');
776
777 value = decode_sv (dec);
778 if (!value)
779 {
780 SvREFCNT_dec (key);
781 goto fail;
722 } 782 }
723 783
724 //TODO: optimise 784 //TODO: optimise
725 hv_store_ent (hv, key, value, 0); 785 hv_store_ent (hv, key, value, 0);
726 786
727 WS; 787 WS;
728 788
729 if (*dec->cur == '}') 789 if (*dec->cur == '}')
730 { 790 {
731 ++dec->cur; 791 ++dec->cur;
732 break; 792 break;
733 } 793 }
734 794
735 if (*dec->cur != ',') 795 if (*dec->cur != ',')
736 ERR (", or } expected while parsing object/hash"); 796 ERR (", or } expected while parsing object/hash");
737 797
738 ++dec->cur; 798 ++dec->cur;
739 } 799 }
740 800
741 return newRV_noinc ((SV *)hv); 801 return newRV_noinc ((SV *)hv);
742 802
743fail: 803fail:
744 SvREFCNT_dec (hv); 804 SvREFCNT_dec (hv);
784 844
785 case 'n': 845 case 'n':
786 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) 846 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4))
787 { 847 {
788 dec->cur += 4; 848 dec->cur += 4;
789 return newSViv (1); 849 return newSVsv (&PL_sv_undef);
790 } 850 }
791 else 851 else
792 ERR ("'null' expected"); 852 ERR ("'null' expected");
793 853
794 break; 854 break;
795 855
796 default: 856 default:
797 ERR ("malformed json string"); 857 ERR ("malformed json string, neither array, object, number, string or atom");
798 break; 858 break;
799 } 859 }
800 860
801fail: 861fail:
802 return 0; 862 return 0;
805static SV * 865static SV *
806decode_json (SV *string, UV flags) 866decode_json (SV *string, UV flags)
807{ 867{
808 SV *sv; 868 SV *sv;
809 869
810 if (!(flags & F_UTF8)) 870 if (flags & F_UTF8)
871 sv_utf8_downgrade (string, 0);
872 else
811 sv_utf8_upgrade (string); 873 sv_utf8_upgrade (string);
812 874
813 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 875 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
814 876
815 dec_t dec; 877 dec_t dec;
816 dec.flags = flags; 878 dec.flags = flags;
817 dec.cur = SvPVX (string); 879 dec.cur = SvPVX (string);
818 dec.end = SvEND (string); 880 dec.end = SvEND (string);
819 dec.err = 0; 881 dec.err = 0;
820 882
821 *dec.end = 1; // invalid anywhere
822 sv = decode_sv (&dec); 883 sv = decode_sv (&dec);
823 *dec.end = 0;
824 884
825 if (!sv) 885 if (!sv)
826 { 886 {
887 IV offset = dec.flags & F_UTF8
888 ? dec.cur - SvPVX (string)
827 IV offset = utf8_distance (dec.cur, SvPVX (string)); 889 : utf8_distance (dec.cur, SvPVX (string));
828 SV *uni = sv_newmortal (); 890 SV *uni = sv_newmortal ();
829 891
892 // horrible hack to silence warning inside pv_uni_display
893 COP cop = *PL_curcop;
894 cop.cop_warnings = pWARN_NONE;
895 ENTER;
896 SAVEVPTR (PL_curcop);
897 PL_curcop = &cop;
830 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 898 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
899 LEAVE;
900
831 croak ("%s, at character %d (%s)", 901 croak ("%s, at character offset %d (%s)",
832 dec.err, 902 dec.err,
833 (int)offset, 903 (int)offset,
834 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 904 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
835 } 905 }
836 906
837 sv = sv_2mortal (sv); 907 sv = sv_2mortal (sv);
838 908
839 if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) 909 if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv))
840 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)");
841 911
842 return sv; 912 return sv;
843} 913}
844 914
845MODULE = JSON::XS PACKAGE = JSON::XS 915MODULE = JSON::XS PACKAGE = JSON::XS
867 CODE: 937 CODE:
868 RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); 938 RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash);
869 OUTPUT: 939 OUTPUT:
870 RETVAL 940 RETVAL
871 941
872SV *ascii (SV *self, int enable) 942SV *ascii (SV *self, int enable = 1)
873 ALIAS: 943 ALIAS:
874 ascii = F_ASCII 944 ascii = F_ASCII
875 utf8 = F_UTF8 945 utf8 = F_UTF8
876 indent = F_INDENT 946 indent = F_INDENT
877 canonical = F_CANONICAL 947 canonical = F_CANONICAL
878 space_before = F_SPACE_BEFORE 948 space_before = F_SPACE_BEFORE
879 space_after = F_SPACE_AFTER 949 space_after = F_SPACE_AFTER
880 json_rpc = F_JSON_RPC
881 pretty = F_PRETTY 950 pretty = F_PRETTY
882 allow_nonref = F_ALLOW_NONREF 951 allow_nonref = F_ALLOW_NONREF
952 shrink = F_SHRINK
883 CODE: 953 CODE:
884{ 954{
885 UV *uv = SvJSON (self); 955 UV *uv = SvJSON (self);
886 if (enable) 956 if (enable)
887 *uv |= ix; 957 *uv |= ix;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines