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.48 by root, Sun Jul 1 22:20:00 2007 UTC vs.
Revision 1.49 by root, Sun Jul 1 23:40:07 2007 UTC

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 29#define F_ALLOW_BLESSED 0x00000400UL
30#define F_CONV_BLESSED 0x00000800UL // NYI 30#define F_CONV_BLESSED 0x00000800UL
31#define F_MAXDEPTH 0xf8000000UL 31#define F_MAXDEPTH 0xf8000000UL
32#define S_MAXDEPTH 27 32#define S_MAXDEPTH 27
33#define F_MAXSIZE 0x01f00000UL 33#define F_MAXSIZE 0x01f00000UL
34#define S_MAXSIZE 20 34#define S_MAXSIZE 20
35#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
35 36
36#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 37#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
37#define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) 38#define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE ))
38 39
39#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 40#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
61static HV *json_stash, *json_boolean_stash; // JSON::XS:: 62static HV *json_stash, *json_boolean_stash; // JSON::XS::
62static SV *json_true, *json_false; 63static SV *json_true, *json_false;
63 64
64typedef struct { 65typedef struct {
65 U32 flags; 66 U32 flags;
67 SV *cb_object, *cb_sk_object;
66} JSON; 68} JSON;
67 69
68///////////////////////////////////////////////////////////////////////////// 70/////////////////////////////////////////////////////////////////////////////
69// utility functions 71// utility functions
70 72
490 // we re-bless the reference to get overload and other niceties right 492 // we re-bless the reference to get overload and other niceties right
491 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 493 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1);
492 494
493 if (to_json) 495 if (to_json)
494 { 496 {
495 dSP; 497 dSP; ENTER; SAVETMPS; PUSHMARK (SP);
496 ENTER;
497 SAVETMPS;
498 PUSHMARK (SP);
499 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 498 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
500 499
501 // calling with G_SCALAR ensures that we always get a 1 reutrn value 500 // calling with G_SCALAR ensures that we always get a 1 reutrn value
502 // check anyways. 501 // check anyways.
503 PUTBACK; 502 PUTBACK;
504 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 503 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR));
505 SPAGAIN; 504 SPAGAIN;
506 505
507 encode_sv (enc, POPs); 506 encode_sv (enc, POPs);
508 507
509 FREETMPS; 508 FREETMPS; LEAVE;
510 LEAVE;
511 } 509 }
512 else if (enc->json.flags & F_ALLOW_BLESSED) 510 else if (enc->json.flags & F_ALLOW_BLESSED)
513 encode_str (enc, "null", 4, 0); 511 encode_str (enc, "null", 4, 0);
514 else 512 else
515 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", 513 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
1008} 1006}
1009 1007
1010static SV * 1008static SV *
1011decode_hv (dec_t *dec) 1009decode_hv (dec_t *dec)
1012{ 1010{
1011 SV *sv;
1013 HV *hv = newHV (); 1012 HV *hv = newHV ();
1014 1013
1015 DEC_INC_DEPTH; 1014 DEC_INC_DEPTH;
1016 decode_ws (dec); 1015 decode_ws (dec);
1017 1016
1091 1090
1092 ++dec->cur; 1091 ++dec->cur;
1093 } 1092 }
1094 1093
1095 DEC_DEC_DEPTH; 1094 DEC_DEC_DEPTH;
1095 sv = newRV_noinc ((SV *)hv);
1096
1097 // check filter callbacks
1098 if (dec->json.flags & F_HOOK)
1099 {
1100 ENTER; SAVETMPS;
1101
1102 if (HvKEYS (hv) == 1 && dec->json.cb_sk_object)
1103 {
1104 int count;
1105
1106 dSP; PUSHMARK (SP);
1107 XPUSHs (sv_2mortal (sv));
1108
1109 PUTBACK; count = call_sv (dec->json.cb_sk_object, G_ARRAY); SPAGAIN;
1110
1111 if (count == 1)
1112 sv = newSVsv (POPs);
1113 else
1114 SvREFCNT_inc (sv);
1115 }
1116
1117 if (dec->json.cb_object)
1118 {
1119 int count;
1120
1121 dSP; ENTER; SAVETMPS; PUSHMARK (SP);
1122 XPUSHs (sv_2mortal (sv));
1123
1124 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1125
1126 if (count == 1)
1127 sv = newSVsv (POPs);
1128 else
1129 SvREFCNT_inc (sv);
1130 }
1131
1132 FREETMPS; LEAVE;
1133 }
1134
1096 return newRV_noinc ((SV *)hv); 1135 return newRV_noinc ((SV *)hv);
1097 1136
1098fail: 1137fail:
1099 SvREFCNT_dec (hv); 1138 SvREFCNT_dec (hv);
1100 DEC_DEC_DEPTH; 1139 DEC_DEC_DEPTH;
1187 dec.end = SvEND (string); 1226 dec.end = SvEND (string);
1188 dec.err = 0; 1227 dec.err = 0;
1189 dec.depth = 0; 1228 dec.depth = 0;
1190 dec.maxdepth = DEC_DEPTH (dec.json.flags); 1229 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1191 1230
1231 if (dec.json.cb_object || dec.json.cb_sk_object)
1232 dec.json.flags |= F_HOOK;
1233
1192 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1234 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1193 sv = decode_sv (&dec); 1235 sv = decode_sv (&dec);
1194 1236
1195 if (!(offset_return || !sv)) 1237 if (!(offset_return || !sv))
1196 { 1238 {
1270void new (char *klass) 1312void new (char *klass)
1271 PPCODE: 1313 PPCODE:
1272{ 1314{
1273 SV *pv = NEWSV (0, sizeof (JSON)); 1315 SV *pv = NEWSV (0, sizeof (JSON));
1274 SvPOK_only (pv); 1316 SvPOK_only (pv);
1275 Zero (SvPVX (pv), 1, sizeof (JSON)); 1317 Zero (SvPVX (pv), 1, JSON);
1276 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1318 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1277 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1319 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash)));
1278} 1320}
1279 1321
1280void ascii (JSON *self, int enable = 1) 1322void ascii (JSON *self, int enable = 1)
1330 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1372 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1331 1373
1332 XPUSHs (ST (0)); 1374 XPUSHs (ST (0));
1333} 1375}
1334 1376
1377void filter_json_objects (JSON *self, SV *cb = &PL_sv_undef)
1378 ALIAS:
1379 filter_sk_json_objects = 1
1380 PPCODE:
1381{
1382 if (!SvOK (cb))
1383 cb = 0;
1384
1385 switch (ix)
1386 {
1387 case 0: self->cb_object = cb; break;
1388 case 1: self->cb_sk_object = cb; break;
1389 }
1390
1391 XPUSHs (ST (0));
1392}
1393
1335void encode (JSON *self, SV *scalar) 1394void encode (JSON *self, SV *scalar)
1336 PPCODE: 1395 PPCODE:
1337 XPUSHs (encode_json (scalar, self)); 1396 XPUSHs (encode_json (scalar, self));
1338 1397
1339void decode (JSON *self, SV *jsonstr) 1398void decode (JSON *self, SV *jsonstr)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines