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.22 by root, Sat Mar 31 14:20:06 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 (12UL << 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
405 408
406 encode_nl (enc); 409 encode_nl (enc);
407 } 410 }
408 else 411 else
409 { 412 {
410 SV *sv;
411 HE *he = hv_iternext (hv); 413 HE *he = hv_iternext (hv);
412 414
413 for (;;) 415 for (;;)
414 { 416 {
415 encode_indent (enc); 417 encode_indent (enc);
430 432
431// encode objects, arrays and special \0=false and \1=true values. 433// encode objects, arrays and special \0=false and \1=true values.
432static void 434static void
433encode_rv (enc_t *enc, SV *sv) 435encode_rv (enc_t *enc, SV *sv)
434{ 436{
437 svtype svt;
438
435 SvGETMAGIC (sv); 439 SvGETMAGIC (sv);
436
437 svtype svt = SvTYPE (sv); 440 svt = SvTYPE (sv);
438 441
439 if (svt == SVt_PVHV) 442 if (svt == SVt_PVHV)
440 encode_hv (enc, (HV *)sv); 443 encode_hv (enc, (HV *)sv);
441 else if (svt == SVt_PVAV) 444 else if (svt == SVt_PVAV)
442 encode_av (enc, (AV *)sv); 445 encode_av (enc, (AV *)sv);
492} 495}
493 496
494static SV * 497static SV *
495encode_json (SV *scalar, U32 flags) 498encode_json (SV *scalar, U32 flags)
496{ 499{
500 enc_t enc;
501
497 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 502 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar))
498 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)");
499 504
500 enc_t enc;
501 enc.flags = flags; 505 enc.flags = flags;
502 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 506 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
503 enc.cur = SvPVX (enc.sv); 507 enc.cur = SvPVX (enc.sv);
504 enc.end = SvEND (enc.sv); 508 enc.end = SvEND (enc.sv);
505 enc.indent = 0; 509 enc.indent = 0;
667 } 671 }
668 else if (ch >= 0x20 && ch <= 0x7f) 672 else if (ch >= 0x20 && ch <= 0x7f)
669 *cur++ = ch; 673 *cur++ = ch;
670 else if (ch >= 0x80) 674 else if (ch >= 0x80)
671 { 675 {
676 STRLEN clen;
677 UV uch;
678
672 --dec->cur; 679 --dec->cur;
673 680
674 STRLEN clen;
675 UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); 681 uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen);
676 if (clen == (STRLEN)-1) 682 if (clen == (STRLEN)-1)
677 ERR ("malformed UTF-8 character in JSON string"); 683 ERR ("malformed UTF-8 character in JSON string");
678 684
679 do 685 do
680 {
681 *cur++ = *dec->cur++; 686 *cur++ = *dec->cur++;
682 }
683 while (--clen); 687 while (--clen);
684 688
685 utf8 = 1; 689 utf8 = 1;
686 } 690 }
687 else if (!ch)
688 ERR ("unexpected end of string while parsing json string");
689 else 691 else
692 {
693 --dec->cur;
694
695 if (!ch)
696 ERR ("unexpected end of string while parsing JSON string");
697 else
690 ERR ("invalid character encountered"); 698 ERR ("invalid character encountered while parsing JSON string");
691 699 }
692 } 700 }
693 while (cur < buf + SHORT_STRING_LEN); 701 while (cur < buf + SHORT_STRING_LEN);
694 702
703 {
695 STRLEN len = cur - buf; 704 STRLEN len = cur - buf;
696 705
697 if (sv) 706 if (sv)
698 { 707 {
699 SvGROW (sv, SvCUR (sv) + len + 1); 708 SvGROW (sv, SvCUR (sv) + len + 1);
700 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 709 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
701 SvCUR_set (sv, SvCUR (sv) + len); 710 SvCUR_set (sv, SvCUR (sv) + len);
702 } 711 }
703 else 712 else
704 sv = newSVpvn (buf, len); 713 sv = newSVpvn (buf, len);
714 }
705 } 715 }
706 while (*dec->cur != '"'); 716 while (*dec->cur != '"');
707 717
708 ++dec->cur; 718 ++dec->cur;
709 719
953 ERR ("'null' expected"); 963 ERR ("'null' expected");
954 964
955 break; 965 break;
956 966
957 default: 967 default:
958 ERR ("malformed json string, neither array, object, number, string or atom"); 968 ERR ("malformed JSON string, neither array, object, number, string or atom");
959 break; 969 break;
960 } 970 }
961 971
962fail: 972fail:
963 return 0; 973 return 0;
964} 974}
965 975
966static SV * 976static SV *
967decode_json (SV *string, U32 flags) 977decode_json (SV *string, U32 flags)
968{ 978{
979 dec_t dec;
969 SV *sv; 980 SV *sv;
970 981
971 SvUPGRADE (string, SVt_PV); 982 SvUPGRADE (string, SVt_PV);
972 983
973 if (flags & F_UTF8) 984 if (flags & F_UTF8)
975 else 986 else
976 sv_utf8_upgrade (string); 987 sv_utf8_upgrade (string);
977 988
978 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 989 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
979 990
980 dec_t dec;
981 dec.flags = flags; 991 dec.flags = flags;
982 dec.cur = SvPVX (string); 992 dec.cur = SvPVX (string);
983 dec.end = SvEND (string); 993 dec.end = SvEND (string);
984 dec.err = 0; 994 dec.err = 0;
985 dec.depth = 0; 995 dec.depth = 0;
1002 SAVEVPTR (PL_curcop); 1012 SAVEVPTR (PL_curcop);
1003 PL_curcop = &cop; 1013 PL_curcop = &cop;
1004 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);
1005 LEAVE; 1015 LEAVE;
1006 1016
1007 croak ("%s, at character offset %d (%s)", 1017 croak ("%s, at character offset %d [\"%s\"]",
1008 dec.err, 1018 dec.err,
1009 (int)offset, 1019 (int)offset,
1010 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1020 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1011 } 1021 }
1012 1022
1069 RETVAL = newSVsv (self); 1079 RETVAL = newSVsv (self);
1070} 1080}
1071 OUTPUT: 1081 OUTPUT:
1072 RETVAL 1082 RETVAL
1073 1083
1074SV *max_depth (SV *self, int max_depth = 0x80000000UL) 1084SV *max_depth (SV *self, UV max_depth = 0x80000000UL)
1075 CODE: 1085 CODE:
1076{ 1086{
1077 UV *uv = SvJSON (self); 1087 UV *uv = SvJSON (self);
1078 UV log2 = 0; 1088 UV log2 = 0;
1079 1089

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines