--- JSON-XS/XS.xs 2007/03/25 21:52:47 1.19 +++ JSON-XS/XS.xs 2007/05/09 15:34:04 1.29 @@ -5,6 +5,11 @@ #include "assert.h" #include "string.h" #include "stdlib.h" +#include "stdio.h" + +#if defined(__BORLANDC__) || defined(_MSC_VER) +# define snprintf _snprintf // C compilers have this in stdio.h +#endif #define F_ASCII 0x00000001UL #define F_UTF8 0x00000002UL @@ -23,12 +28,11 @@ // F_BLESSED? <=> { $__class__$ => } #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER -#define F_DEFAULT (13UL << S_MAXDEPTH) +#define F_DEFAULT (9UL << S_MAXDEPTH) #define INIT_SIZE 32 // initial scalar size to be allocated #define INDENT_STEP 3 // spaces per indentation level -#define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character #define SHORT_STRING_LEN 512 // special-case strings of up to this size #define SB do { @@ -78,7 +82,10 @@ return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); } else - return (UV)-1; + { + *clen = (STRLEN)-1; + return (UV)-1; + } } ///////////////////////////////////////////////////////////////////////////// @@ -103,7 +110,7 @@ STRLEN cur = enc->cur - SvPVX (enc->sv); SvGROW (enc->sv, cur + len + 1); enc->cur = SvPVX (enc->sv) + cur; - enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); + enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; } } @@ -210,7 +217,7 @@ } else { - need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed + need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); ++str; } @@ -270,6 +277,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 +355,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))) @@ -401,7 +414,6 @@ } else { - SV *sv; HE *he = hv_iternext (hv); for (;;) @@ -422,6 +434,34 @@ --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) +{ + svtype svt; + + SvGETMAGIC (sv); + 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 +490,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 @@ -476,10 +501,11 @@ static SV * encode_json (SV *scalar, U32 flags) { + enc_t enc; + if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); - enc_t enc; enc.flags = flags; enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); enc.cur = SvPVX (enc.sv); @@ -494,6 +520,7 @@ SvUTF8_on (enc.sv); SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); + *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings if (enc.flags & F_SHRINK) shrink (enc.sv); @@ -575,7 +602,7 @@ do { - char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; + char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; char *cur = buf; do @@ -651,39 +678,45 @@ *cur++ = ch; else if (ch >= 0x80) { + STRLEN clen; + UV uch; + --dec->cur; - STRLEN clen; - UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); + uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); if (clen == (STRLEN)-1) 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); - STRLEN len = cur - buf; + { + STRLEN len = cur - buf; - if (sv) - { - SvGROW (sv, SvCUR (sv) + len + 1); - memcpy (SvPVX (sv) + SvCUR (sv), buf, len); - SvCUR_set (sv, SvCUR (sv) + len); - } - else - sv = newSVpvn (buf, len); + if (sv) + { + SvGROW (sv, SvCUR (sv) + len + 1); + memcpy (SvPVX (sv) + SvCUR (sv), buf, len); + SvCUR_set (sv, SvCUR (sv) + len); + } + else + sv = newSVpvn (buf, len); + } } while (*dec->cur != '"'); @@ -937,7 +970,7 @@ break; default: - ERR ("malformed json string, neither array, object, number, string or atom"); + ERR ("malformed JSON string, neither array, object, number, string or atom"); break; } @@ -948,8 +981,12 @@ static SV * decode_json (SV *string, U32 flags) { + dec_t dec; SV *sv; + SvGETMAGIC (string); + SvUPGRADE (string, SVt_PV); + if (flags & F_UTF8) sv_utf8_downgrade (string, 0); else @@ -957,7 +994,6 @@ SvGROW (string, SvCUR (string) + 1); // should basically be a NOP - dec_t dec; dec.flags = flags; dec.cur = SvPVX (string); dec.end = SvEND (string); @@ -965,7 +1001,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 +1020,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)"); @@ -1051,7 +1087,7 @@ OUTPUT: RETVAL -SV *max_depth (SV *self, int max_depth = 0x80000000UL) +SV *max_depth (SV *self, UV max_depth = 0x80000000UL) CODE: { UV *uv = SvJSON (self);