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.50 by root, Mon Jul 2 00:29:38 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",
526 encode_hv (enc, (HV *)sv); 524 encode_hv (enc, (HV *)sv);
527 else if (svt == SVt_PVAV) 525 else if (svt == SVt_PVAV)
528 encode_av (enc, (AV *)sv); 526 encode_av (enc, (AV *)sv);
529 else if (svt < SVt_PVAV) 527 else if (svt < SVt_PVAV)
530 { 528 {
531 if (SvNIOK (sv) && SvIV (sv) == 0) 529 STRLEN len = 0;
530 char *pv = svt ? SvPV (sv, len) : 0;
531
532 if (len == 1 && *pv == '0')
532 encode_str (enc, "false", 5, 0); 533 encode_str (enc, "false", 5, 0);
533 else if (SvNIOK (sv) && SvIV (sv) == 1) 534 else if (len == 1 && *pv == '1')
534 encode_str (enc, "true", 4, 0); 535 encode_str (enc, "true", 4, 0);
535 else 536 else
536 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", 537 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
537 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 538 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
538 } 539 }
1008} 1009}
1009 1010
1010static SV * 1011static SV *
1011decode_hv (dec_t *dec) 1012decode_hv (dec_t *dec)
1012{ 1013{
1014 SV *sv;
1013 HV *hv = newHV (); 1015 HV *hv = newHV ();
1014 1016
1015 DEC_INC_DEPTH; 1017 DEC_INC_DEPTH;
1016 decode_ws (dec); 1018 decode_ws (dec);
1017 1019
1091 1093
1092 ++dec->cur; 1094 ++dec->cur;
1093 } 1095 }
1094 1096
1095 DEC_DEC_DEPTH; 1097 DEC_DEC_DEPTH;
1098 sv = newRV_noinc ((SV *)hv);
1099
1100 // check filter callbacks
1101 if (dec->json.flags & F_HOOK)
1102 {
1103 ENTER; SAVETMPS;
1104
1105 if (HvKEYS (hv) == 1 && dec->json.cb_sk_object)
1106 {
1107 int count;
1108
1109 dSP; PUSHMARK (SP);
1110 XPUSHs (sv_2mortal (sv));
1111
1112 PUTBACK; count = call_sv (dec->json.cb_sk_object, G_ARRAY); SPAGAIN;
1113
1114 if (count == 1)
1115 sv = newSVsv (POPs);
1116 else
1117 SvREFCNT_inc (sv);
1118 }
1119
1120 if (dec->json.cb_object)
1121 {
1122 int count;
1123
1124 dSP; ENTER; SAVETMPS; PUSHMARK (SP);
1125 XPUSHs (sv_2mortal (sv));
1126
1127 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1128
1129 if (count == 1)
1130 sv = newSVsv (POPs);
1131 else
1132 SvREFCNT_inc (sv);
1133 }
1134
1135 FREETMPS; LEAVE;
1136 }
1137
1096 return newRV_noinc ((SV *)hv); 1138 return newRV_noinc ((SV *)hv);
1097 1139
1098fail: 1140fail:
1099 SvREFCNT_dec (hv); 1141 SvREFCNT_dec (hv);
1100 DEC_DEC_DEPTH; 1142 DEC_DEC_DEPTH;
1187 dec.end = SvEND (string); 1229 dec.end = SvEND (string);
1188 dec.err = 0; 1230 dec.err = 0;
1189 dec.depth = 0; 1231 dec.depth = 0;
1190 dec.maxdepth = DEC_DEPTH (dec.json.flags); 1232 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1191 1233
1234 if (dec.json.cb_object || dec.json.cb_sk_object)
1235 dec.json.flags |= F_HOOK;
1236
1192 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1237 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1193 sv = decode_sv (&dec); 1238 sv = decode_sv (&dec);
1194 1239
1195 if (!(offset_return || !sv)) 1240 if (!(offset_return || !sv))
1196 { 1241 {
1270void new (char *klass) 1315void new (char *klass)
1271 PPCODE: 1316 PPCODE:
1272{ 1317{
1273 SV *pv = NEWSV (0, sizeof (JSON)); 1318 SV *pv = NEWSV (0, sizeof (JSON));
1274 SvPOK_only (pv); 1319 SvPOK_only (pv);
1275 Zero (SvPVX (pv), 1, sizeof (JSON)); 1320 Zero (SvPVX (pv), 1, JSON);
1276 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1321 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1277 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1322 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash)));
1278} 1323}
1279 1324
1280void ascii (JSON *self, int enable = 1) 1325void ascii (JSON *self, int enable = 1)
1330 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1375 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1331 1376
1332 XPUSHs (ST (0)); 1377 XPUSHs (ST (0));
1333} 1378}
1334 1379
1380void filter_json_objects (JSON *self, SV *cb = &PL_sv_undef)
1381 ALIAS:
1382 filter_sk_json_objects = 1
1383 PPCODE:
1384{
1385 if (!SvOK (cb))
1386 cb = 0;
1387
1388 switch (ix)
1389 {
1390 case 0: self->cb_object = cb; break;
1391 case 1: self->cb_sk_object = cb; break;
1392 }
1393
1394 XPUSHs (ST (0));
1395}
1396
1335void encode (JSON *self, SV *scalar) 1397void encode (JSON *self, SV *scalar)
1336 PPCODE: 1398 PPCODE:
1337 XPUSHs (encode_json (scalar, self)); 1399 XPUSHs (encode_json (scalar, self));
1338 1400
1339void decode (JSON *self, SV *jsonstr) 1401void decode (JSON *self, SV *jsonstr)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines