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.41 by root, Sat Jun 23 22:53:16 2007 UTC vs.
Revision 1.44 by root, Mon Jun 25 04:08:17 2007 UTC

10#if defined(__BORLANDC__) || defined(_MSC_VER) 10#if defined(__BORLANDC__) || defined(_MSC_VER)
11# define snprintf _snprintf // C compilers have this in stdio.h 11# define snprintf _snprintf // C compilers have this in stdio.h
12#endif 12#endif
13 13
14// some old perls do not have this, try to make it work, no 14// some old perls do not have this, try to make it work, no
15// guarentees, though. 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::
59static SV *json_true, *json_false;
60 60
61///////////////////////////////////////////////////////////////////////////// 61/////////////////////////////////////////////////////////////////////////////
62// utility functions 62// utility functions
63 63
64static UV * 64static UV *
468 svtype svt; 468 svtype svt;
469 469
470 SvGETMAGIC (sv); 470 SvGETMAGIC (sv);
471 svt = SvTYPE (sv); 471 svt = SvTYPE (sv);
472 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 }
473 if (svt == SVt_PVHV) 527 else if (svt == SVt_PVHV)
474 encode_hv (enc, (HV *)sv); 528 encode_hv (enc, (HV *)sv);
475 else if (svt == SVt_PVAV) 529 else if (svt == SVt_PVAV)
476 encode_av (enc, (AV *)sv); 530 encode_av (enc, (AV *)sv);
477 else if (svt < SVt_PVAV) 531 else if (svt < SVt_PVAV)
478 { 532 {
1031 1085
1032 case 't': 1086 case 't':
1033 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1087 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1034 { 1088 {
1035 dec->cur += 4; 1089 dec->cur += 4;
1036 return newSViv (1); 1090 return SvREFCNT_inc (json_true);
1037 } 1091 }
1038 else 1092 else
1039 ERR ("'true' expected"); 1093 ERR ("'true' expected");
1040 1094
1041 break; 1095 break;
1042 1096
1043 case 'f': 1097 case 'f':
1044 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1098 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1045 { 1099 {
1046 dec->cur += 5; 1100 dec->cur += 5;
1047 return newSViv (0); 1101 return SvREFCNT_inc (json_false);
1048 } 1102 }
1049 else 1103 else
1050 ERR ("'false' expected"); 1104 ERR ("'false' expected");
1051 1105
1052 break; 1106 break;
1162 i >= '0' && i <= '9' ? i - '0' 1216 i >= '0' && i <= '9' ? i - '0'
1163 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1217 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1164 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1218 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1165 : -1; 1219 : -1;
1166 1220
1167 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);
1223
1224 json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true );
1225 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1168} 1226}
1169 1227
1170PROTOTYPES: DISABLE 1228PROTOTYPES: DISABLE
1171 1229
1172SV *new (char *dummy) 1230SV *new (char *dummy)
1175 OUTPUT: 1233 OUTPUT:
1176 RETVAL 1234 RETVAL
1177 1235
1178SV *ascii (SV *self, int enable = 1) 1236SV *ascii (SV *self, int enable = 1)
1179 ALIAS: 1237 ALIAS:
1180 ascii = F_ASCII 1238 ascii = F_ASCII
1181 latin1 = F_LATIN1 1239 latin1 = F_LATIN1
1182 utf8 = F_UTF8 1240 utf8 = F_UTF8
1183 indent = F_INDENT 1241 indent = F_INDENT
1184 canonical = F_CANONICAL 1242 canonical = F_CANONICAL
1185 space_before = F_SPACE_BEFORE 1243 space_before = F_SPACE_BEFORE
1186 space_after = F_SPACE_AFTER 1244 space_after = F_SPACE_AFTER
1187 pretty = F_PRETTY 1245 pretty = F_PRETTY
1188 allow_nonref = F_ALLOW_NONREF 1246 allow_nonref = F_ALLOW_NONREF
1189 shrink = F_SHRINK 1247 shrink = F_SHRINK
1248 allow_blessed = F_ALLOW_BLESSED
1249 convert_blessed = F_CONV_BLESSED
1190 CODE: 1250 CODE:
1191{ 1251{
1192 UV *uv = SvJSON (self); 1252 UV *uv = SvJSON (self);
1193 if (enable) 1253 if (enable)
1194 *uv |= ix; 1254 *uv |= ix;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines