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.61 by root, Sun Aug 26 21:56:47 2007 UTC vs.
Revision 1.67 by root, Wed Nov 28 14:01:01 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, 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, 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
1423 pretty = F_PRETTY 1474 pretty = F_PRETTY
1424 allow_nonref = F_ALLOW_NONREF 1475 allow_nonref = F_ALLOW_NONREF
1425 shrink = F_SHRINK 1476 shrink = F_SHRINK
1426 allow_blessed = F_ALLOW_BLESSED 1477 allow_blessed = F_ALLOW_BLESSED
1427 convert_blessed = F_CONV_BLESSED 1478 convert_blessed = F_CONV_BLESSED
1479 relaxed = F_RELAXED
1428 PPCODE: 1480 PPCODE:
1429{ 1481{
1430 if (enable) 1482 if (enable)
1431 self->flags |= ix; 1483 self->flags |= ix;
1432 else 1484 else
1433 self->flags &= ~ix; 1485 self->flags &= ~ix;
1434 1486
1435 XPUSHs (ST (0)); 1487 XPUSHs (ST (0));
1436} 1488}
1437 1489
1490void get_ascii (JSON *self)
1491 ALIAS:
1492 get_ascii = F_ASCII
1493 get_latin1 = F_LATIN1
1494 get_utf8 = F_UTF8
1495 get_indent = F_INDENT
1496 get_canonical = F_CANONICAL
1497 get_space_before = F_SPACE_BEFORE
1498 get_space_after = F_SPACE_AFTER
1499 get_pretty = F_PRETTY
1500 get_allow_nonref = F_ALLOW_NONREF
1501 get_shrink = F_SHRINK
1502 get_allow_blessed = F_ALLOW_BLESSED
1503 get_convert_blessed = F_CONV_BLESSED
1504 get_relaxed = F_RELAXED
1505 PPCODE:
1506 XPUSHs (boolSV (self->flags & ix));
1507
1438void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1508void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1439 PPCODE: 1509 PPCODE:
1440{ 1510{
1441 UV log2 = 0; 1511 UV log2 = 0;
1442 1512
1448 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1518 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1449 1519
1450 XPUSHs (ST (0)); 1520 XPUSHs (ST (0));
1451} 1521}
1452 1522
1523U32 get_max_depth (JSON *self)
1524 CODE:
1525 RETVAL = DEC_DEPTH (self->flags);
1526 OUTPUT:
1527 RETVAL
1528
1453void max_size (JSON *self, UV max_size = 0) 1529void max_size (JSON *self, UV max_size = 0)
1454 PPCODE: 1530 PPCODE:
1455{ 1531{
1456 UV log2 = 0; 1532 UV log2 = 0;
1457 1533
1463 1539
1464 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1540 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1465 1541
1466 XPUSHs (ST (0)); 1542 XPUSHs (ST (0));
1467} 1543}
1544
1545int get_max_size (JSON *self)
1546 CODE:
1547 RETVAL = DEC_SIZE (self->flags);
1548 OUTPUT:
1549 RETVAL
1468 1550
1469void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1551void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1470 PPCODE: 1552 PPCODE:
1471{ 1553{
1472 SvREFCNT_dec (self->cb_object); 1554 SvREFCNT_dec (self->cb_object);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines