--- JSON-XS/XS.xs 2007/03/25 21:52:47 1.19 +++ JSON-XS/XS.xs 2007/04/03 23:34:17 1.23 @@ -23,7 +23,7 @@ // F_BLESSED? <=> { $__class__$ => } #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER -#define F_DEFAULT (13UL << S_MAXDEPTH) +#define F_DEFAULT (12UL << S_MAXDEPTH) #define INIT_SIZE 32 // initial scalar size to be allocated #define INDENT_STEP 3 // spaces per indentation level @@ -78,7 +78,10 @@ return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); } else - return (UV)-1; + { + *clen = (STRLEN)-1; + return (UV)-1; + } } ///////////////////////////////////////////////////////////////////////////// @@ -270,6 +273,9 @@ { int i, len = av_len (av); + if (enc->indent >= enc->maxdepth) + croak ("data structure too deep (hit recursion limit)"); + encode_ch (enc, '['); encode_nl (enc); ++enc->indent; @@ -345,6 +351,9 @@ { int count, i; + if (enc->indent >= enc->maxdepth) + croak ("data structure too deep (hit recursion limit)"); + encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; if ((count = hv_iterinit (hv))) @@ -422,6 +431,33 @@ --enc->indent; encode_indent (enc); encode_ch (enc, '}'); } +// encode objects, arrays and special \0=false and \1=true values. +static void +encode_rv (enc_t *enc, SV *sv) +{ + SvGETMAGIC (sv); + + svtype svt = SvTYPE (sv); + + if (svt == SVt_PVHV) + encode_hv (enc, (HV *)sv); + else if (svt == SVt_PVAV) + encode_av (enc, (AV *)sv); + else if (svt < SVt_PVAV) + { + if (SvNIOK (sv) && SvIV (sv) == 0) + encode_str (enc, "false", 5, 0); + else if (SvNIOK (sv) && SvIV (sv) == 1) + encode_str (enc, "true", 4, 0); + else + croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", + SvPV_nolen (sv_2mortal (newRV_inc (sv)))); + } + else + croak ("encountered %s, but JSON can only represent references to arrays or hashes", + SvPV_nolen (sv_2mortal (newRV_inc (sv)))); +} + static void encode_sv (enc_t *enc, SV *sv) { @@ -450,22 +486,7 @@ : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); } else if (SvROK (sv)) - { - SV *rv = SvRV (sv); - - if (enc->indent >= enc->maxdepth) - croak ("data structure too deep (hit recursion limit)"); - - switch (SvTYPE (rv)) - { - case SVt_PVAV: encode_av (enc, (AV *)rv); break; - case SVt_PVHV: encode_hv (enc, (HV *)rv); break; - - default: - croak ("encountered %s, but JSON can only represent references to arrays or hashes", - SvPV_nolen (sv)); - } - } + encode_rv (enc, SvRV (sv)); else if (!SvOK (sv)) encode_str (enc, "null", 4, 0); else @@ -659,18 +680,20 @@ ERR ("malformed UTF-8 character in JSON string"); do - { - *cur++ = *dec->cur++; - } + *cur++ = *dec->cur++; while (--clen); utf8 = 1; } - else if (!ch) - ERR ("unexpected end of string while parsing json string"); else - ERR ("invalid character encountered"); + { + --dec->cur; + if (!ch) + ERR ("unexpected end of string while parsing json string"); + else + ERR ("invalid character encountered while parsing json string"); + } } while (cur < buf + SHORT_STRING_LEN); @@ -950,6 +973,8 @@ { SV *sv; + SvUPGRADE (string, SVt_PV); + if (flags & F_UTF8) sv_utf8_downgrade (string, 0); else @@ -965,7 +990,7 @@ dec.depth = 0; dec.maxdepth = DEC_DEPTH (dec.flags); - *SvEND (string) = 0; // this should basically be a nop, too + *dec.end = 0; // this should basically be a nop, too, but make sure its there sv = decode_sv (&dec); if (!sv) @@ -984,7 +1009,7 @@ pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); LEAVE; - croak ("%s, at character offset %d (%s)", + croak ("%s, at character offset %d [\"%s\"]", dec.err, (int)offset, dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");