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.62 by root, Sun Aug 26 22:27:32 2007 UTC vs.
Revision 1.69 by root, Wed Dec 5 10:59:28 2007 UTC

27#define F_SPACE_AFTER 0x00000040UL 27#define F_SPACE_AFTER 0x00000040UL
28#define F_ALLOW_NONREF 0x00000100UL 28#define F_ALLOW_NONREF 0x00000100UL
29#define F_SHRINK 0x00000200UL 29#define F_SHRINK 0x00000200UL
30#define F_ALLOW_BLESSED 0x00000400UL 30#define F_ALLOW_BLESSED 0x00000400UL
31#define F_CONV_BLESSED 0x00000800UL 31#define F_CONV_BLESSED 0x00000800UL
32#define F_RELAXED 0x00001000UL
33
32#define F_MAXDEPTH 0xf8000000UL 34#define F_MAXDEPTH 0xf8000000UL
33#define S_MAXDEPTH 27 35#define S_MAXDEPTH 27
34#define F_MAXSIZE 0x01f00000UL 36#define F_MAXSIZE 0x01f00000UL
35#define S_MAXSIZE 20 37#define S_MAXSIZE 20
36#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing 38#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
310 int i, len = av_len (av); 312 int i, len = av_len (av);
311 313
312 if (enc->indent >= enc->maxdepth) 314 if (enc->indent >= enc->maxdepth)
313 croak ("data structure too deep (hit recursion limit)"); 315 croak ("data structure too deep (hit recursion limit)");
314 316
315 encode_ch (enc, '['); encode_nl (enc); 317 encode_ch (enc, '[');
316 ++enc->indent; 318
319 if (len >= 0)
320 {
321 encode_nl (enc); ++enc->indent;
317 322
318 for (i = 0; i <= len; ++i) 323 for (i = 0; i <= len; ++i)
319 { 324 {
320 SV **svp = av_fetch (av, i, 0); 325 SV **svp = av_fetch (av, i, 0);
321 326
322 encode_indent (enc); 327 encode_indent (enc);
323 328
324 if (svp) 329 if (svp)
325 encode_sv (enc, *svp); 330 encode_sv (enc, *svp);
326 else 331 else
327 encode_str (enc, "null", 4, 0); 332 encode_str (enc, "null", 4, 0);
328 333
329 if (i < len) 334 if (i < len)
330 encode_comma (enc); 335 encode_comma (enc);
331 } 336 }
332 337
338 encode_nl (enc); --enc->indent; encode_indent (enc);
339 }
340
333 encode_nl (enc); 341 encode_ch (enc, ']');
334
335 --enc->indent;
336 encode_indent (enc); encode_ch (enc, ']');
337} 342}
338 343
339static void 344static void
340encode_hk (enc_t *enc, HE *he) 345encode_hk (enc_t *enc, HE *he)
341{ 346{
394 int count; 399 int count;
395 400
396 if (enc->indent >= enc->maxdepth) 401 if (enc->indent >= enc->maxdepth)
397 croak ("data structure too deep (hit recursion limit)"); 402 croak ("data structure too deep (hit recursion limit)");
398 403
399 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 404 encode_ch (enc, '{');
400 405
401 // for canonical output we have to sort by keys first 406 // for canonical output we have to sort by keys first
402 // actually, this is mostly due to the stupid so-called 407 // actually, this is mostly due to the stupid so-called
403 // security workaround added somewhere in 5.8.x. 408 // security workaround added somewhere in 5.8.x.
404 // that randomises hash orderings 409 // that randomises hash orderings
457 462
458 FREETMPS; 463 FREETMPS;
459 LEAVE; 464 LEAVE;
460 } 465 }
461 466
467 encode_nl (enc); ++enc->indent;
468
462 while (count--) 469 while (count--)
463 { 470 {
464 encode_indent (enc); 471 encode_indent (enc);
465 he = hes [count]; 472 he = hes [count];
466 encode_hk (enc, he); 473 encode_hk (enc, he);
467 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); 474 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
468 475
469 if (count) 476 if (count)
470 encode_comma (enc); 477 encode_comma (enc);
471 } 478 }
479
480 encode_nl (enc); --enc->indent; encode_indent (enc);
472 } 481 }
473 } 482 }
474 else 483 else
475 { 484 {
476 if (hv_iterinit (hv) || SvMAGICAL (hv)) 485 if (hv_iterinit (hv) || SvMAGICAL (hv))
477 if ((he = hv_iternext (hv))) 486 if ((he = hv_iternext (hv)))
487 {
488 encode_nl (enc); ++enc->indent;
489
478 for (;;) 490 for (;;)
479 { 491 {
480 encode_indent (enc); 492 encode_indent (enc);
481 encode_hk (enc, he); 493 encode_hk (enc, he);
482 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); 494 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
483 495
484 if (!(he = hv_iternext (hv))) 496 if (!(he = hv_iternext (hv)))
485 break; 497 break;
486 498
487 encode_comma (enc); 499 encode_comma (enc);
488 } 500 }
489 }
490 501
502 encode_nl (enc); --enc->indent; encode_indent (enc);
503 }
504 }
505
491 encode_nl (enc); 506 encode_ch (enc, '}');
492
493 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
494} 507}
495 508
496// encode objects, arrays and special \0=false and \1=true values. 509// encode objects, arrays and special \0=false and \1=true values.
497static void 510static void
498encode_rv (enc_t *enc, SV *sv) 511encode_rv (enc_t *enc, SV *sv)
698 U32 depth; // recursion depth 711 U32 depth; // recursion depth
699 U32 maxdepth; // recursion depth limit 712 U32 maxdepth; // recursion depth limit
700} dec_t; 713} dec_t;
701 714
702inline void 715inline void
716decode_comment (dec_t *dec)
717{
718 // only '#'-style comments allowed a.t.m.
719
720 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
721 ++dec->cur;
722}
723
724inline void
703decode_ws (dec_t *dec) 725decode_ws (dec_t *dec)
704{ 726{
705 for (;;) 727 for (;;)
706 { 728 {
707 char ch = *dec->cur; 729 char ch = *dec->cur;
708 730
709 if (ch > 0x20 731 if (ch > 0x20)
732 {
733 if (expect_false (ch == '#'))
734 {
735 if (dec->json.flags & F_RELAXED)
736 decode_comment (dec);
737 else
738 break;
739 }
740 else
741 break;
742 }
710 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 743 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
711 break; 744 break; // parse error, but let higher level handle it, gives better error messages
712 745
713 ++dec->cur; 746 ++dec->cur;
714 } 747 }
715} 748}
716 749
1053 1086
1054 if (*dec->cur != ',') 1087 if (*dec->cur != ',')
1055 ERR (", or ] expected while parsing array"); 1088 ERR (", or ] expected while parsing array");
1056 1089
1057 ++dec->cur; 1090 ++dec->cur;
1091
1092 decode_ws (dec);
1093
1094 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1095 {
1096 ++dec->cur;
1097 break;
1098 }
1058 } 1099 }
1059 1100
1060 DEC_DEC_DEPTH; 1101 DEC_DEC_DEPTH;
1061 return newRV_noinc ((SV *)av); 1102 return newRV_noinc ((SV *)av);
1062 1103
1078 if (*dec->cur == '}') 1119 if (*dec->cur == '}')
1079 ++dec->cur; 1120 ++dec->cur;
1080 else 1121 else
1081 for (;;) 1122 for (;;)
1082 { 1123 {
1083 decode_ws (dec); EXPECT_CH ('"'); 1124 EXPECT_CH ('"');
1084 1125
1085 // heuristic: assume that 1126 // heuristic: assume that
1086 // a) decode_str + hv_store_ent are abysmally slow. 1127 // a) decode_str + hv_store_ent are abysmally slow.
1087 // b) most hash keys are short, simple ascii text. 1128 // b) most hash keys are short, simple ascii text.
1088 // => try to "fast-match" such strings to avoid 1129 // => try to "fast-match" such strings to avoid
1102 if (!key) 1143 if (!key)
1103 goto fail; 1144 goto fail;
1104 1145
1105 decode_ws (dec); EXPECT_CH (':'); 1146 decode_ws (dec); EXPECT_CH (':');
1106 1147
1148 decode_ws (dec);
1107 value = decode_sv (dec); 1149 value = decode_sv (dec);
1108 if (!value) 1150 if (!value)
1109 { 1151 {
1110 SvREFCNT_dec (key); 1152 SvREFCNT_dec (key);
1111 goto fail; 1153 goto fail;
1123 int len = p - key; 1165 int len = p - key;
1124 dec->cur = p + 1; 1166 dec->cur = p + 1;
1125 1167
1126 decode_ws (dec); EXPECT_CH (':'); 1168 decode_ws (dec); EXPECT_CH (':');
1127 1169
1170 decode_ws (dec);
1128 value = decode_sv (dec); 1171 value = decode_sv (dec);
1129 if (!value) 1172 if (!value)
1130 goto fail; 1173 goto fail;
1131 1174
1132 hv_store (hv, key, len, value, 0); 1175 hv_store (hv, key, len, value, 0);
1148 1191
1149 if (*dec->cur != ',') 1192 if (*dec->cur != ',')
1150 ERR (", or } expected while parsing object/hash"); 1193 ERR (", or } expected while parsing object/hash");
1151 1194
1152 ++dec->cur; 1195 ++dec->cur;
1196
1197 decode_ws (dec);
1198
1199 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1200 {
1201 ++dec->cur;
1202 break;
1203 }
1153 } 1204 }
1154 1205
1155 DEC_DEC_DEPTH; 1206 DEC_DEC_DEPTH;
1156 sv = newRV_noinc ((SV *)hv); 1207 sv = newRV_noinc ((SV *)hv);
1157 1208
1222} 1273}
1223 1274
1224static SV * 1275static SV *
1225decode_sv (dec_t *dec) 1276decode_sv (dec_t *dec)
1226{ 1277{
1227 decode_ws (dec);
1228
1229 // the beauty of JSON: you need exactly one character lookahead 1278 // the beauty of JSON: you need exactly one character lookahead
1230 // to parse anything. 1279 // to parse anything.
1231 switch (*dec->cur) 1280 switch (*dec->cur)
1232 { 1281 {
1233 case '"': ++dec->cur; return decode_str (dec); 1282 case '"': ++dec->cur; return decode_str (dec);
1317 1366
1318 if (dec.json.cb_object || dec.json.cb_sk_object) 1367 if (dec.json.cb_object || dec.json.cb_sk_object)
1319 dec.json.flags |= F_HOOK; 1368 dec.json.flags |= F_HOOK;
1320 1369
1321 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1370 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1371
1372 decode_ws (&dec);
1322 sv = decode_sv (&dec); 1373 sv = decode_sv (&dec);
1323 1374
1324 if (!(offset_return || !sv)) 1375 if (!(offset_return || !sv))
1325 { 1376 {
1326 // check for trailing garbage 1377 // check for trailing garbage
1406{ 1457{
1407 SV *pv = NEWSV (0, sizeof (JSON)); 1458 SV *pv = NEWSV (0, sizeof (JSON));
1408 SvPOK_only (pv); 1459 SvPOK_only (pv);
1409 Zero (SvPVX (pv), 1, JSON); 1460 Zero (SvPVX (pv), 1, JSON);
1410 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1461 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1411 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); 1462 XPUSHs (sv_2mortal (sv_bless (
1463 newRV_noinc (pv),
1464 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1465 )));
1412} 1466}
1413 1467
1414void ascii (JSON *self, int enable = 1) 1468void ascii (JSON *self, int enable = 1)
1415 ALIAS: 1469 ALIAS:
1416 ascii = F_ASCII 1470 ascii = F_ASCII
1423 pretty = F_PRETTY 1477 pretty = F_PRETTY
1424 allow_nonref = F_ALLOW_NONREF 1478 allow_nonref = F_ALLOW_NONREF
1425 shrink = F_SHRINK 1479 shrink = F_SHRINK
1426 allow_blessed = F_ALLOW_BLESSED 1480 allow_blessed = F_ALLOW_BLESSED
1427 convert_blessed = F_CONV_BLESSED 1481 convert_blessed = F_CONV_BLESSED
1482 relaxed = F_RELAXED
1428 PPCODE: 1483 PPCODE:
1429{ 1484{
1430 if (enable) 1485 if (enable)
1431 self->flags |= ix; 1486 self->flags |= ix;
1432 else 1487 else
1433 self->flags &= ~ix; 1488 self->flags &= ~ix;
1434 1489
1435 XPUSHs (ST (0)); 1490 XPUSHs (ST (0));
1436} 1491}
1437 1492
1493void get_ascii (JSON *self)
1494 ALIAS:
1495 get_ascii = F_ASCII
1496 get_latin1 = F_LATIN1
1497 get_utf8 = F_UTF8
1498 get_indent = F_INDENT
1499 get_canonical = F_CANONICAL
1500 get_space_before = F_SPACE_BEFORE
1501 get_space_after = F_SPACE_AFTER
1502 get_allow_nonref = F_ALLOW_NONREF
1503 get_shrink = F_SHRINK
1504 get_allow_blessed = F_ALLOW_BLESSED
1505 get_convert_blessed = F_CONV_BLESSED
1506 get_relaxed = F_RELAXED
1507 PPCODE:
1508 XPUSHs (boolSV (self->flags & ix));
1509
1438void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1510void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1439 PPCODE: 1511 PPCODE:
1440{ 1512{
1441 UV log2 = 0; 1513 UV log2 = 0;
1442 1514
1448 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1520 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1449 1521
1450 XPUSHs (ST (0)); 1522 XPUSHs (ST (0));
1451} 1523}
1452 1524
1525U32 get_max_depth (JSON *self)
1526 CODE:
1527 RETVAL = DEC_DEPTH (self->flags);
1528 OUTPUT:
1529 RETVAL
1530
1453void max_size (JSON *self, UV max_size = 0) 1531void max_size (JSON *self, UV max_size = 0)
1454 PPCODE: 1532 PPCODE:
1455{ 1533{
1456 UV log2 = 0; 1534 UV log2 = 0;
1457 1535
1463 1541
1464 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1542 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1465 1543
1466 XPUSHs (ST (0)); 1544 XPUSHs (ST (0));
1467} 1545}
1546
1547int get_max_size (JSON *self)
1548 CODE:
1549 RETVAL = DEC_SIZE (self->flags);
1550 OUTPUT:
1551 RETVAL
1468 1552
1469void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1553void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1470 PPCODE: 1554 PPCODE:
1471{ 1555{
1472 SvREFCNT_dec (self->cb_object); 1556 SvREFCNT_dec (self->cb_object);
1519 SvREFCNT_dec (self->cb_sk_object); 1603 SvREFCNT_dec (self->cb_sk_object);
1520 SvREFCNT_dec (self->cb_object); 1604 SvREFCNT_dec (self->cb_object);
1521 1605
1522PROTOTYPES: ENABLE 1606PROTOTYPES: ENABLE
1523 1607
1524void to_json (SV *scalar) 1608void encode_json (SV *scalar)
1525 PPCODE: 1609 PPCODE:
1526{ 1610{
1527 JSON json = { F_DEFAULT | F_UTF8 }; 1611 JSON json = { F_DEFAULT | F_UTF8 };
1528 XPUSHs (encode_json (scalar, &json)); 1612 XPUSHs (encode_json (scalar, &json));
1529} 1613}
1530 1614
1531void from_json (SV *jsonstr) 1615void decode_json (SV *jsonstr)
1532 PPCODE: 1616 PPCODE:
1533{ 1617{
1534 JSON json = { F_DEFAULT | F_UTF8 }; 1618 JSON json = { F_DEFAULT | F_UTF8 };
1535 XPUSHs (decode_json (jsonstr, &json, 0)); 1619 XPUSHs (decode_json (jsonstr, &json, 0));
1536} 1620}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines