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.52 by root, Mon Jul 2 02:57:11 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;
68 HV *cb_sk_object;
66} JSON__XS; 69} JSON;
67 70
68///////////////////////////////////////////////////////////////////////////// 71/////////////////////////////////////////////////////////////////////////////
69// utility functions 72// utility functions
70 73
71static UV * 74inline 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) 75shrink (SV *sv)
82{ 76{
83 sv_utf8_downgrade (sv, 1); 77 sv_utf8_downgrade (sv, 1);
84 if (SvLEN (sv) > SvCUR (sv) + 1) 78 if (SvLEN (sv) > SvCUR (sv) + 1)
85 { 79 {
120typedef struct 114typedef struct
121{ 115{
122 char *cur; // SvPVX (sv) + current output position 116 char *cur; // SvPVX (sv) + current output position
123 char *end; // SvEND (sv) 117 char *end; // SvEND (sv)
124 SV *sv; // result scalar 118 SV *sv; // result scalar
125 struct json json; 119 JSON json;
126 U32 indent; // indentation level 120 U32 indent; // indentation level
127 U32 maxdepth; // max. indentation/recursion level 121 U32 maxdepth; // max. indentation/recursion level
128} enc_t; 122} enc_t;
129 123
130inline void 124inline void
479 473
480 if (expect_false (SvOBJECT (sv))) 474 if (expect_false (SvOBJECT (sv)))
481 { 475 {
482 if (SvSTASH (sv) == json_boolean_stash) 476 if (SvSTASH (sv) == json_boolean_stash)
483 { 477 {
484 if (SvIV (sv) == 0) 478 if (SvIV (sv))
479 encode_str (enc, "true", 4, 0);
480 else
485 encode_str (enc, "false", 5, 0); 481 encode_str (enc, "false", 5, 0);
486 else
487 encode_str (enc, "true", 4, 0);
488 } 482 }
489 else 483 else
490 { 484 {
491#if 0 485#if 0
492 if (0 && sv_derived_from (rv, "JSON::Literal")) 486 if (0 && sv_derived_from (rv, "JSON::Literal"))
499 // we re-bless the reference to get overload and other niceties right 493 // we re-bless the reference to get overload and other niceties right
500 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 494 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1);
501 495
502 if (to_json) 496 if (to_json)
503 { 497 {
504 dSP; 498 dSP; ENTER; SAVETMPS; PUSHMARK (SP);
505 ENTER;
506 SAVETMPS;
507 PUSHMARK (SP);
508 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 499 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
509 500
510 // calling with G_SCALAR ensures that we always get a 1 reutrn value 501 // calling with G_SCALAR ensures that we always get a 1 reutrn value
511 // check anyways. 502 // check anyways.
512 PUTBACK; 503 PUTBACK;
513 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 504 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR));
514 SPAGAIN; 505 SPAGAIN;
515 506
516 encode_sv (enc, POPs); 507 encode_sv (enc, POPs);
517 508
518 FREETMPS; 509 FREETMPS; LEAVE;
519 LEAVE;
520 } 510 }
521 else if (enc->json.flags & F_ALLOW_BLESSED) 511 else if (enc->json.flags & F_ALLOW_BLESSED)
522 encode_str (enc, "null", 4, 0); 512 encode_str (enc, "null", 4, 0);
523 else 513 else
524 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", 514 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
535 encode_hv (enc, (HV *)sv); 525 encode_hv (enc, (HV *)sv);
536 else if (svt == SVt_PVAV) 526 else if (svt == SVt_PVAV)
537 encode_av (enc, (AV *)sv); 527 encode_av (enc, (AV *)sv);
538 else if (svt < SVt_PVAV) 528 else if (svt < SVt_PVAV)
539 { 529 {
540 if (SvNIOK (sv) && SvIV (sv) == 0) 530 STRLEN len = 0;
531 char *pv = svt ? SvPV (sv, len) : 0;
532
533 if (len == 1 && *pv == '1')
534 encode_str (enc, "true", 4, 0);
535 else if (len == 1 && *pv == '0')
541 encode_str (enc, "false", 5, 0); 536 encode_str (enc, "false", 5, 0);
542 else if (SvNIOK (sv) && SvIV (sv) == 1)
543 encode_str (enc, "true", 4, 0);
544 else 537 else
545 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", 538 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
546 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 539 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
547 } 540 }
548 else 541 else
618 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 611 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this",
619 SvPV_nolen (sv), SvFLAGS (sv)); 612 SvPV_nolen (sv), SvFLAGS (sv));
620} 613}
621 614
622static SV * 615static SV *
623encode_json (SV *scalar, struct json *json) 616encode_json (SV *scalar, JSON *json)
624{ 617{
625 enc_t enc; 618 enc_t enc;
626 619
627 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) 620 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar))
628 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 621 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
656typedef struct 649typedef struct
657{ 650{
658 char *cur; // current parser pointer 651 char *cur; // current parser pointer
659 char *end; // end of input string 652 char *end; // end of input string
660 const char *err; // parse error, if != 0 653 const char *err; // parse error, if != 0
661 struct json json; 654 JSON json;
662 U32 depth; // recursion depth 655 U32 depth; // recursion depth
663 U32 maxdepth; // recursion depth limit 656 U32 maxdepth; // recursion depth limit
664} dec_t; 657} dec_t;
665 658
666inline void 659inline void
1017} 1010}
1018 1011
1019static SV * 1012static SV *
1020decode_hv (dec_t *dec) 1013decode_hv (dec_t *dec)
1021{ 1014{
1015 SV *sv;
1022 HV *hv = newHV (); 1016 HV *hv = newHV ();
1023 1017
1024 DEC_INC_DEPTH; 1018 DEC_INC_DEPTH;
1025 decode_ws (dec); 1019 decode_ws (dec);
1026 1020
1100 1094
1101 ++dec->cur; 1095 ++dec->cur;
1102 } 1096 }
1103 1097
1104 DEC_DEC_DEPTH; 1098 DEC_DEC_DEPTH;
1105 return newRV_noinc ((SV *)hv); 1099 sv = newRV_noinc ((SV *)hv);
1100
1101 // check filter callbacks
1102 if (dec->json.flags & F_HOOK)
1103 {
1104 if (dec->json.cb_sk_object && HvKEYS (hv) == 1)
1105 {
1106 HE *cb, *he;
1107
1108 hv_iterinit (hv);
1109 he = hv_iternext (hv);
1110 hv_iterinit (hv);
1111
1112 // the next line creates a mortal sv each time its called.
1113 // might want to optimise this for common cases.
1114 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1115
1116 if (cb)
1117 {
1118 int count;
1119 ENTER; SAVETMPS;
1120
1121 dSP; PUSHMARK (SP);
1122 XPUSHs (HeVAL (he));
1123
1124 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1125
1126 if (count == 1)
1127 {
1128 sv = newSVsv (POPs);
1129 FREETMPS; LEAVE;
1130 return sv;
1131 }
1132
1133 FREETMPS; LEAVE;
1134 }
1135 }
1136
1137 if (dec->json.cb_object)
1138 {
1139 int count;
1140 ENTER; SAVETMPS;
1141
1142 dSP; ENTER; SAVETMPS; PUSHMARK (SP);
1143 XPUSHs (sv_2mortal (sv));
1144
1145 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1146
1147 if (count == 1)
1148 {
1149 sv = newSVsv (POPs);
1150 FREETMPS; LEAVE;
1151 return sv;
1152 }
1153
1154 SvREFCNT_inc (sv);
1155 FREETMPS; LEAVE;
1156 }
1157 }
1158
1159 return sv;
1106 1160
1107fail: 1161fail:
1108 SvREFCNT_dec (hv); 1162 SvREFCNT_dec (hv);
1109 DEC_DEC_DEPTH; 1163 DEC_DEC_DEPTH;
1110 return 0; 1164 return 0;
1169fail: 1223fail:
1170 return 0; 1224 return 0;
1171} 1225}
1172 1226
1173static SV * 1227static SV *
1174decode_json (SV *string, struct json *json, UV *offset_return) 1228decode_json (SV *string, JSON *json, UV *offset_return)
1175{ 1229{
1176 dec_t dec; 1230 dec_t dec;
1177 UV offset; 1231 UV offset;
1178 SV *sv; 1232 SV *sv;
1179 1233
1195 dec.cur = SvPVX (string); 1249 dec.cur = SvPVX (string);
1196 dec.end = SvEND (string); 1250 dec.end = SvEND (string);
1197 dec.err = 0; 1251 dec.err = 0;
1198 dec.depth = 0; 1252 dec.depth = 0;
1199 dec.maxdepth = DEC_DEPTH (dec.json.flags); 1253 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1254
1255 if (dec.json.cb_object || dec.json.cb_sk_object)
1256 dec.json.flags |= F_HOOK;
1200 1257
1201 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1258 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1202 sv = decode_sv (&dec); 1259 sv = decode_sv (&dec);
1203 1260
1204 if (!(offset_return || !sv)) 1261 if (!(offset_return || !sv))
1274 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1331 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1275} 1332}
1276 1333
1277PROTOTYPES: DISABLE 1334PROTOTYPES: DISABLE
1278 1335
1279SV *new (char *dummy) 1336void new (char *klass)
1280 CODE: 1337 PPCODE:
1281 RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); 1338{
1282 OUTPUT: 1339 SV *pv = NEWSV (0, sizeof (JSON));
1283 RETVAL 1340 SvPOK_only (pv);
1341 Zero (SvPVX (pv), 1, JSON);
1342 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1343 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash)));
1344}
1284 1345
1285SV *ascii (SV *self, int enable = 1) 1346void ascii (JSON *self, int enable = 1)
1286 ALIAS: 1347 ALIAS:
1287 ascii = F_ASCII 1348 ascii = F_ASCII
1288 latin1 = F_LATIN1 1349 latin1 = F_LATIN1
1289 utf8 = F_UTF8 1350 utf8 = F_UTF8
1290 indent = F_INDENT 1351 indent = F_INDENT
1294 pretty = F_PRETTY 1355 pretty = F_PRETTY
1295 allow_nonref = F_ALLOW_NONREF 1356 allow_nonref = F_ALLOW_NONREF
1296 shrink = F_SHRINK 1357 shrink = F_SHRINK
1297 allow_blessed = F_ALLOW_BLESSED 1358 allow_blessed = F_ALLOW_BLESSED
1298 convert_blessed = F_CONV_BLESSED 1359 convert_blessed = F_CONV_BLESSED
1299 CODE: 1360 PPCODE:
1300{ 1361{
1301 UV *uv = SvJSON (self);
1302 if (enable) 1362 if (enable)
1303 *uv |= ix; 1363 self->flags |= ix;
1304 else 1364 else
1305 *uv &= ~ix; 1365 self->flags &= ~ix;
1306 1366
1307 RETVAL = newSVsv (self); 1367 XPUSHs (ST (0));
1308} 1368}
1309 OUTPUT:
1310 RETVAL
1311 1369
1312SV *max_depth (SV *self, UV max_depth = 0x80000000UL) 1370void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1313 CODE: 1371 PPCODE:
1314{ 1372{
1315 UV *uv = SvJSON (self);
1316 UV log2 = 0; 1373 UV log2 = 0;
1317 1374
1318 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; 1375 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL;
1319 1376
1320 while ((1UL << log2) < max_depth) 1377 while ((1UL << log2) < max_depth)
1321 ++log2; 1378 ++log2;
1322 1379
1323 *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1380 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1324 1381
1325 RETVAL = newSVsv (self); 1382 XPUSHs (ST (0));
1326} 1383}
1327 OUTPUT:
1328 RETVAL
1329 1384
1330SV *max_size (SV *self, UV max_size = 0) 1385void max_size (JSON *self, UV max_size = 0)
1331 CODE: 1386 PPCODE:
1332{ 1387{
1333 UV *uv = SvJSON (self);
1334 UV log2 = 0; 1388 UV log2 = 0;
1335 1389
1336 if (max_size > 0x80000000UL) max_size = 0x80000000UL; 1390 if (max_size > 0x80000000UL) max_size = 0x80000000UL;
1337 if (max_size == 1) max_size = 2; 1391 if (max_size == 1) max_size = 2;
1338 1392
1339 while ((1UL << log2) < max_size) 1393 while ((1UL << log2) < max_size)
1340 ++log2; 1394 ++log2;
1341 1395
1342 *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1396 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1343 1397
1344 RETVAL = newSVsv (self); 1398 XPUSHs (ST (0));
1345} 1399}
1346 OUTPUT:
1347 RETVAL
1348 1400
1349void encode (SV *self, SV *scalar) 1401void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1350 PPCODE: 1402 PPCODE:
1351{ 1403{
1352 struct json json = { *SvJSON (self) }; 1404 SvREFCNT_dec (self->cb_object);
1353 XPUSHs (encode_json (scalar, &json)); 1405 self->cb_object = SvOK (cb) ? newSVsv (cb) : 0;
1354}
1355 1406
1356void decode (SV *self, SV *jsonstr) 1407 XPUSHs (ST (0));
1408}
1409
1410void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef)
1357 PPCODE: 1411 PPCODE:
1358{ 1412{
1359 struct json json = { *SvJSON (self) }; 1413 if (!self->cb_sk_object)
1360 XPUSHs (decode_json (jsonstr, &json, 0)); 1414 self->cb_sk_object = newHV ();
1361}
1362 1415
1363void decode_prefix (SV *self, SV *jsonstr) 1416 if (SvOK (cb))
1417 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1418 else
1419 {
1420 hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0);
1421
1422 if (!HvKEYS (self->cb_sk_object))
1423 {
1424 SvREFCNT_dec (self->cb_sk_object);
1425 self->cb_sk_object = 0;
1426 }
1427 }
1428
1429 XPUSHs (ST (0));
1430}
1431
1432void encode (JSON *self, SV *scalar)
1364 PPCODE: 1433 PPCODE:
1434 XPUSHs (encode_json (scalar, self));
1435
1436void decode (JSON *self, SV *jsonstr)
1437 PPCODE:
1438 XPUSHs (decode_json (jsonstr, self, 0));
1439
1440void decode_prefix (JSON *self, SV *jsonstr)
1441 PPCODE:
1365{ 1442{
1366 UV offset; 1443 UV offset;
1367 struct json json = { *SvJSON (self) };
1368 EXTEND (SP, 2); 1444 EXTEND (SP, 2);
1369 PUSHs (decode_json (jsonstr, &json, &offset)); 1445 PUSHs (decode_json (jsonstr, self, &offset));
1370 PUSHs (sv_2mortal (newSVuv (offset))); 1446 PUSHs (sv_2mortal (newSVuv (offset)));
1371} 1447}
1448
1449void DESTROY (JSON *self)
1450 CODE:
1451 SvREFCNT_dec (self->cb_sk_object);
1452 SvREFCNT_dec (self->cb_object);
1372 1453
1373PROTOTYPES: ENABLE 1454PROTOTYPES: ENABLE
1374 1455
1375void to_json (SV *scalar) 1456void to_json (SV *scalar)
1376 PPCODE: 1457 PPCODE:
1377{ 1458{
1378 struct json json = { F_DEFAULT | F_UTF8 }; 1459 JSON json = { F_DEFAULT | F_UTF8 };
1379 XPUSHs (encode_json (scalar, &json)); 1460 XPUSHs (encode_json (scalar, &json));
1380} 1461}
1381 1462
1382void from_json (SV *jsonstr) 1463void from_json (SV *jsonstr)
1383 PPCODE: 1464 PPCODE:
1384{ 1465{
1385 struct json json = { F_DEFAULT | F_UTF8 }; 1466 JSON json = { F_DEFAULT | F_UTF8 };
1386 XPUSHs (decode_json (jsonstr, &json, 0)); 1467 XPUSHs (decode_json (jsonstr, &json, 0));
1387} 1468}
1388 1469

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines