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.70 by root, Wed Mar 19 00:44:54 2008 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
967 1000
968 if (!is_nv) 1001 if (!is_nv)
969 { 1002 {
970 int len = dec->cur - start; 1003 int len = dec->cur - start;
971 1004
972 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1005 // special case the rather common 1..5-digit-int case
973 if (*start == '-') 1006 if (*start == '-')
974 switch (len) 1007 switch (len)
975 { 1008 {
976 case 2: return newSViv (-( start [1] - '0' * 1)); 1009 case 2: return newSViv (-( start [1] - '0' * 1));
977 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1010 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
978 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 1011 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
979 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 1012 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1013 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
980 } 1014 }
981 else 1015 else
982 switch (len) 1016 switch (len)
983 { 1017 {
984 case 1: return newSViv ( start [0] - '0' * 1); 1018 case 1: return newSViv ( start [0] - '0' * 1);
985 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1019 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
986 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 1020 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
987 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 1021 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1022 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
988 } 1023 }
989 1024
990 { 1025 {
991 UV uv; 1026 UV uv;
992 int numtype = grok_number (start, len, &uv); 1027 int numtype = grok_number (start, len, &uv);
1053 1088
1054 if (*dec->cur != ',') 1089 if (*dec->cur != ',')
1055 ERR (", or ] expected while parsing array"); 1090 ERR (", or ] expected while parsing array");
1056 1091
1057 ++dec->cur; 1092 ++dec->cur;
1093
1094 decode_ws (dec);
1095
1096 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1097 {
1098 ++dec->cur;
1099 break;
1100 }
1058 } 1101 }
1059 1102
1060 DEC_DEC_DEPTH; 1103 DEC_DEC_DEPTH;
1061 return newRV_noinc ((SV *)av); 1104 return newRV_noinc ((SV *)av);
1062 1105
1078 if (*dec->cur == '}') 1121 if (*dec->cur == '}')
1079 ++dec->cur; 1122 ++dec->cur;
1080 else 1123 else
1081 for (;;) 1124 for (;;)
1082 { 1125 {
1083 decode_ws (dec); EXPECT_CH ('"'); 1126 EXPECT_CH ('"');
1084 1127
1085 // heuristic: assume that 1128 // heuristic: assume that
1086 // a) decode_str + hv_store_ent are abysmally slow. 1129 // a) decode_str + hv_store_ent are abysmally slow.
1087 // b) most hash keys are short, simple ascii text. 1130 // b) most hash keys are short, simple ascii text.
1088 // => try to "fast-match" such strings to avoid 1131 // => try to "fast-match" such strings to avoid
1102 if (!key) 1145 if (!key)
1103 goto fail; 1146 goto fail;
1104 1147
1105 decode_ws (dec); EXPECT_CH (':'); 1148 decode_ws (dec); EXPECT_CH (':');
1106 1149
1150 decode_ws (dec);
1107 value = decode_sv (dec); 1151 value = decode_sv (dec);
1108 if (!value) 1152 if (!value)
1109 { 1153 {
1110 SvREFCNT_dec (key); 1154 SvREFCNT_dec (key);
1111 goto fail; 1155 goto fail;
1123 int len = p - key; 1167 int len = p - key;
1124 dec->cur = p + 1; 1168 dec->cur = p + 1;
1125 1169
1126 decode_ws (dec); EXPECT_CH (':'); 1170 decode_ws (dec); EXPECT_CH (':');
1127 1171
1172 decode_ws (dec);
1128 value = decode_sv (dec); 1173 value = decode_sv (dec);
1129 if (!value) 1174 if (!value)
1130 goto fail; 1175 goto fail;
1131 1176
1132 hv_store (hv, key, len, value, 0); 1177 hv_store (hv, key, len, value, 0);
1148 1193
1149 if (*dec->cur != ',') 1194 if (*dec->cur != ',')
1150 ERR (", or } expected while parsing object/hash"); 1195 ERR (", or } expected while parsing object/hash");
1151 1196
1152 ++dec->cur; 1197 ++dec->cur;
1198
1199 decode_ws (dec);
1200
1201 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1202 {
1203 ++dec->cur;
1204 break;
1205 }
1153 } 1206 }
1154 1207
1155 DEC_DEC_DEPTH; 1208 DEC_DEC_DEPTH;
1156 sv = newRV_noinc ((SV *)hv); 1209 sv = newRV_noinc ((SV *)hv);
1157 1210
1222} 1275}
1223 1276
1224static SV * 1277static SV *
1225decode_sv (dec_t *dec) 1278decode_sv (dec_t *dec)
1226{ 1279{
1227 decode_ws (dec);
1228
1229 // the beauty of JSON: you need exactly one character lookahead 1280 // the beauty of JSON: you need exactly one character lookahead
1230 // to parse anything. 1281 // to parse anything.
1231 switch (*dec->cur) 1282 switch (*dec->cur)
1232 { 1283 {
1233 case '"': ++dec->cur; return decode_str (dec); 1284 case '"': ++dec->cur; return decode_str (dec);
1317 1368
1318 if (dec.json.cb_object || dec.json.cb_sk_object) 1369 if (dec.json.cb_object || dec.json.cb_sk_object)
1319 dec.json.flags |= F_HOOK; 1370 dec.json.flags |= F_HOOK;
1320 1371
1321 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1372 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1373
1374 decode_ws (&dec);
1322 sv = decode_sv (&dec); 1375 sv = decode_sv (&dec);
1323 1376
1324 if (!(offset_return || !sv)) 1377 if (!(offset_return || !sv))
1325 { 1378 {
1326 // check for trailing garbage 1379 // check for trailing garbage
1406{ 1459{
1407 SV *pv = NEWSV (0, sizeof (JSON)); 1460 SV *pv = NEWSV (0, sizeof (JSON));
1408 SvPOK_only (pv); 1461 SvPOK_only (pv);
1409 Zero (SvPVX (pv), 1, JSON); 1462 Zero (SvPVX (pv), 1, JSON);
1410 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1463 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1411 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); 1464 XPUSHs (sv_2mortal (sv_bless (
1465 newRV_noinc (pv),
1466 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1467 )));
1412} 1468}
1413 1469
1414void ascii (JSON *self, int enable = 1) 1470void ascii (JSON *self, int enable = 1)
1415 ALIAS: 1471 ALIAS:
1416 ascii = F_ASCII 1472 ascii = F_ASCII
1423 pretty = F_PRETTY 1479 pretty = F_PRETTY
1424 allow_nonref = F_ALLOW_NONREF 1480 allow_nonref = F_ALLOW_NONREF
1425 shrink = F_SHRINK 1481 shrink = F_SHRINK
1426 allow_blessed = F_ALLOW_BLESSED 1482 allow_blessed = F_ALLOW_BLESSED
1427 convert_blessed = F_CONV_BLESSED 1483 convert_blessed = F_CONV_BLESSED
1484 relaxed = F_RELAXED
1428 PPCODE: 1485 PPCODE:
1429{ 1486{
1430 if (enable) 1487 if (enable)
1431 self->flags |= ix; 1488 self->flags |= ix;
1432 else 1489 else
1433 self->flags &= ~ix; 1490 self->flags &= ~ix;
1434 1491
1435 XPUSHs (ST (0)); 1492 XPUSHs (ST (0));
1436} 1493}
1437 1494
1495void get_ascii (JSON *self)
1496 ALIAS:
1497 get_ascii = F_ASCII
1498 get_latin1 = F_LATIN1
1499 get_utf8 = F_UTF8
1500 get_indent = F_INDENT
1501 get_canonical = F_CANONICAL
1502 get_space_before = F_SPACE_BEFORE
1503 get_space_after = F_SPACE_AFTER
1504 get_allow_nonref = F_ALLOW_NONREF
1505 get_shrink = F_SHRINK
1506 get_allow_blessed = F_ALLOW_BLESSED
1507 get_convert_blessed = F_CONV_BLESSED
1508 get_relaxed = F_RELAXED
1509 PPCODE:
1510 XPUSHs (boolSV (self->flags & ix));
1511
1438void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1512void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1439 PPCODE: 1513 PPCODE:
1440{ 1514{
1441 UV log2 = 0; 1515 UV log2 = 0;
1442 1516
1448 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1522 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1449 1523
1450 XPUSHs (ST (0)); 1524 XPUSHs (ST (0));
1451} 1525}
1452 1526
1527U32 get_max_depth (JSON *self)
1528 CODE:
1529 RETVAL = DEC_DEPTH (self->flags);
1530 OUTPUT:
1531 RETVAL
1532
1453void max_size (JSON *self, UV max_size = 0) 1533void max_size (JSON *self, UV max_size = 0)
1454 PPCODE: 1534 PPCODE:
1455{ 1535{
1456 UV log2 = 0; 1536 UV log2 = 0;
1457 1537
1463 1543
1464 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1544 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1465 1545
1466 XPUSHs (ST (0)); 1546 XPUSHs (ST (0));
1467} 1547}
1548
1549int get_max_size (JSON *self)
1550 CODE:
1551 RETVAL = DEC_SIZE (self->flags);
1552 OUTPUT:
1553 RETVAL
1468 1554
1469void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1555void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1470 PPCODE: 1556 PPCODE:
1471{ 1557{
1472 SvREFCNT_dec (self->cb_object); 1558 SvREFCNT_dec (self->cb_object);
1519 SvREFCNT_dec (self->cb_sk_object); 1605 SvREFCNT_dec (self->cb_sk_object);
1520 SvREFCNT_dec (self->cb_object); 1606 SvREFCNT_dec (self->cb_object);
1521 1607
1522PROTOTYPES: ENABLE 1608PROTOTYPES: ENABLE
1523 1609
1524void to_json (SV *scalar) 1610void encode_json (SV *scalar)
1525 PPCODE: 1611 PPCODE:
1526{ 1612{
1527 JSON json = { F_DEFAULT | F_UTF8 }; 1613 JSON json = { F_DEFAULT | F_UTF8 };
1528 XPUSHs (encode_json (scalar, &json)); 1614 XPUSHs (encode_json (scalar, &json));
1529} 1615}
1530 1616
1531void from_json (SV *jsonstr) 1617void decode_json (SV *jsonstr)
1532 PPCODE: 1618 PPCODE:
1533{ 1619{
1534 JSON json = { F_DEFAULT | F_UTF8 }; 1620 JSON json = { F_DEFAULT | F_UTF8 };
1535 XPUSHs (decode_json (jsonstr, &json, 0)); 1621 XPUSHs (decode_json (jsonstr, &json, 0));
1536} 1622}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines