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.47 by root, Sun Jul 1 14:08:03 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
59#define expect_true(expr) expect ((expr) != 0, 1) 60#define expect_true(expr) expect ((expr) != 0, 1)
60 61
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 json { 65typedef struct {
65 U32 flags; 66 U32 flags;
67 SV *cb_object, *cb_sk_object;
66} JSON__XS; 68} JSON;
67 69
68///////////////////////////////////////////////////////////////////////////// 70/////////////////////////////////////////////////////////////////////////////
69// utility functions 71// utility functions
70 72
71static UV * 73inline void
72SvJSON (SV *sv)
73{
74 if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash))
75 croak ("object is not of type JSON::XS");
76
77 return &SvUVX (SvRV (sv));
78}
79
80static void
81shrink (SV *sv) 74shrink (SV *sv)
82{ 75{
83 sv_utf8_downgrade (sv, 1); 76 sv_utf8_downgrade (sv, 1);
84 if (SvLEN (sv) > SvCUR (sv) + 1) 77 if (SvLEN (sv) > SvCUR (sv) + 1)
85 { 78 {
120typedef struct 113typedef struct
121{ 114{
122 char *cur; // SvPVX (sv) + current output position 115 char *cur; // SvPVX (sv) + current output position
123 char *end; // SvEND (sv) 116 char *end; // SvEND (sv)
124 SV *sv; // result scalar 117 SV *sv; // result scalar
125 struct json json; 118 JSON json;
126 U32 indent; // indentation level 119 U32 indent; // indentation level
127 U32 maxdepth; // max. indentation/recursion level 120 U32 maxdepth; // max. indentation/recursion level
128} enc_t; 121} enc_t;
129 122
130inline void 123inline void
499 // 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
500 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 493 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1);
501 494
502 if (to_json) 495 if (to_json)
503 { 496 {
504 dSP; 497 dSP; ENTER; SAVETMPS; PUSHMARK (SP);
505 ENTER;
506 SAVETMPS;
507 PUSHMARK (SP);
508 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 498 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
509 499
510 // 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
511 // check anyways. 501 // check anyways.
512 PUTBACK; 502 PUTBACK;
513 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 503 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR));
514 SPAGAIN; 504 SPAGAIN;
515 505
516 encode_sv (enc, POPs); 506 encode_sv (enc, POPs);
517 507
518 FREETMPS; 508 FREETMPS; LEAVE;
519 LEAVE;
520 } 509 }
521 else if (enc->json.flags & F_ALLOW_BLESSED) 510 else if (enc->json.flags & F_ALLOW_BLESSED)
522 encode_str (enc, "null", 4, 0); 511 encode_str (enc, "null", 4, 0);
523 else 512 else
524 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",
535 encode_hv (enc, (HV *)sv); 524 encode_hv (enc, (HV *)sv);
536 else if (svt == SVt_PVAV) 525 else if (svt == SVt_PVAV)
537 encode_av (enc, (AV *)sv); 526 encode_av (enc, (AV *)sv);
538 else if (svt < SVt_PVAV) 527 else if (svt < SVt_PVAV)
539 { 528 {
540 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')
541 encode_str (enc, "false", 5, 0); 533 encode_str (enc, "false", 5, 0);
542 else if (SvNIOK (sv) && SvIV (sv) == 1) 534 else if (len == 1 && *pv == '1')
543 encode_str (enc, "true", 4, 0); 535 encode_str (enc, "true", 4, 0);
544 else 536 else
545 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",
546 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 538 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
547 } 539 }
618 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 610 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this",
619 SvPV_nolen (sv), SvFLAGS (sv)); 611 SvPV_nolen (sv), SvFLAGS (sv));
620} 612}
621 613
622static SV * 614static SV *
623encode_json (SV *scalar, struct json *json) 615encode_json (SV *scalar, JSON *json)
624{ 616{
625 enc_t enc; 617 enc_t enc;
626 618
627 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) 619 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar))
628 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 620 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
656typedef struct 648typedef struct
657{ 649{
658 char *cur; // current parser pointer 650 char *cur; // current parser pointer
659 char *end; // end of input string 651 char *end; // end of input string
660 const char *err; // parse error, if != 0 652 const char *err; // parse error, if != 0
661 struct json json; 653 JSON json;
662 U32 depth; // recursion depth 654 U32 depth; // recursion depth
663 U32 maxdepth; // recursion depth limit 655 U32 maxdepth; // recursion depth limit
664} dec_t; 656} dec_t;
665 657
666inline void 658inline void
1017} 1009}
1018 1010
1019static SV * 1011static SV *
1020decode_hv (dec_t *dec) 1012decode_hv (dec_t *dec)
1021{ 1013{
1014 SV *sv;
1022 HV *hv = newHV (); 1015 HV *hv = newHV ();
1023 1016
1024 DEC_INC_DEPTH; 1017 DEC_INC_DEPTH;
1025 decode_ws (dec); 1018 decode_ws (dec);
1026 1019
1100 1093
1101 ++dec->cur; 1094 ++dec->cur;
1102 } 1095 }
1103 1096
1104 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
1105 return newRV_noinc ((SV *)hv); 1138 return newRV_noinc ((SV *)hv);
1106 1139
1107fail: 1140fail:
1108 SvREFCNT_dec (hv); 1141 SvREFCNT_dec (hv);
1109 DEC_DEC_DEPTH; 1142 DEC_DEC_DEPTH;
1169fail: 1202fail:
1170 return 0; 1203 return 0;
1171} 1204}
1172 1205
1173static SV * 1206static SV *
1174decode_json (SV *string, struct json *json, UV *offset_return) 1207decode_json (SV *string, JSON *json, UV *offset_return)
1175{ 1208{
1176 dec_t dec; 1209 dec_t dec;
1177 UV offset; 1210 UV offset;
1178 SV *sv; 1211 SV *sv;
1179 1212
1195 dec.cur = SvPVX (string); 1228 dec.cur = SvPVX (string);
1196 dec.end = SvEND (string); 1229 dec.end = SvEND (string);
1197 dec.err = 0; 1230 dec.err = 0;
1198 dec.depth = 0; 1231 dec.depth = 0;
1199 dec.maxdepth = DEC_DEPTH (dec.json.flags); 1232 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1233
1234 if (dec.json.cb_object || dec.json.cb_sk_object)
1235 dec.json.flags |= F_HOOK;
1200 1236
1201 *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
1202 sv = decode_sv (&dec); 1238 sv = decode_sv (&dec);
1203 1239
1204 if (!(offset_return || !sv)) 1240 if (!(offset_return || !sv))
1274 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1310 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1275} 1311}
1276 1312
1277PROTOTYPES: DISABLE 1313PROTOTYPES: DISABLE
1278 1314
1279SV *new (char *dummy) 1315void new (char *klass)
1280 CODE: 1316 PPCODE:
1281 RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); 1317{
1282 OUTPUT: 1318 SV *pv = NEWSV (0, sizeof (JSON));
1283 RETVAL 1319 SvPOK_only (pv);
1320 Zero (SvPVX (pv), 1, JSON);
1321 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1322 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash)));
1323}
1284 1324
1285SV *ascii (SV *self, int enable = 1) 1325void ascii (JSON *self, int enable = 1)
1286 ALIAS: 1326 ALIAS:
1287 ascii = F_ASCII 1327 ascii = F_ASCII
1288 latin1 = F_LATIN1 1328 latin1 = F_LATIN1
1289 utf8 = F_UTF8 1329 utf8 = F_UTF8
1290 indent = F_INDENT 1330 indent = F_INDENT
1294 pretty = F_PRETTY 1334 pretty = F_PRETTY
1295 allow_nonref = F_ALLOW_NONREF 1335 allow_nonref = F_ALLOW_NONREF
1296 shrink = F_SHRINK 1336 shrink = F_SHRINK
1297 allow_blessed = F_ALLOW_BLESSED 1337 allow_blessed = F_ALLOW_BLESSED
1298 convert_blessed = F_CONV_BLESSED 1338 convert_blessed = F_CONV_BLESSED
1299 CODE: 1339 PPCODE:
1300{ 1340{
1301 UV *uv = SvJSON (self);
1302 if (enable) 1341 if (enable)
1303 *uv |= ix; 1342 self->flags |= ix;
1304 else 1343 else
1305 *uv &= ~ix; 1344 self->flags &= ~ix;
1306 1345
1307 RETVAL = newSVsv (self); 1346 XPUSHs (ST (0));
1308} 1347}
1309 OUTPUT:
1310 RETVAL
1311 1348
1312SV *max_depth (SV *self, UV max_depth = 0x80000000UL) 1349void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1313 CODE: 1350 PPCODE:
1314{ 1351{
1315 UV *uv = SvJSON (self);
1316 UV log2 = 0; 1352 UV log2 = 0;
1317 1353
1318 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; 1354 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL;
1319 1355
1320 while ((1UL << log2) < max_depth) 1356 while ((1UL << log2) < max_depth)
1321 ++log2; 1357 ++log2;
1322 1358
1323 *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1359 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1324 1360
1325 RETVAL = newSVsv (self); 1361 XPUSHs (ST (0));
1326} 1362}
1327 OUTPUT:
1328 RETVAL
1329 1363
1330SV *max_size (SV *self, UV max_size = 0) 1364void max_size (JSON *self, UV max_size = 0)
1331 CODE: 1365 PPCODE:
1332{ 1366{
1333 UV *uv = SvJSON (self);
1334 UV log2 = 0; 1367 UV log2 = 0;
1335 1368
1336 if (max_size > 0x80000000UL) max_size = 0x80000000UL; 1369 if (max_size > 0x80000000UL) max_size = 0x80000000UL;
1337 if (max_size == 1) max_size = 2; 1370 if (max_size == 1) max_size = 2;
1338 1371
1339 while ((1UL << log2) < max_size) 1372 while ((1UL << log2) < max_size)
1340 ++log2; 1373 ++log2;
1341 1374
1342 *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1375 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1343 1376
1344 RETVAL = newSVsv (self); 1377 XPUSHs (ST (0));
1345} 1378}
1346 OUTPUT:
1347 RETVAL
1348 1379
1349void encode (SV *self, SV *scalar) 1380void filter_json_objects (JSON *self, SV *cb = &PL_sv_undef)
1381 ALIAS:
1382 filter_sk_json_objects = 1
1350 PPCODE: 1383 PPCODE:
1351{ 1384{
1352 struct json json = { *SvJSON (self) }; 1385 if (!SvOK (cb))
1353 XPUSHs (encode_json (scalar, &json)); 1386 cb = 0;
1354}
1355 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
1356void decode (SV *self, SV *jsonstr) 1397void encode (JSON *self, SV *scalar)
1357 PPCODE: 1398 PPCODE:
1358{
1359 struct json json = { *SvJSON (self) };
1360 XPUSHs (decode_json (jsonstr, &json, 0)); 1399 XPUSHs (encode_json (scalar, self));
1361}
1362 1400
1363void decode_prefix (SV *self, SV *jsonstr) 1401void decode (JSON *self, SV *jsonstr)
1364 PPCODE: 1402 PPCODE:
1403 XPUSHs (decode_json (jsonstr, self, 0));
1404
1405void decode_prefix (JSON *self, SV *jsonstr)
1406 PPCODE:
1365{ 1407{
1366 UV offset; 1408 UV offset;
1367 struct json json = { *SvJSON (self) };
1368 EXTEND (SP, 2); 1409 EXTEND (SP, 2);
1369 PUSHs (decode_json (jsonstr, &json, &offset)); 1410 PUSHs (decode_json (jsonstr, self, &offset));
1370 PUSHs (sv_2mortal (newSVuv (offset))); 1411 PUSHs (sv_2mortal (newSVuv (offset)));
1371} 1412}
1372 1413
1373PROTOTYPES: ENABLE 1414PROTOTYPES: ENABLE
1374 1415
1375void to_json (SV *scalar) 1416void to_json (SV *scalar)
1376 PPCODE: 1417 PPCODE:
1377{ 1418{
1378 struct json json = { F_DEFAULT | F_UTF8 }; 1419 JSON json = { F_DEFAULT | F_UTF8 };
1379 XPUSHs (encode_json (scalar, &json)); 1420 XPUSHs (encode_json (scalar, &json));
1380} 1421}
1381 1422
1382void from_json (SV *jsonstr) 1423void from_json (SV *jsonstr)
1383 PPCODE: 1424 PPCODE:
1384{ 1425{
1385 struct json json = { F_DEFAULT | F_UTF8 }; 1426 JSON json = { F_DEFAULT | F_UTF8 };
1386 XPUSHs (decode_json (jsonstr, &json, 0)); 1427 XPUSHs (decode_json (jsonstr, &json, 0));
1387} 1428}
1388 1429

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines