--- JSON-XS/XS.xs 2007/03/22 16:40:16 1.1 +++ JSON-XS/XS.xs 2007/03/22 18:10:29 1.3 @@ -13,7 +13,9 @@ #define F_SPACE_BEFORE 0x00000010 #define F_SPACE_AFTER 0x00000020 #define F_JSON_RPC 0x00000040 +#define F_ALLOW_NONREF 0x00000080 +#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER #define F_DEFAULT 0 #define INIT_SIZE 32 // initial scalar size to be allocated @@ -148,7 +150,7 @@ } \ SE -#define SPACE SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, ' '); } SE +#define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE #define COMMA SB \ encode_ch (enc, ','); \ @@ -360,6 +362,9 @@ static SV * encode_json (SV *scalar, UV flags) { + if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) + croak ("hash- or arraref required (not a simple scalar, use allow_nonref to allow this)"); + enc_t enc; enc.flags = flags; enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); @@ -773,8 +778,12 @@ dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); } - sv_dump (sv);//D - return sv_2mortal (sv); + sv = sv_2mortal (sv); + + if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) + croak ("JSON object or array expected (but number, string, true, false or null found, use allow_nonref to allow this)"); + + return sv; } MODULE = JSON::XS PACKAGE = JSON::XS @@ -811,6 +820,8 @@ space_before = F_SPACE_BEFORE space_after = F_SPACE_AFTER json_rpc = F_JSON_RPC + pretty = F_PRETTY + allow_nonref = F_ALLOW_NONREF CODE: { UV *uv = SvJSON (self); @@ -828,7 +839,15 @@ PPCODE: XPUSHs (encode_json (scalar, *SvJSON (self))); -void decode (SV *self, SV *jsondata) +void decode (SV *self, SV *jsonstr) + PPCODE: + XPUSHs (decode_json (jsonstr, *SvJSON (self))); + +void to_json (SV *scalar) + PPCODE: + XPUSHs (encode_json (scalar, F_UTF8)); + +void from_json (SV *jsonstr) PPCODE: - XPUSHs (decode_json (jsondata, *SvJSON (self))); + XPUSHs (decode_json (jsonstr, F_UTF8));