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.2 by root, Thu Mar 22 17:28:50 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_ALLOW_NONREF 0x00000080
15#define F_JSON_RPC 0x00000040 16#define F_SHRINK 0x00000100
16 17
17#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 18#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
18#define F_DEFAULT 0 19#define F_DEFAULT 0
19 20
20#define INIT_SIZE 32 // initial scalar size to be allocated 21#define INIT_SIZE 32 // initial scalar size to be allocated
39// structure used for decoding JSON 40// structure used for decoding JSON
40typedef struct 41typedef struct
41{ 42{
42 char *cur; 43 char *cur;
43 char *end; 44 char *end;
44 char *err; 45 const char *err;
45 UV flags; 46 UV flags;
46} dec_t; 47} dec_t;
47 48
48static UV * 49static UV *
49SvJSON (SV *sv) 50SvJSON (SV *sv)
50{ 51{
51 if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) 52 if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash))
52 croak ("object is not of type JSON::XS"); 53 croak ("object is not of type JSON::XS");
53 54
54 return &SvUVX (SvRV (sv)); 55 return &SvUVX (SvRV (sv));
56}
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
55} 65}
56 66
57///////////////////////////////////////////////////////////////////////////// 67/////////////////////////////////////////////////////////////////////////////
58 68
59static void 69static void
62 if (enc->cur + len >= enc->end) 72 if (enc->cur + len >= enc->end)
63 { 73 {
64 STRLEN cur = enc->cur - SvPVX (enc->sv); 74 STRLEN cur = enc->cur - SvPVX (enc->sv);
65 SvGROW (enc->sv, cur + len + 1); 75 SvGROW (enc->sv, cur + len + 1);
66 enc->cur = SvPVX (enc->sv) + cur; 76 enc->cur = SvPVX (enc->sv) + cur;
67 enc->end = SvEND (enc->sv); 77 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv);
68 } 78 }
69} 79}
70 80
71static void 81static void
72encode_ch (enc_t *enc, char ch) 82encode_ch (enc_t *enc, char ch)
78static void 88static void
79encode_str (enc_t *enc, char *str, STRLEN len, int is_utf8) 89encode_str (enc_t *enc, char *str, STRLEN len, int is_utf8)
80{ 90{
81 char *end = str + len; 91 char *end = str + len;
82 92
93 need (enc, len);
94
83 while (str < end) 95 while (str < end)
84 { 96 {
85 unsigned char ch = *(unsigned char *)str; 97 unsigned char ch = *(unsigned char *)str;
98
86 if (ch >= 0x20 && ch < 0x80) // most common case 99 if (ch >= 0x20 && ch < 0x80) // most common case
87 { 100 {
101 if (ch == '"') // but with slow exceptions
102 {
103 need (enc, len += 1);
104 *enc->cur++ = '\\';
105 *enc->cur++ = '"';
106 }
107 else if (ch == '\\')
108 {
109 need (enc, len += 1);
110 *enc->cur++ = '\\';
111 *enc->cur++ = '\\';
112 }
113 else
88 *enc->cur++ = ch; 114 *enc->cur++ = ch;
115
89 str++; 116 ++str;
90 } 117 }
91 else 118 else
92 { 119 {
93 STRLEN clen; 120 switch (ch)
94 UV uch;
95
96 if (is_utf8)
97 { 121 {
98 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;
99 if (clen < 0) 123 case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break;
100 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;
101 } 125 case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break;
102 else 126 case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break;
103 {
104 uch = ch;
105 clen = 1;
106 }
107 127
108 need (enc, len += 6); 128 default:
109
110 if (uch < 0xa0 || enc->flags & F_ASCII)
111 {
112 if (uch > 0xFFFFUL)
113 { 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;
114 len += 6; 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 {
115 need (enc, len += 6); 152 need (enc, len += 11);
116 sprintf (enc->cur, "\\u%04x\\u%04x", 153 sprintf (enc->cur, "\\u%04x\\u%04x",
117 (uch - 0x10000) / 0x400 + 0xD800, 154 (int)((uch - 0x10000) / 0x400 + 0xD800),
118 (uch - 0x10000) % 0x400 + 0xDC00); 155 (int)((uch - 0x10000) % 0x400 + 0xDC00));
119 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;
120 } 171 }
172 else if (is_utf8)
173 {
174 need (enc, len += clen);
175 do
176 {
177 *enc->cur++ = *str++;
178 }
179 while (--clen);
180 }
121 else 181 else
122 { 182 {
123 sprintf (enc->cur, "\\u%04x", uch); 183 need (enc, len += 10); // never more than 11 bytes needed
124 enc->cur += 6; 184 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
185 ++str;
186 }
125 } 187 }
126 } 188 }
127 else if (is_utf8)
128 {
129 memcpy (enc->cur, str, clen);
130 enc->cur += clen;
131 }
132 else
133 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
134
135 str += clen;
136 } 189 }
137 190
138 --len; 191 --len;
139 } 192 }
140} 193}
191 244
192 if (HeKLEN (he) == HEf_SVKEY) 245 if (HeKLEN (he) == HEf_SVKEY)
193 { 246 {
194 SV *sv = HeSVKEY (he); 247 SV *sv = HeSVKEY (he);
195 STRLEN len; 248 STRLEN len;
249 char *str;
250
251 SvGETMAGIC (sv);
196 char *str = SvPV (sv, len); 252 str = SvPV (sv, len);
197 253
198 encode_str (enc, str, len, SvUTF8 (sv)); 254 encode_str (enc, str, len, SvUTF8 (sv));
199 } 255 }
200 else 256 else
201 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); 257 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he));
218 HE *b = *(HE **)b_; 274 HE *b = *(HE **)b_;
219 275
220 STRLEN la = HeKLEN (a); 276 STRLEN la = HeKLEN (a);
221 STRLEN lb = HeKLEN (b); 277 STRLEN lb = HeKLEN (b);
222 278
223 if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 279 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb)))
224 cmp = la < lb ? -1 : la == lb ? 0 : 1; 280 cmp = la - lb;
225 281
226 return cmp; 282 return cmp;
227} 283}
228 284
229// 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
263 319
264 if (fast) 320 if (fast)
265 qsort (hes, count, sizeof (HE *), he_cmp_fast); 321 qsort (hes, count, sizeof (HE *), he_cmp_fast);
266 else 322 else
267 { 323 {
268 // hack to disable "use bytes" 324 // hack to forcefully disable "use bytes"
269 COP *oldcop = PL_curcop, cop; 325 COP cop = *PL_curcop;
270 cop.op_private = 0; 326 cop.op_private = 0;
327
328 ENTER;
329 SAVETMPS;
330
331 SAVEVPTR (PL_curcop);
271 PL_curcop = &cop; 332 PL_curcop = &cop;
272 333
273 SAVETMPS;
274 qsort (hes, count, sizeof (HE *), he_cmp_slow); 334 qsort (hes, count, sizeof (HE *), he_cmp_slow);
335
275 FREETMPS; 336 FREETMPS;
276 337 LEAVE;
277 PL_curcop = oldcop;
278 } 338 }
279 339
280 for (i = 0; i < count; ++i) 340 for (i = 0; i < count; ++i)
281 { 341 {
282 INDENT; 342 INDENT;
312} 372}
313 373
314static void 374static void
315encode_sv (enc_t *enc, SV *sv) 375encode_sv (enc_t *enc, SV *sv)
316{ 376{
377 SvGETMAGIC (sv);
378
317 if (SvPOKp (sv)) 379 if (SvPOKp (sv))
318 { 380 {
319 STRLEN len; 381 STRLEN len;
320 char *str = SvPV (sv, len); 382 char *str = SvPV (sv, len);
321 encode_ch (enc, '"'); 383 encode_ch (enc, '"');
336 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) 398 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv))
337 : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); 399 : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv));
338 } 400 }
339 else if (SvROK (sv)) 401 else if (SvROK (sv))
340 { 402 {
403 SV *rv = SvRV (sv);
404
341 if (!--enc->max_recurse) 405 if (!--enc->max_recurse)
342 croak ("data structure too deep (hit recursion limit)"); 406 croak ("data structure too deep (hit recursion limit)");
343 407
344 sv = SvRV (sv);
345
346 switch (SvTYPE (sv)) 408 switch (SvTYPE (rv))
347 { 409 {
348 case SVt_PVAV: encode_av (enc, (AV *)sv); break; 410 case SVt_PVAV: encode_av (enc, (AV *)rv); break;
349 case SVt_PVHV: encode_hv (enc, (HV *)sv); break; 411 case SVt_PVHV: encode_hv (enc, (HV *)rv); break;
350 412
351 default: 413 default:
352 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));
353 } 416 }
354 } 417 }
355 else if (!SvOK (sv)) 418 else if (!SvOK (sv))
356 encode_str (enc, "null", 4, 0); 419 encode_str (enc, "null", 4, 0);
357 else 420 else
358 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));
359} 423}
360 424
361static SV * 425static SV *
362encode_json (SV *scalar, UV flags) 426encode_json (SV *scalar, UV flags)
363{ 427{
428 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar))
429 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
430
364 enc_t enc; 431 enc_t enc;
365 enc.flags = flags; 432 enc.flags = flags;
366 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 433 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
367 enc.cur = SvPVX (enc.sv); 434 enc.cur = SvPVX (enc.sv);
368 enc.end = SvEND (enc.sv); 435 enc.end = SvEND (enc.sv);
374 441
375 if (!(flags & (F_ASCII | F_UTF8))) 442 if (!(flags & (F_ASCII | F_UTF8)))
376 SvUTF8_on (enc.sv); 443 SvUTF8_on (enc.sv);
377 444
378 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
379 return enc.sv; 450 return enc.sv;
380} 451}
381 452
382///////////////////////////////////////////////////////////////////////////// 453/////////////////////////////////////////////////////////////////////////////
383 454
398 ++dec->cur; \ 469 ++dec->cur; \
399 SE 470 SE
400 471
401static SV *decode_sv (dec_t *dec); 472static SV *decode_sv (dec_t *dec);
402 473
403#define APPEND_CH(ch) SB \
404 SvGROW (sv, cur + 1 + 1); \
405 SvPVX (sv)[cur++] = (ch); \
406 SE
407
408static signed char decode_hexdigit[256]; 474static signed char decode_hexdigit[256];
409 475
410static UV 476static UV
411decode_4hex (dec_t *dec) 477decode_4hex (dec_t *dec)
412{ 478{
430 496
431fail: 497fail:
432 return (UV)-1; 498 return (UV)-1;
433} 499}
434 500
501#define APPEND_GROW(n) SB \
502 if (cur + (n) >= end) \
503 { \
504 STRLEN ofs = cur - SvPVX (sv); \
505 SvGROW (sv, ofs + (n) + 1); \
506 cur = SvPVX (sv) + ofs; \
507 end = SvEND (sv); \
508 } \
509 SE
510
511#define APPEND_CH(ch) SB \
512 APPEND_GROW (1); \
513 *cur++ = (ch); \
514 SE
515
435static SV * 516static SV *
436decode_str (dec_t *dec) 517decode_str (dec_t *dec)
437{ 518{
438 SV *sv = NEWSV (0,2); 519 SV *sv = NEWSV (0,2);
439 STRLEN cur = 0;
440 int utf8 = 0; 520 int utf8 = 0;
521 char *cur = SvPVX (sv);
522 char *end = SvEND (sv);
441 523
442 for (;;) 524 for (;;)
443 { 525 {
444 unsigned char ch = *(unsigned char *)dec->cur; 526 unsigned char ch = *(unsigned char *)dec->cur;
445 527
470 552
471 // possibly a surrogate pair 553 // possibly a surrogate pair
472 if (hi >= 0xd800 && hi < 0xdc00) 554 if (hi >= 0xd800 && hi < 0xdc00)
473 { 555 {
474 if (dec->cur [0] != '\\' || dec->cur [1] != 'u') 556 if (dec->cur [0] != '\\' || dec->cur [1] != 'u')
475 ERR ("illegal surrogate character"); 557 ERR ("missing low surrogate character in surrogate pair");
476 558
477 dec->cur += 2; 559 dec->cur += 2;
478 560
479 lo = decode_4hex (dec); 561 lo = decode_4hex (dec);
480 if (lo == (UV)-1) 562 if (lo == (UV)-1)
483 if (lo < 0xdc00 || lo >= 0xe000) 565 if (lo < 0xdc00 || lo >= 0xe000)
484 ERR ("surrogate pair expected"); 566 ERR ("surrogate pair expected");
485 567
486 hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; 568 hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000;
487 } 569 }
488 else if (lo >= 0xdc00 && lo < 0xe000) 570 else if (hi >= 0xdc00 && hi < 0xe000)
489 ERR ("illegal surrogate character"); 571 ERR ("missing high surrogate character in surrogate pair");
490 572
491 if (hi >= 0x80) 573 if (hi >= 0x80)
492 { 574 {
493 utf8 = 1; 575 utf8 = 1;
494 576
495 SvGROW (sv, cur + 4 + 1); // at most 4 bytes for 21 bits 577 APPEND_GROW (4); // at most 4 bytes for 21 bits
496 cur = (char *)uvuni_to_utf8_flags (SvPVX (sv) + cur, hi, 0) - SvPVX (sv); 578 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0);
497 } 579 }
498 else 580 else
499 APPEND_CH (hi); 581 APPEND_CH (hi);
500 } 582 }
501 break; 583 break;
584
585 default:
586 --dec->cur;
587 ERR ("illegal backslash escape sequence in string");
502 } 588 }
503 } 589 }
504 else if (ch >= 0x20 && ch <= 0x7f) 590 else if (ch >= 0x20 && ch <= 0x7f)
505 APPEND_CH (*dec->cur++); 591 APPEND_CH (*dec->cur++);
506 else if (ch >= 0x80) 592 else if (ch >= 0x80)
507 { 593 {
508 STRLEN clen; 594 STRLEN clen;
509 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);
510 if (clen < 0) 596 if (clen == (STRLEN)-1)
511 ERR ("malformed UTF-8 character in string, cannot convert to JSON"); 597 ERR ("malformed UTF-8 character in JSON string");
512 598
513 SvGROW (sv, cur + clen + 1); // at most 4 bytes for 21 bits 599 APPEND_GROW (clen);
514 memcpy (SvPVX (sv) + cur, dec->cur, clen); 600 do
515 dec->cur += clen; 601 {
602 *cur++ = *dec->cur++;
603 }
604 while (--clen);
605
606 utf8 = 1;
516 } 607 }
608 else if (dec->cur == dec->end)
609 ERR ("unexpected end of string while parsing json string");
517 else 610 else
518 ERR ("invalid character encountered"); 611 ERR ("invalid character encountered");
519 } 612 }
520 613
521 ++dec->cur; 614 ++dec->cur;
522 615
616 SvCUR_set (sv, cur - SvPVX (sv));
617
523 SvPOK_only (sv); 618 SvPOK_only (sv);
524
525 SvCUR_set (sv, cur);
526 *SvEND (sv) = 0; 619 *SvEND (sv) = 0;
527 620
528 if (utf8) 621 if (utf8)
529 SvUTF8_on (sv); 622 SvUTF8_on (sv);
623
624 if (dec->flags & F_SHRINK)
625 shrink (sv);
530 626
531 return sv; 627 return sv;
532 628
533fail: 629fail:
534 SvREFCNT_dec (sv); 630 SvREFCNT_dec (sv);
549 { 645 {
550 ++dec->cur; 646 ++dec->cur;
551 if (*dec->cur >= '0' && *dec->cur <= '9') 647 if (*dec->cur >= '0' && *dec->cur <= '9')
552 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)");
553 } 649 }
554 650 else if (*dec->cur < '0' || *dec->cur > '9')
555 // int 651 ERR ("malformed number (no digits after initial minus)");
652 else
653 do
654 {
655 ++dec->cur;
656 }
556 while (*dec->cur >= '0' && *dec->cur <= '9') 657 while (*dec->cur >= '0' && *dec->cur <= '9');
557 ++dec->cur;
558 658
559 // [frac] 659 // [frac]
560 if (*dec->cur == '.') 660 if (*dec->cur == '.')
561 { 661 {
562 is_nv = 1; 662 ++dec->cur;
663
664 if (*dec->cur < '0' || *dec->cur > '9')
665 ERR ("malformed number (no digits after decimal point)");
563 666
564 do 667 do
565 { 668 {
566 ++dec->cur; 669 ++dec->cur;
567 } 670 }
568 while (*dec->cur >= '0' && *dec->cur <= '9'); 671 while (*dec->cur >= '0' && *dec->cur <= '9');
672
673 is_nv = 1;
569 } 674 }
570 675
571 // [exp] 676 // [exp]
572 if (*dec->cur == 'e' || *dec->cur == 'E') 677 if (*dec->cur == 'e' || *dec->cur == 'E')
573 { 678 {
574 is_nv = 1;
575
576 ++dec->cur; 679 ++dec->cur;
680
577 if (*dec->cur == '-' || *dec->cur == '+') 681 if (*dec->cur == '-' || *dec->cur == '+')
578 ++dec->cur; 682 ++dec->cur;
579 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 }
580 while (*dec->cur >= '0' && *dec->cur <= '9') 691 while (*dec->cur >= '0' && *dec->cur <= '9');
581 ++dec->cur; 692
693 is_nv = 1;
582 } 694 }
583 695
584 if (!is_nv) 696 if (!is_nv)
585 { 697 {
586 UV uv; 698 UV uv;
604static SV * 716static SV *
605decode_av (dec_t *dec) 717decode_av (dec_t *dec)
606{ 718{
607 AV *av = newAV (); 719 AV *av = newAV ();
608 720
721 WS;
722 if (*dec->cur == ']')
723 ++dec->cur;
724 else
609 for (;;) 725 for (;;)
610 { 726 {
611 SV *value; 727 SV *value;
612 728
613 value = decode_sv (dec); 729 value = decode_sv (dec);
614 if (!value) 730 if (!value)
615 goto fail; 731 goto fail;
616 732
617 av_push (av, value); 733 av_push (av, value);
618 734
619 WS; 735 WS;
620 736
621 if (*dec->cur == ']') 737 if (*dec->cur == ']')
622 { 738 {
623 ++dec->cur; 739 ++dec->cur;
624 break; 740 break;
741 }
625 } 742
626
627 if (*dec->cur != ',') 743 if (*dec->cur != ',')
628 ERR (", or ] expected while parsing array"); 744 ERR (", or ] expected while parsing array");
629 745
630 ++dec->cur; 746 ++dec->cur;
631 } 747 }
632 748
633 return newRV_noinc ((SV *)av); 749 return newRV_noinc ((SV *)av);
634 750
635fail: 751fail:
636 SvREFCNT_dec (av); 752 SvREFCNT_dec (av);
640static SV * 756static SV *
641decode_hv (dec_t *dec) 757decode_hv (dec_t *dec)
642{ 758{
643 HV *hv = newHV (); 759 HV *hv = newHV ();
644 760
761 WS;
762 if (*dec->cur == '}')
763 ++dec->cur;
764 else
645 for (;;) 765 for (;;)
646 { 766 {
647 SV *key, *value; 767 SV *key, *value;
648 768
649 WS; EXPECT_CH ('"'); 769 WS; EXPECT_CH ('"');
650 770
651 key = decode_str (dec); 771 key = decode_str (dec);
652 if (!key) 772 if (!key)
653 goto fail;
654
655 WS; EXPECT_CH (':');
656
657 value = decode_sv (dec);
658 if (!value)
659 {
660 SvREFCNT_dec (key);
661 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;
662 } 782 }
663 783
664 //TODO: optimise 784 //TODO: optimise
665 hv_store_ent (hv, key, value, 0); 785 hv_store_ent (hv, key, value, 0);
666 786
667 WS; 787 WS;
668 788
669 if (*dec->cur == '}') 789 if (*dec->cur == '}')
670 { 790 {
671 ++dec->cur; 791 ++dec->cur;
672 break; 792 break;
673 } 793 }
674 794
675 if (*dec->cur != ',') 795 if (*dec->cur != ',')
676 ERR (", or } expected while parsing object/hash"); 796 ERR (", or } expected while parsing object/hash");
677 797
678 ++dec->cur; 798 ++dec->cur;
679 } 799 }
680 800
681 return newRV_noinc ((SV *)hv); 801 return newRV_noinc ((SV *)hv);
682 802
683fail: 803fail:
684 SvREFCNT_dec (hv); 804 SvREFCNT_dec (hv);
724 844
725 case 'n': 845 case 'n':
726 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) 846 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4))
727 { 847 {
728 dec->cur += 4; 848 dec->cur += 4;
729 return newSViv (1); 849 return newSVsv (&PL_sv_undef);
730 } 850 }
731 else 851 else
732 ERR ("'null' expected"); 852 ERR ("'null' expected");
733 853
734 break; 854 break;
735 855
736 default: 856 default:
737 ERR ("malformed json string"); 857 ERR ("malformed json string, neither array, object, number, string or atom");
738 break; 858 break;
739 } 859 }
740 860
741fail: 861fail:
742 return 0; 862 return 0;
745static SV * 865static SV *
746decode_json (SV *string, UV flags) 866decode_json (SV *string, UV flags)
747{ 867{
748 SV *sv; 868 SV *sv;
749 869
750 if (!(flags & F_UTF8)) 870 if (flags & F_UTF8)
871 sv_utf8_downgrade (string, 0);
872 else
751 sv_utf8_upgrade (string); 873 sv_utf8_upgrade (string);
752 874
753 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 875 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
754 876
755 dec_t dec; 877 dec_t dec;
756 dec.flags = flags; 878 dec.flags = flags;
757 dec.cur = SvPVX (string); 879 dec.cur = SvPVX (string);
758 dec.end = SvEND (string); 880 dec.end = SvEND (string);
759 dec.err = 0; 881 dec.err = 0;
760 882
761 *dec.end = 1; // invalid anywhere
762 sv = decode_sv (&dec); 883 sv = decode_sv (&dec);
763 *dec.end = 0;
764 884
765 if (!sv) 885 if (!sv)
766 { 886 {
887 IV offset = dec.flags & F_UTF8
888 ? dec.cur - SvPVX (string)
767 IV offset = utf8_distance (dec.cur, SvPVX (string)); 889 : utf8_distance (dec.cur, SvPVX (string));
768 SV *uni = sv_newmortal (); 890 SV *uni = sv_newmortal ();
769 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;
770 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
771 croak ("%s, at character %d (%s)", 901 croak ("%s, at character offset %d (%s)",
772 dec.err, 902 dec.err,
773 (int)offset, 903 (int)offset,
774 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 904 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
775 } 905 }
776 906
777 sv_dump (sv);//D
778 return sv_2mortal (sv); 907 sv = sv_2mortal (sv);
908
909 if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv))
910 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
911
912 return sv;
779} 913}
780 914
781MODULE = JSON::XS PACKAGE = JSON::XS 915MODULE = JSON::XS PACKAGE = JSON::XS
782 916
783BOOT: 917BOOT:
786 920
787 memset (decode_hexdigit, 0xff, 256); 921 memset (decode_hexdigit, 0xff, 256);
788 for (i = 10; i--; ) 922 for (i = 10; i--; )
789 decode_hexdigit ['0' + i] = i; 923 decode_hexdigit ['0' + i] = i;
790 924
791 for (i = 6; --i; ) 925 for (i = 7; i--; )
792 { 926 {
793 decode_hexdigit ['a' + i] = 10 + i; 927 decode_hexdigit ['a' + i] = 10 + i;
794 decode_hexdigit ['A' + i] = 10 + i; 928 decode_hexdigit ['A' + i] = 10 + i;
795 } 929 }
796 930
797 json_stash = gv_stashpv ("JSON::XS", 1); 931 json_stash = gv_stashpv ("JSON::XS", 1);
798} 932}
933
934PROTOTYPES: DISABLE
799 935
800SV *new (char *dummy) 936SV *new (char *dummy)
801 CODE: 937 CODE:
802 RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); 938 RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash);
803 OUTPUT: 939 OUTPUT:
804 RETVAL 940 RETVAL
805 941
806SV *ascii (SV *self, int enable) 942SV *ascii (SV *self, int enable = 1)
807 ALIAS: 943 ALIAS:
808 ascii = F_ASCII 944 ascii = F_ASCII
809 utf8 = F_UTF8 945 utf8 = F_UTF8
810 indent = F_INDENT 946 indent = F_INDENT
811 canonical = F_CANONICAL 947 canonical = F_CANONICAL
812 space_before = F_SPACE_BEFORE 948 space_before = F_SPACE_BEFORE
813 space_after = F_SPACE_AFTER 949 space_after = F_SPACE_AFTER
814 json_rpc = F_JSON_RPC
815 pretty = F_PRETTY 950 pretty = F_PRETTY
951 allow_nonref = F_ALLOW_NONREF
952 shrink = F_SHRINK
816 CODE: 953 CODE:
817{ 954{
818 UV *uv = SvJSON (self); 955 UV *uv = SvJSON (self);
819 if (enable) 956 if (enable)
820 *uv |= ix; 957 *uv |= ix;
832 969
833void decode (SV *self, SV *jsonstr) 970void decode (SV *self, SV *jsonstr)
834 PPCODE: 971 PPCODE:
835 XPUSHs (decode_json (jsonstr, *SvJSON (self))); 972 XPUSHs (decode_json (jsonstr, *SvJSON (self)));
836 973
974PROTOTYPES: ENABLE
975
837void to_json (SV *scalar) 976void to_json (SV *scalar)
838 PPCODE: 977 PPCODE:
839 XPUSHs (encode_json (scalar, F_UTF8)); 978 XPUSHs (encode_json (scalar, F_UTF8));
840 979
841void from_json (SV *jsonstr) 980void from_json (SV *jsonstr)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines