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.43 by root, Sat Jun 23 23:49:29 2007 UTC vs.
Revision 1.44 by root, Mon Jun 25 04:08:17 2007 UTC

15// guarentees, though. if it breaks, you get to keep the pieces. 15// guarentees, though. if it breaks, you get to keep the pieces.
16#ifndef UTF8_MAXBYTES 16#ifndef UTF8_MAXBYTES
17# define UTF8_MAXBYTES 13 17# define UTF8_MAXBYTES 13
18#endif 18#endif
19 19
20#define F_ASCII 0x00000001UL 20#define F_ASCII 0x00000001UL
21#define F_LATIN1 0x00000002UL 21#define F_LATIN1 0x00000002UL
22#define F_UTF8 0x00000004UL 22#define F_UTF8 0x00000004UL
23#define F_INDENT 0x00000008UL 23#define F_INDENT 0x00000008UL
24#define F_CANONICAL 0x00000010UL 24#define F_CANONICAL 0x00000010UL
25#define F_SPACE_BEFORE 0x00000020UL 25#define F_SPACE_BEFORE 0x00000020UL
26#define F_SPACE_AFTER 0x00000040UL 26#define F_SPACE_AFTER 0x00000040UL
27#define F_ALLOW_NONREF 0x00000100UL 27#define F_ALLOW_NONREF 0x00000100UL
28#define F_SHRINK 0x00000200UL 28#define F_SHRINK 0x00000200UL
29#define F_ALLOW_BLESSED 0x00000400UL
30#define F_CONV_BLESSED 0x00000800UL // NYI
29#define F_MAXDEPTH 0xf8000000UL 31#define F_MAXDEPTH 0xf8000000UL
30#define S_MAXDEPTH 27 32#define S_MAXDEPTH 27
31 33
32#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 34#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
33
34// F_SELFCONVERT? <=> to_json/toJson
35// F_BLESSED? <=> { $__class__$ => }
36 35
37#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 36#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
38#define F_DEFAULT (9UL << S_MAXDEPTH) 37#define F_DEFAULT (9UL << S_MAXDEPTH)
39 38
40#define INIT_SIZE 32 // initial scalar size to be allocated 39#define INIT_SIZE 32 // initial scalar size to be allocated
54#endif 53#endif
55 54
56#define expect_false(expr) expect ((expr) != 0, 0) 55#define expect_false(expr) expect ((expr) != 0, 0)
57#define expect_true(expr) expect ((expr) != 0, 1) 56#define expect_true(expr) expect ((expr) != 0, 1)
58 57
59static HV *json_stash; // JSON::XS:: 58static HV *json_stash, *json_boolean_stash; // JSON::XS::
60static SV *json_true, *json_false; 59static SV *json_true, *json_false;
61 60
62///////////////////////////////////////////////////////////////////////////// 61/////////////////////////////////////////////////////////////////////////////
63// utility functions 62// utility functions
64 63
469 svtype svt; 468 svtype svt;
470 469
471 SvGETMAGIC (sv); 470 SvGETMAGIC (sv);
472 svt = SvTYPE (sv); 471 svt = SvTYPE (sv);
473 472
473 if (expect_false (SvOBJECT (sv)))
474 {
475 if (SvSTASH (sv) == json_boolean_stash)
476 {
477 if (SvIV (sv) == 0)
478 encode_str (enc, "false", 5, 0);
479 else
480 encode_str (enc, "true", 4, 0);
481 }
482 else
483 {
484#if 0
485 if (0 && sv_derived_from (rv, "JSON::Literal"))
486 {
487 // not yet
488 }
489#endif
490 if (enc->flags & F_CONV_BLESSED)
491 {
492 // we re-bless the reference to get overload and other niceties right
493 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1);
494
495 if (to_json)
496 {
497 dSP;
498 ENTER;
499 SAVETMPS;
500 PUSHMARK (SP);
501 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
502
503 // calling with G_SCALAR ensures that we always get a 1 reutrn value
504 // check anyways.
505 PUTBACK;
506 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR));
507 SPAGAIN;
508
509 encode_sv (enc, POPs);
510
511 FREETMPS;
512 LEAVE;
513 }
514 else if (enc->flags & F_ALLOW_BLESSED)
515 encode_str (enc, "null", 4, 0);
516 else
517 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
518 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
519 }
520 else if (enc->flags & F_ALLOW_BLESSED)
521 encode_str (enc, "null", 4, 0);
522 else
523 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled",
524 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
525 }
526 }
474 if (svt == SVt_PVHV) 527 else if (svt == SVt_PVHV)
475 encode_hv (enc, (HV *)sv); 528 encode_hv (enc, (HV *)sv);
476 else if (svt == SVt_PVAV) 529 else if (svt == SVt_PVAV)
477 encode_av (enc, (AV *)sv); 530 encode_av (enc, (AV *)sv);
478 else if (svt < SVt_PVAV) 531 else if (svt < SVt_PVAV)
479 { 532 {
1163 i >= '0' && i <= '9' ? i - '0' 1216 i >= '0' && i <= '9' ? i - '0'
1164 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1217 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1165 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1218 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1166 : -1; 1219 : -1;
1167 1220
1168 json_stash = gv_stashpv ("JSON::XS", 1); 1221 json_stash = gv_stashpv ("JSON::XS" , 1);
1222 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1169 1223
1170 json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); 1224 json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true );
1171 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1225 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1172} 1226}
1173 1227
1179 OUTPUT: 1233 OUTPUT:
1180 RETVAL 1234 RETVAL
1181 1235
1182SV *ascii (SV *self, int enable = 1) 1236SV *ascii (SV *self, int enable = 1)
1183 ALIAS: 1237 ALIAS:
1184 ascii = F_ASCII 1238 ascii = F_ASCII
1185 latin1 = F_LATIN1 1239 latin1 = F_LATIN1
1186 utf8 = F_UTF8 1240 utf8 = F_UTF8
1187 indent = F_INDENT 1241 indent = F_INDENT
1188 canonical = F_CANONICAL 1242 canonical = F_CANONICAL
1189 space_before = F_SPACE_BEFORE 1243 space_before = F_SPACE_BEFORE
1190 space_after = F_SPACE_AFTER 1244 space_after = F_SPACE_AFTER
1191 pretty = F_PRETTY 1245 pretty = F_PRETTY
1192 allow_nonref = F_ALLOW_NONREF 1246 allow_nonref = F_ALLOW_NONREF
1193 shrink = F_SHRINK 1247 shrink = F_SHRINK
1248 allow_blessed = F_ALLOW_BLESSED
1249 convert_blessed = F_CONV_BLESSED
1194 CODE: 1250 CODE:
1195{ 1251{
1196 UV *uv = SvJSON (self); 1252 UV *uv = SvJSON (self);
1197 if (enable) 1253 if (enable)
1198 *uv |= ix; 1254 *uv |= ix;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines