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.19 by root, Sun Mar 25 21:52:47 2007 UTC vs.
Revision 1.24 by root, Tue Apr 3 23:59:04 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 {
420 } 429 }
421 430
422 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 431 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
423} 432}
424 433
434// encode objects, arrays and special \0=false and \1=true values.
435static void
436encode_rv (enc_t *enc, SV *sv)
437{
438 SvGETMAGIC (sv);
439
440 svtype 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))));
459}
460
425static void 461static void
426encode_sv (enc_t *enc, SV *sv) 462encode_sv (enc_t *enc, SV *sv)
427{ 463{
428 SvGETMAGIC (sv); 464 SvGETMAGIC (sv);
429 465
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));
657 UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); 678 UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen);
658 if (clen == (STRLEN)-1) 679 if (clen == (STRLEN)-1)
659 ERR ("malformed UTF-8 character in JSON string"); 680 ERR ("malformed UTF-8 character in JSON string");
660 681
661 do 682 do
662 {
663 *cur++ = *dec->cur++; 683 *cur++ = *dec->cur++;
664 }
665 while (--clen); 684 while (--clen);
666 685
667 utf8 = 1; 686 utf8 = 1;
668 } 687 }
669 else if (!ch)
670 ERR ("unexpected end of string while parsing json string");
671 else 688 else
689 {
690 --dec->cur;
691
692 if (!ch)
693 ERR ("unexpected end of string while parsing JSON string");
694 else
672 ERR ("invalid character encountered"); 695 ERR ("invalid character encountered while parsing JSON string");
673 696 }
674 } 697 }
675 while (cur < buf + SHORT_STRING_LEN); 698 while (cur < buf + SHORT_STRING_LEN);
676 699
677 STRLEN len = cur - buf; 700 STRLEN len = cur - buf;
678 701
935 ERR ("'null' expected"); 958 ERR ("'null' expected");
936 959
937 break; 960 break;
938 961
939 default: 962 default:
940 ERR ("malformed json string, neither array, object, number, string or atom"); 963 ERR ("malformed JSON string, neither array, object, number, string or atom");
941 break; 964 break;
942 } 965 }
943 966
944fail: 967fail:
945 return 0; 968 return 0;
947 970
948static SV * 971static SV *
949decode_json (SV *string, U32 flags) 972decode_json (SV *string, U32 flags)
950{ 973{
951 SV *sv; 974 SV *sv;
975
976 SvUPGRADE (string, SVt_PV);
952 977
953 if (flags & F_UTF8) 978 if (flags & F_UTF8)
954 sv_utf8_downgrade (string, 0); 979 sv_utf8_downgrade (string, 0);
955 else 980 else
956 sv_utf8_upgrade (string); 981 sv_utf8_upgrade (string);
963 dec.end = SvEND (string); 988 dec.end = SvEND (string);
964 dec.err = 0; 989 dec.err = 0;
965 dec.depth = 0; 990 dec.depth = 0;
966 dec.maxdepth = DEC_DEPTH (dec.flags); 991 dec.maxdepth = DEC_DEPTH (dec.flags);
967 992
968 *SvEND (string) = 0; // this should basically be a nop, too 993 *dec.end = 0; // this should basically be a nop, too, but make sure its there
969 sv = decode_sv (&dec); 994 sv = decode_sv (&dec);
970 995
971 if (!sv) 996 if (!sv)
972 { 997 {
973 IV offset = dec.flags & F_UTF8 998 IV offset = dec.flags & F_UTF8
982 SAVEVPTR (PL_curcop); 1007 SAVEVPTR (PL_curcop);
983 PL_curcop = &cop; 1008 PL_curcop = &cop;
984 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1009 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
985 LEAVE; 1010 LEAVE;
986 1011
987 croak ("%s, at character offset %d (%s)", 1012 croak ("%s, at character offset %d [\"%s\"]",
988 dec.err, 1013 dec.err,
989 (int)offset, 1014 (int)offset,
990 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1015 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
991 } 1016 }
992 1017

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines