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.11 by root, Sat Mar 24 19:42:14 2007 UTC vs.
Revision 1.12 by root, Sat Mar 24 22:10:08 2007 UTC

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
22#define INDENT_STEP 3 // spaces per indentation level
23
24#define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character
25#define SHORT_STRING_LEN 256 // special-case strings of up to this size
22 26
23#define SB do { 27#define SB do {
24#define SE } while (0) 28#define SE } while (0)
25 29
26static HV *json_stash; 30static HV *json_stash; // JSON::XS::
31
32/////////////////////////////////////////////////////////////////////////////
33// utility functions
34
35static UV *
36SvJSON (SV *sv)
37{
38 if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash))
39 croak ("object is not of type JSON::XS");
40
41 return &SvUVX (SvRV (sv));
42}
43
44static void
45shrink (SV *sv)
46{
47 sv_utf8_downgrade (sv, 1);
48 if (SvLEN (sv) > SvCUR (sv) + 1)
49 {
50#ifdef SvPV_shrink_to_cur
51 SvPV_shrink_to_cur (sv);
52#elif defined (SvPV_renew)
53 SvPV_renew (sv, SvCUR (sv) + 1);
54#endif
55 }
56}
57
58/////////////////////////////////////////////////////////////////////////////
59// encoder
27 60
28// structure used for encoding JSON 61// structure used for encoding JSON
29typedef struct 62typedef struct
30{ 63{
31 char *cur; 64 char *cur; // SvPVX (sv) + current output position
32 STRLEN len; // SvLEN (sv)
33 char *end; // SvEND (sv) 65 char *end; // SvEND (sv)
34 SV *sv; 66 SV *sv; // result scalar
35 UV flags; 67 UV flags; // F_*
36 int max_recurse; 68 int indent; // indentation level
37 int indent; 69 int max_depth; // max. recursion level
38} enc_t; 70} enc_t;
39
40// structure used for decoding JSON
41typedef struct
42{
43 char *cur;
44 char *end;
45 const char *err;
46 UV flags;
47} dec_t;
48
49static UV *
50SvJSON (SV *sv)
51{
52 if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash))
53 croak ("object is not of type JSON::XS");
54
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
65}
66
67/////////////////////////////////////////////////////////////////////////////
68 71
69static void 72static void
70need (enc_t *enc, STRLEN len) 73need (enc_t *enc, STRLEN len)
71{ 74{
72 if (enc->cur + len >= enc->end) 75 if (enc->cur + len >= enc->end)
178 } 181 }
179 while (--clen); 182 while (--clen);
180 } 183 }
181 else 184 else
182 { 185 {
183 need (enc, len += 10); // never more than 11 bytes needed 186 need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed
184 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 187 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
185 ++str; 188 ++str;
186 } 189 }
187 } 190 }
188 } 191 }
190 193
191 --len; 194 --len;
192 } 195 }
193} 196}
194 197
195#define INDENT SB \ 198static void
199encode_indent (enc_t *enc)
200{
196 if (enc->flags & F_INDENT) \ 201 if (enc->flags & F_INDENT)
197 { \ 202 {
198 int i_; \ 203 int spaces = enc->indent * INDENT_STEP;
199 need (enc, enc->indent); \
200 for (i_ = enc->indent * 3; i_--; )\
201 encode_ch (enc, ' '); \
202 } \
203 SE
204 204
205#define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE 205 need (enc, spaces);
206#define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE 206 memset (enc->cur, ' ', spaces);
207#define COMMA SB \ 207 enc->cur += spaces;
208 }
209}
210
211static void
212encode_space (enc_t *enc)
213{
214 need (enc, 1);
208 encode_ch (enc, ','); \ 215 encode_ch (enc, ' ');
216}
217
218static void
219encode_nl (enc_t *enc)
220{
209 if (enc->flags & F_INDENT) \ 221 if (enc->flags & F_INDENT)
210 NL; \ 222 {
223 need (enc, 1);
224 encode_ch (enc, '\n');
225 }
226}
227
228static void
229encode_comma (enc_t *enc)
230{
231 encode_ch (enc, ',');
232
233 if (enc->flags & F_INDENT)
234 encode_nl (enc);
211 else if (enc->flags & F_SPACE_AFTER) \ 235 else if (enc->flags & F_SPACE_AFTER)
212 SPACE; \ 236 encode_space (enc);
213 SE 237}
214 238
215static void encode_sv (enc_t *enc, SV *sv); 239static void encode_sv (enc_t *enc, SV *sv);
216 240
217static void 241static void
218encode_av (enc_t *enc, AV *av) 242encode_av (enc_t *enc, AV *av)
219{ 243{
220 int i, len = av_len (av); 244 int i, len = av_len (av);
221 245
222 encode_ch (enc, '['); NL; 246 encode_ch (enc, '['); encode_nl (enc);
223 ++enc->indent; 247 ++enc->indent;
224 248
225 for (i = 0; i <= len; ++i) 249 for (i = 0; i <= len; ++i)
226 { 250 {
227 INDENT; 251 encode_indent (enc);
228 encode_sv (enc, *av_fetch (av, i, 0)); 252 encode_sv (enc, *av_fetch (av, i, 0));
229 253
230 if (i < len) 254 if (i < len)
231 COMMA; 255 encode_comma (enc);
232 } 256 }
233 257
234 NL; 258 encode_nl (enc);
235 259
236 --enc->indent; 260 --enc->indent;
237 INDENT; encode_ch (enc, ']'); 261 encode_indent (enc); encode_ch (enc, ']');
238} 262}
239 263
240static void 264static void
241encode_he (enc_t *enc, HE *he) 265encode_he (enc_t *enc, HE *he)
242{ 266{
256 else 280 else
257 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); 281 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he));
258 282
259 encode_ch (enc, '"'); 283 encode_ch (enc, '"');
260 284
261 if (enc->flags & F_SPACE_BEFORE) SPACE; 285 if (enc->flags & F_SPACE_BEFORE) encode_space (enc);
262 encode_ch (enc, ':'); 286 encode_ch (enc, ':');
263 if (enc->flags & F_SPACE_AFTER ) SPACE; 287 if (enc->flags & F_SPACE_AFTER ) encode_space (enc);
264 encode_sv (enc, HeVAL (he)); 288 encode_sv (enc, HeVAL (he));
265} 289}
266 290
267// compare hash entries, used when all keys are bytestrings 291// compare hash entries, used when all keys are bytestrings
268static int 292static int
292static void 316static void
293encode_hv (enc_t *enc, HV *hv) 317encode_hv (enc_t *enc, HV *hv)
294{ 318{
295 int count, i; 319 int count, i;
296 320
297 encode_ch (enc, '{'); NL; ++enc->indent; 321 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent;
298 322
299 if ((count = hv_iterinit (hv))) 323 if ((count = hv_iterinit (hv)))
300 { 324 {
301 // for canonical output we have to sort by keys first 325 // for canonical output we have to sort by keys first
302 // actually, this is mostly due to the stupid so-called 326 // actually, this is mostly due to the stupid so-called
303 // security workaround added somewhere in 5.8.x. 327 // security workaround added somewhere in 5.8.x.
304 // that randomises hash orderings 328 // that randomises hash orderings
305 if (enc->flags & F_CANONICAL) 329 if (enc->flags & F_CANONICAL)
306 { 330 {
307 HE *he, *hes [count]; 331 HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode
308 int fast = 1; 332 int fast = 1;
309 333
310 i = 0; 334 i = 0;
311 while ((he = hv_iternext (hv))) 335 while ((he = hv_iternext (hv)))
312 { 336 {
337 LEAVE; 361 LEAVE;
338 } 362 }
339 363
340 for (i = 0; i < count; ++i) 364 for (i = 0; i < count; ++i)
341 { 365 {
342 INDENT; 366 encode_indent (enc);
343 encode_he (enc, hes [i]); 367 encode_he (enc, hes [i]);
344 368
345 if (i < count - 1) 369 if (i < count - 1)
346 COMMA; 370 encode_comma (enc);
347 } 371 }
348 372
349 NL; 373 encode_nl (enc);
350 } 374 }
351 else 375 else
352 { 376 {
353 SV *sv; 377 SV *sv;
354 HE *he = hv_iternext (hv); 378 HE *he = hv_iternext (hv);
355 379
356 for (;;) 380 for (;;)
357 { 381 {
358 INDENT; 382 encode_indent (enc);
359 encode_he (enc, he); 383 encode_he (enc, he);
360 384
361 if (!(he = hv_iternext (hv))) 385 if (!(he = hv_iternext (hv)))
362 break; 386 break;
363 387
364 COMMA; 388 encode_comma (enc);
365 } 389 }
366 390
367 NL; 391 encode_nl (enc);
368 } 392 }
369 } 393 }
370 394
371 --enc->indent; INDENT; encode_ch (enc, '}'); 395 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
372} 396}
373 397
374static void 398static void
375encode_sv (enc_t *enc, SV *sv) 399encode_sv (enc_t *enc, SV *sv)
376{ 400{
400 } 424 }
401 else if (SvROK (sv)) 425 else if (SvROK (sv))
402 { 426 {
403 SV *rv = SvRV (sv); 427 SV *rv = SvRV (sv);
404 428
405 if (!--enc->max_recurse) 429 if (enc->indent >= enc->max_depth)
406 croak ("data structure too deep (hit recursion limit)"); 430 croak ("data structure too deep (hit recursion limit)");
407 431
408 switch (SvTYPE (rv)) 432 switch (SvTYPE (rv))
409 { 433 {
410 case SVt_PVAV: encode_av (enc, (AV *)rv); break; 434 case SVt_PVAV: encode_av (enc, (AV *)rv); break;
427{ 451{
428 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 452 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar))
429 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 453 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
430 454
431 enc_t enc; 455 enc_t enc;
432 enc.flags = flags; 456 enc.flags = flags;
433 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 457 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
434 enc.cur = SvPVX (enc.sv); 458 enc.cur = SvPVX (enc.sv);
435 enc.end = SvEND (enc.sv); 459 enc.end = SvEND (enc.sv);
436 enc.max_recurse = 0;
437 enc.indent = 0; 460 enc.indent = 0;
461 enc.max_depth = 0x7fffffffUL;
438 462
439 SvPOK_only (enc.sv); 463 SvPOK_only (enc.sv);
440 encode_sv (&enc, scalar); 464 encode_sv (&enc, scalar);
441 465
442 if (!(flags & (F_ASCII | F_UTF8))) 466 if (!(flags & (F_ASCII | F_UTF8)))
449 473
450 return enc.sv; 474 return enc.sv;
451} 475}
452 476
453///////////////////////////////////////////////////////////////////////////// 477/////////////////////////////////////////////////////////////////////////////
478// decoder
454 479
455#define WS \ 480// structure used for decoding JSON
481typedef struct
482{
483 char *cur; // current parser pointer
484 char *end; // end of input string
485 const char *err; // parse error, if != 0
486 UV flags; // F_*
487} dec_t;
488
489static void
490decode_ws (dec_t *dec)
491{
456 for (;;) \ 492 for (;;)
457 { \ 493 {
458 char ch = *dec->cur; \ 494 char ch = *dec->cur;
495
459 if (ch > 0x20 \ 496 if (ch > 0x20
460 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ 497 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09))
461 break; \ 498 break;
499
462 ++dec->cur; \ 500 ++dec->cur;
463 } 501 }
502}
464 503
465#define ERR(reason) SB dec->err = reason; goto fail; SE 504#define ERR(reason) SB dec->err = reason; goto fail; SE
466#define EXPECT_CH(ch) SB \ 505#define EXPECT_CH(ch) SB \
467 if (*dec->cur != ch) \ 506 if (*dec->cur != ch) \
468 ERR (# ch " expected"); \ 507 ERR (# ch " expected"); \
475 514
476static UV 515static UV
477decode_4hex (dec_t *dec) 516decode_4hex (dec_t *dec)
478{ 517{
479 signed char d1, d2, d3, d4; 518 signed char d1, d2, d3, d4;
519 unsigned char *cur = (unsigned char *)dec->cur;
480 520
481 d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]];
482 if (d1 < 0) ERR ("four hexadecimal digits expected"); 521 d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected");
483 d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]];
484 if (d2 < 0) ERR ("four hexadecimal digits expected"); 522 d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected");
485 d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]];
486 if (d3 < 0) ERR ("four hexadecimal digits expected"); 523 d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected");
487 d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]];
488 if (d4 < 0) ERR ("four hexadecimal digits expected"); 524 d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected");
489 525
490 dec->cur += 4; 526 dec->cur += 4;
491 527
492 return ((UV)d1) << 12 528 return ((UV)d1) << 12
493 | ((UV)d2) << 8 529 | ((UV)d2) << 8
496 532
497fail: 533fail:
498 return (UV)-1; 534 return (UV)-1;
499} 535}
500 536
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
516static SV * 537static SV *
517decode_str (dec_t *dec) 538decode_str (dec_t *dec)
518{ 539{
519 SV *sv = NEWSV (0,2); 540 SV *sv = 0;
520 int utf8 = 0; 541 int utf8 = 0;
521 char *cur = SvPVX (sv);
522 char *end = SvEND (sv);
523 542
524 for (;;) 543 do
525 { 544 {
526 unsigned char ch = *(unsigned char *)dec->cur; 545 char buf [SHORT_STRING_LEN + UTF8_MAX_LEN];
546 char *cur = buf;
527 547
528 if (ch == '"') 548 do
529 break;
530 else if (ch == '\\')
531 { 549 {
532 switch (*++dec->cur) 550 unsigned char ch = *(unsigned char *)dec->cur++;
551
552 if (ch == '"')
533 { 553 {
534 case '\\': 554 --dec->cur;
535 case '/': 555 break;
536 case '"': APPEND_CH (*dec->cur++); break; 556 }
537 557 else if (ch == '\\')
538 case 'b': APPEND_CH ('\010'); ++dec->cur; break; 558 {
539 case 't': APPEND_CH ('\011'); ++dec->cur; break; 559 switch (*dec->cur)
540 case 'n': APPEND_CH ('\012'); ++dec->cur; break;
541 case 'f': APPEND_CH ('\014'); ++dec->cur; break;
542 case 'r': APPEND_CH ('\015'); ++dec->cur; break;
543
544 case 'u':
545 { 560 {
546 UV lo, hi; 561 case '\\':
547 ++dec->cur; 562 case '/':
563 case '"': *cur++ = *dec->cur++; break;
548 564
549 hi = decode_4hex (dec); 565 case 'b': ++dec->cur; *cur++ = '\010'; break;
550 if (hi == (UV)-1) 566 case 't': ++dec->cur; *cur++ = '\011'; break;
551 goto fail; 567 case 'n': ++dec->cur; *cur++ = '\012'; break;
568 case 'f': ++dec->cur; *cur++ = '\014'; break;
569 case 'r': ++dec->cur; *cur++ = '\015'; break;
552 570
553 // possibly a surrogate pair 571 case 'u':
554 if (hi >= 0xd800 && hi < 0xdc00)
555 { 572 {
556 if (dec->cur [0] != '\\' || dec->cur [1] != 'u') 573 UV lo, hi;
557 ERR ("missing low surrogate character in surrogate pair");
558
559 dec->cur += 2; 574 ++dec->cur;
560 575
561 lo = decode_4hex (dec); 576 hi = decode_4hex (dec);
562 if (lo == (UV)-1) 577 if (hi == (UV)-1)
563 goto fail; 578 goto fail;
564 579
580 // possibly a surrogate pair
581 if (hi >= 0xd800)
582 if (hi < 0xdc00)
583 {
584 if (dec->cur [0] != '\\' || dec->cur [1] != 'u')
585 ERR ("missing low surrogate character in surrogate pair");
586
587 dec->cur += 2;
588
589 lo = decode_4hex (dec);
590 if (lo == (UV)-1)
591 goto fail;
592
565 if (lo < 0xdc00 || lo >= 0xe000) 593 if (lo < 0xdc00 || lo >= 0xe000)
566 ERR ("surrogate pair expected"); 594 ERR ("surrogate pair expected");
567 595
568 hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; 596 hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000;
597 }
598 else if (hi < 0xe000)
599 ERR ("missing high surrogate character in surrogate pair");
600
601 if (hi >= 0x80)
602 {
603 utf8 = 1;
604
605 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0);
606 }
607 else
608 *cur++ = hi;
569 } 609 }
570 else if (hi >= 0xdc00 && hi < 0xe000)
571 ERR ("missing high surrogate character in surrogate pair");
572
573 if (hi >= 0x80)
574 { 610 break;
575 utf8 = 1;
576 611
577 APPEND_GROW (4); // at most 4 bytes for 21 bits
578 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0);
579 }
580 else 612 default:
581 APPEND_CH (hi); 613 --dec->cur;
614 ERR ("illegal backslash escape sequence in string");
582 } 615 }
583 break;
584
585 default:
586 --dec->cur;
587 ERR ("illegal backslash escape sequence in string");
588 } 616 }
617 else if (ch >= 0x20 && ch <= 0x7f)
618 *cur++ = ch;
619 else if (ch >= 0x80)
620 {
621 --dec->cur;
622
623 STRLEN clen;
624 UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY);
625 if (clen == (STRLEN)-1)
626 ERR ("malformed UTF-8 character in JSON string");
627
628 do
629 {
630 *cur++ = *dec->cur++;
631 }
632 while (--clen);
633
634 utf8 = 1;
635 }
636 else if (!ch)
637 ERR ("unexpected end of string while parsing json string");
638 else
639 ERR ("invalid character encountered");
640
589 } 641 }
590 else if (ch >= 0x20 && ch <= 0x7f) 642 while (cur < buf + SHORT_STRING_LEN);
591 APPEND_CH (*dec->cur++); 643
592 else if (ch >= 0x80) 644 STRLEN len = cur - buf;
645
646 if (sv)
593 { 647 {
594 STRLEN clen; 648 SvGROW (sv, SvCUR (sv) + len + 1);
595 UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); 649 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
596 if (clen == (STRLEN)-1) 650 SvCUR_set (sv, SvCUR (sv) + len);
597 ERR ("malformed UTF-8 character in JSON string");
598
599 APPEND_GROW (clen);
600 do
601 {
602 *cur++ = *dec->cur++;
603 }
604 while (--clen);
605
606 utf8 = 1;
607 } 651 }
608 else if (dec->cur == dec->end)
609 ERR ("unexpected end of string while parsing json string");
610 else 652 else
611 ERR ("invalid character encountered"); 653 sv = newSVpvn (buf, len);
612 } 654 }
655 while (*dec->cur != '"');
613 656
614 ++dec->cur; 657 ++dec->cur;
615 658
616 SvCUR_set (sv, cur - SvPVX (sv)); 659 if (sv)
617 660 {
618 SvPOK_only (sv); 661 SvPOK_only (sv);
619 *SvEND (sv) = 0; 662 *SvEND (sv) = 0;
620 663
621 if (utf8) 664 if (utf8)
622 SvUTF8_on (sv); 665 SvUTF8_on (sv);
623 666 }
624 if (dec->flags & F_SHRINK) 667 else
625 shrink (sv); 668 sv = newSVpvn ("", 0);
626 669
627 return sv; 670 return sv;
628 671
629fail: 672fail:
630 SvREFCNT_dec (sv);
631 return 0; 673 return 0;
632} 674}
633 675
634static SV * 676static SV *
635decode_num (dec_t *dec) 677decode_num (dec_t *dec)
716static SV * 758static SV *
717decode_av (dec_t *dec) 759decode_av (dec_t *dec)
718{ 760{
719 AV *av = newAV (); 761 AV *av = newAV ();
720 762
721 WS; 763 decode_ws (dec);
722 if (*dec->cur == ']') 764 if (*dec->cur == ']')
723 ++dec->cur; 765 ++dec->cur;
724 else 766 else
725 for (;;) 767 for (;;)
726 { 768 {
730 if (!value) 772 if (!value)
731 goto fail; 773 goto fail;
732 774
733 av_push (av, value); 775 av_push (av, value);
734 776
735 WS; 777 decode_ws (dec);
736 778
737 if (*dec->cur == ']') 779 if (*dec->cur == ']')
738 { 780 {
739 ++dec->cur; 781 ++dec->cur;
740 break; 782 break;
756static SV * 798static SV *
757decode_hv (dec_t *dec) 799decode_hv (dec_t *dec)
758{ 800{
759 HV *hv = newHV (); 801 HV *hv = newHV ();
760 802
761 WS; 803 decode_ws (dec);
762 if (*dec->cur == '}') 804 if (*dec->cur == '}')
763 ++dec->cur; 805 ++dec->cur;
764 else 806 else
765 for (;;) 807 for (;;)
766 { 808 {
767 SV *key, *value; 809 SV *key, *value;
768 810
769 WS; EXPECT_CH ('"'); 811 decode_ws (dec); EXPECT_CH ('"');
770 812
771 key = decode_str (dec); 813 key = decode_str (dec);
772 if (!key) 814 if (!key)
773 goto fail; 815 goto fail;
774 816
775 WS; EXPECT_CH (':'); 817 decode_ws (dec); EXPECT_CH (':');
776 818
777 value = decode_sv (dec); 819 value = decode_sv (dec);
778 if (!value) 820 if (!value)
779 { 821 {
780 SvREFCNT_dec (key); 822 SvREFCNT_dec (key);
782 } 824 }
783 825
784 //TODO: optimise 826 //TODO: optimise
785 hv_store_ent (hv, key, value, 0); 827 hv_store_ent (hv, key, value, 0);
786 828
787 WS; 829 decode_ws (dec);
788 830
789 if (*dec->cur == '}') 831 if (*dec->cur == '}')
790 { 832 {
791 ++dec->cur; 833 ++dec->cur;
792 break; 834 break;
806} 848}
807 849
808static SV * 850static SV *
809decode_sv (dec_t *dec) 851decode_sv (dec_t *dec)
810{ 852{
811 WS; 853 decode_ws (dec);
812 switch (*dec->cur) 854 switch (*dec->cur)
813 { 855 {
814 case '"': ++dec->cur; return decode_str (dec); 856 case '"': ++dec->cur; return decode_str (dec);
815 case '[': ++dec->cur; return decode_av (dec); 857 case '[': ++dec->cur; return decode_av (dec);
816 case '{': ++dec->cur; return decode_hv (dec); 858 case '{': ++dec->cur; return decode_hv (dec);
909 if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) 951 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)"); 952 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
911 953
912 return sv; 954 return sv;
913} 955}
956
957/////////////////////////////////////////////////////////////////////////////
958// XS interface functions
914 959
915MODULE = JSON::XS PACKAGE = JSON::XS 960MODULE = JSON::XS PACKAGE = JSON::XS
916 961
917BOOT: 962BOOT:
918{ 963{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines