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.20 by root, Sun Mar 25 21:53:19 2007 UTC vs.
Revision 1.25 by root, Fri Apr 6 20:37:22 2007 UTC

21 21
22// F_SELFCONVERT? <=> to_json/toJson 22// F_SELFCONVERT? <=> to_json/toJson
23// F_BLESSED? <=> { $__class__$ => } 23// F_BLESSED? <=> { $__class__$ => }
24 24
25#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 25#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
26#define F_DEFAULT (13UL << S_MAXDEPTH) 26#define F_DEFAULT (9UL << S_MAXDEPTH)
27 27
28#define INIT_SIZE 32 // initial scalar size to be allocated 28#define INIT_SIZE 32 // initial scalar size to be allocated
29#define INDENT_STEP 3 // spaces per indentation level 29#define INDENT_STEP 3 // spaces per indentation level
30 30
31#define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character 31#define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character
76 { 76 {
77 *clen = 2; 77 *clen = 2;
78 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 78 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
79 } 79 }
80 else 80 else
81 {
82 *clen = (STRLEN)-1;
81 return (UV)-1; 83 return (UV)-1;
84 }
82} 85}
83 86
84///////////////////////////////////////////////////////////////////////////// 87/////////////////////////////////////////////////////////////////////////////
85// encoder 88// encoder
86 89
268static void 271static void
269encode_av (enc_t *enc, AV *av) 272encode_av (enc_t *enc, AV *av)
270{ 273{
271 int i, len = av_len (av); 274 int i, len = av_len (av);
272 275
276 if (enc->indent >= enc->maxdepth)
277 croak ("data structure too deep (hit recursion limit)");
278
273 encode_ch (enc, '['); encode_nl (enc); 279 encode_ch (enc, '['); encode_nl (enc);
274 ++enc->indent; 280 ++enc->indent;
275 281
276 for (i = 0; i <= len; ++i) 282 for (i = 0; i <= len; ++i)
277 { 283 {
342 348
343static void 349static void
344encode_hv (enc_t *enc, HV *hv) 350encode_hv (enc_t *enc, HV *hv)
345{ 351{
346 int count, i; 352 int count, i;
353
354 if (enc->indent >= enc->maxdepth)
355 croak ("data structure too deep (hit recursion limit)");
347 356
348 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 357 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent;
349 358
350 if ((count = hv_iterinit (hv))) 359 if ((count = hv_iterinit (hv)))
351 { 360 {
399 408
400 encode_nl (enc); 409 encode_nl (enc);
401 } 410 }
402 else 411 else
403 { 412 {
404 SV *sv;
405 HE *he = hv_iternext (hv); 413 HE *he = hv_iternext (hv);
406 414
407 for (;;) 415 for (;;)
408 { 416 {
409 encode_indent (enc); 417 encode_indent (enc);
418 encode_nl (enc); 426 encode_nl (enc);
419 } 427 }
420 } 428 }
421 429
422 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 430 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
431}
432
433// encode objects, arrays and special \0=false and \1=true values.
434static void
435encode_rv (enc_t *enc, SV *sv)
436{
437 svtype svt;
438
439 SvGETMAGIC (sv);
440 svt = SvTYPE (sv);
441
442 if (svt == SVt_PVHV)
443 encode_hv (enc, (HV *)sv);
444 else if (svt == SVt_PVAV)
445 encode_av (enc, (AV *)sv);
446 else if (svt < SVt_PVAV)
447 {
448 if (SvNIOK (sv) && SvIV (sv) == 0)
449 encode_str (enc, "false", 5, 0);
450 else if (SvNIOK (sv) && SvIV (sv) == 1)
451 encode_str (enc, "true", 4, 0);
452 else
453 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
454 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
455 }
456 else
457 croak ("encountered %s, but JSON can only represent references to arrays or hashes",
458 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
423} 459}
424 460
425static void 461static void
426encode_sv (enc_t *enc, SV *sv) 462encode_sv (enc_t *enc, SV *sv)
427{ 463{
448 SvIsUV(sv) 484 SvIsUV(sv)
449 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) 485 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv))
450 : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); 486 : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv));
451 } 487 }
452 else if (SvROK (sv)) 488 else if (SvROK (sv))
453 { 489 encode_rv (enc, SvRV (sv));
454 SV *rv = SvRV (sv);
455
456 if (enc->indent >= enc->maxdepth)
457 croak ("data structure too deep (hit recursion limit)");
458
459 switch (SvTYPE (rv))
460 {
461 case SVt_PVAV: encode_av (enc, (AV *)rv); break;
462 case SVt_PVHV: encode_hv (enc, (HV *)rv); break;
463
464 default:
465 croak ("encountered %s, but JSON can only represent references to arrays or hashes",
466 SvPV_nolen (sv));
467 }
468 }
469 else if (!SvOK (sv)) 490 else if (!SvOK (sv))
470 encode_str (enc, "null", 4, 0); 491 encode_str (enc, "null", 4, 0);
471 else 492 else
472 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 493 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this",
473 SvPV_nolen (sv), SvFLAGS (sv)); 494 SvPV_nolen (sv), SvFLAGS (sv));
474} 495}
475 496
476static SV * 497static SV *
477encode_json (SV *scalar, U32 flags) 498encode_json (SV *scalar, U32 flags)
478{ 499{
500 enc_t enc;
501
479 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 502 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar))
480 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 503 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
481 504
482 enc_t enc;
483 enc.flags = flags; 505 enc.flags = flags;
484 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 506 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
485 enc.cur = SvPVX (enc.sv); 507 enc.cur = SvPVX (enc.sv);
486 enc.end = SvEND (enc.sv); 508 enc.end = SvEND (enc.sv);
487 enc.indent = 0; 509 enc.indent = 0;
649 } 671 }
650 else if (ch >= 0x20 && ch <= 0x7f) 672 else if (ch >= 0x20 && ch <= 0x7f)
651 *cur++ = ch; 673 *cur++ = ch;
652 else if (ch >= 0x80) 674 else if (ch >= 0x80)
653 { 675 {
676 STRLEN clen;
677 UV uch;
678
654 --dec->cur; 679 --dec->cur;
655 680
656 STRLEN clen;
657 UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); 681 uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen);
658 if (clen == (STRLEN)-1) 682 if (clen == (STRLEN)-1)
659 ERR ("malformed UTF-8 character in JSON string"); 683 ERR ("malformed UTF-8 character in JSON string");
660 684
661 do 685 do
662 {
663 *cur++ = *dec->cur++; 686 *cur++ = *dec->cur++;
664 }
665 while (--clen); 687 while (--clen);
666 688
667 utf8 = 1; 689 utf8 = 1;
668 } 690 }
669 else if (!ch)
670 ERR ("unexpected end of string while parsing json string");
671 else 691 else
692 {
693 --dec->cur;
694
695 if (!ch)
696 ERR ("unexpected end of string while parsing JSON string");
697 else
672 ERR ("invalid character encountered"); 698 ERR ("invalid character encountered while parsing JSON string");
673 699 }
674 } 700 }
675 while (cur < buf + SHORT_STRING_LEN); 701 while (cur < buf + SHORT_STRING_LEN);
676 702
703 {
677 STRLEN len = cur - buf; 704 STRLEN len = cur - buf;
678 705
679 if (sv) 706 if (sv)
680 { 707 {
681 SvGROW (sv, SvCUR (sv) + len + 1); 708 SvGROW (sv, SvCUR (sv) + len + 1);
682 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 709 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
683 SvCUR_set (sv, SvCUR (sv) + len); 710 SvCUR_set (sv, SvCUR (sv) + len);
684 } 711 }
685 else 712 else
686 sv = newSVpvn (buf, len); 713 sv = newSVpvn (buf, len);
714 }
687 } 715 }
688 while (*dec->cur != '"'); 716 while (*dec->cur != '"');
689 717
690 ++dec->cur; 718 ++dec->cur;
691 719
935 ERR ("'null' expected"); 963 ERR ("'null' expected");
936 964
937 break; 965 break;
938 966
939 default: 967 default:
940 ERR ("malformed json string, neither array, object, number, string or atom"); 968 ERR ("malformed JSON string, neither array, object, number, string or atom");
941 break; 969 break;
942 } 970 }
943 971
944fail: 972fail:
945 return 0; 973 return 0;
946} 974}
947 975
948static SV * 976static SV *
949decode_json (SV *string, U32 flags) 977decode_json (SV *string, U32 flags)
950{ 978{
979 dec_t dec;
951 SV *sv; 980 SV *sv;
981
982 SvUPGRADE (string, SVt_PV);
952 983
953 if (flags & F_UTF8) 984 if (flags & F_UTF8)
954 sv_utf8_downgrade (string, 0); 985 sv_utf8_downgrade (string, 0);
955 else 986 else
956 sv_utf8_upgrade (string); 987 sv_utf8_upgrade (string);
957 988
958 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 989 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
959 990
960 dec_t dec;
961 dec.flags = flags; 991 dec.flags = flags;
962 dec.cur = SvPVX (string); 992 dec.cur = SvPVX (string);
963 dec.end = SvEND (string); 993 dec.end = SvEND (string);
964 dec.err = 0; 994 dec.err = 0;
965 dec.depth = 0; 995 dec.depth = 0;
982 SAVEVPTR (PL_curcop); 1012 SAVEVPTR (PL_curcop);
983 PL_curcop = &cop; 1013 PL_curcop = &cop;
984 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1014 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
985 LEAVE; 1015 LEAVE;
986 1016
987 croak ("%s, at character offset %d (%s)", 1017 croak ("%s, at character offset %d [\"%s\"]",
988 dec.err, 1018 dec.err,
989 (int)offset, 1019 (int)offset,
990 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1020 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
991 } 1021 }
992 1022
1049 RETVAL = newSVsv (self); 1079 RETVAL = newSVsv (self);
1050} 1080}
1051 OUTPUT: 1081 OUTPUT:
1052 RETVAL 1082 RETVAL
1053 1083
1054SV *max_depth (SV *self, int max_depth = 0x80000000UL) 1084SV *max_depth (SV *self, UV max_depth = 0x80000000UL)
1055 CODE: 1085 CODE:
1056{ 1086{
1057 UV *uv = SvJSON (self); 1087 UV *uv = SvJSON (self);
1058 UV log2 = 0; 1088 UV log2 = 0;
1059 1089

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines