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.60 by root, Mon Aug 13 16:19:13 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_he (enc_t *enc, HE *he) 345encode_hk (enc_t *enc, HE *he)
341{ 346{
342 encode_ch (enc, '"'); 347 encode_ch (enc, '"');
343 348
344 if (HeKLEN (he) == HEf_SVKEY) 349 if (HeKLEN (he) == HEf_SVKEY)
345 { 350 {
358 encode_ch (enc, '"'); 363 encode_ch (enc, '"');
359 364
360 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 365 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
361 encode_ch (enc, ':'); 366 encode_ch (enc, ':');
362 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 367 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
363 encode_sv (enc, HeVAL (he));
364} 368}
365 369
366// compare hash entries, used when all keys are bytestrings 370// compare hash entries, used when all keys are bytestrings
367static int 371static int
368he_cmp_fast (const void *a_, const void *b_) 372he_cmp_fast (const void *a_, const void *b_)
373 HE *b = *(HE **)b_; 377 HE *b = *(HE **)b_;
374 378
375 STRLEN la = HeKLEN (a); 379 STRLEN la = HeKLEN (a);
376 STRLEN lb = HeKLEN (b); 380 STRLEN lb = HeKLEN (b);
377 381
378 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 382 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
379 cmp = la - lb; 383 cmp = lb - la;
380 384
381 return cmp; 385 return cmp;
382} 386}
383 387
384// compare hash entries, used when some keys are sv's or utf-x 388// compare hash entries, used when some keys are sv's or utf-x
385static int 389static int
386he_cmp_slow (const void *a, const void *b) 390he_cmp_slow (const void *a, const void *b)
387{ 391{
388 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 392 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
389} 393}
390 394
391static void 395static void
392encode_hv (enc_t *enc, HV *hv) 396encode_hv (enc_t *enc, HV *hv)
393{ 397{
398 HE *he;
394 int count, i; 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 if ((count = hv_iterinit (hv)))
402 {
403 // for canonical output we have to sort by keys first 406 // for canonical output we have to sort by keys first
404 // actually, this is mostly due to the stupid so-called 407 // actually, this is mostly due to the stupid so-called
405 // security workaround added somewhere in 5.8.x. 408 // security workaround added somewhere in 5.8.x.
406 // that randomises hash orderings 409 // that randomises hash orderings
407 if (enc->json.flags & F_CANONICAL) 410 if (enc->json.flags & F_CANONICAL)
411 {
412 int count = hv_iterinit (hv);
413
414 if (SvMAGICAL (hv))
408 { 415 {
416 // need to count by iterating. could improve by dynamically building the vector below
417 // but I don't care for the speed of this special case.
418 // note also that we will run into undefined behaviour when the two iterations
419 // do not result in the same count, something I might care for in some later release.
420
421 count = 0;
422 while (hv_iternext (hv))
423 ++count;
424
425 hv_iterinit (hv);
426 }
427
428 if (count)
429 {
409 int fast = 1; 430 int i, fast = 1;
410 HE *he;
411#if defined(__BORLANDC__) || defined(_MSC_VER) 431#if defined(__BORLANDC__) || defined(_MSC_VER)
412 HE **hes = _alloca (count * sizeof (HE)); 432 HE **hes = _alloca (count * sizeof (HE));
413#else 433#else
414 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 434 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
415#endif 435#endif
442 462
443 FREETMPS; 463 FREETMPS;
444 LEAVE; 464 LEAVE;
445 } 465 }
446 466
447 for (i = 0; i < count; ++i) 467 encode_nl (enc); ++enc->indent;
468
469 while (count--)
448 { 470 {
449 encode_indent (enc); 471 encode_indent (enc);
472 he = hes [count];
450 encode_he (enc, hes [i]); 473 encode_hk (enc, he);
474 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
451 475
452 if (i < count - 1) 476 if (count)
453 encode_comma (enc); 477 encode_comma (enc);
454 } 478 }
455 479
456 encode_nl (enc); 480 encode_nl (enc); --enc->indent; encode_indent (enc);
457 } 481 }
482 }
458 else 483 else
484 {
485 if (hv_iterinit (hv) || SvMAGICAL (hv))
486 if ((he = hv_iternext (hv)))
459 { 487 {
460 HE *he = hv_iternext (hv); 488 encode_nl (enc); ++enc->indent;
461 489
462 for (;;) 490 for (;;)
463 { 491 {
464 encode_indent (enc); 492 encode_indent (enc);
465 encode_he (enc, he); 493 encode_hk (enc, he);
494 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
466 495
467 if (!(he = hv_iternext (hv))) 496 if (!(he = hv_iternext (hv)))
468 break; 497 break;
469 498
470 encode_comma (enc); 499 encode_comma (enc);
471 } 500 }
472 501
473 encode_nl (enc); 502 encode_nl (enc); --enc->indent; encode_indent (enc);
474 } 503 }
475 } 504 }
476 505
477 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 506 encode_ch (enc, '}');
478} 507}
479 508
480// encode objects, arrays and special \0=false and \1=true values. 509// encode objects, arrays and special \0=false and \1=true values.
481static void 510static void
482encode_rv (enc_t *enc, SV *sv) 511encode_rv (enc_t *enc, SV *sv)
682 U32 depth; // recursion depth 711 U32 depth; // recursion depth
683 U32 maxdepth; // recursion depth limit 712 U32 maxdepth; // recursion depth limit
684} dec_t; 713} dec_t;
685 714
686inline 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
687decode_ws (dec_t *dec) 725decode_ws (dec_t *dec)
688{ 726{
689 for (;;) 727 for (;;)
690 { 728 {
691 char ch = *dec->cur; 729 char ch = *dec->cur;
692 730
693 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 }
694 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 743 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
695 break; 744 break; // parse error, but let higher level handle it, gives better error messages
696 745
697 ++dec->cur; 746 ++dec->cur;
698 } 747 }
699} 748}
700 749
951 1000
952 if (!is_nv) 1001 if (!is_nv)
953 { 1002 {
954 int len = dec->cur - start; 1003 int len = dec->cur - start;
955 1004
956 // 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
957 if (*start == '-') 1006 if (*start == '-')
958 switch (len) 1007 switch (len)
959 { 1008 {
960 case 2: return newSViv (-( start [1] - '0' * 1)); 1009 case 2: return newSViv (-( start [1] - '0' * 1));
961 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1010 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
962 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));
963 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));
964 } 1014 }
965 else 1015 else
966 switch (len) 1016 switch (len)
967 { 1017 {
968 case 1: return newSViv ( start [0] - '0' * 1); 1018 case 1: return newSViv ( start [0] - '0' * 1);
969 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1019 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
970 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);
971 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);
972 } 1023 }
973 1024
974 { 1025 {
975 UV uv; 1026 UV uv;
976 int numtype = grok_number (start, len, &uv); 1027 int numtype = grok_number (start, len, &uv);
1037 1088
1038 if (*dec->cur != ',') 1089 if (*dec->cur != ',')
1039 ERR (", or ] expected while parsing array"); 1090 ERR (", or ] expected while parsing array");
1040 1091
1041 ++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 }
1042 } 1101 }
1043 1102
1044 DEC_DEC_DEPTH; 1103 DEC_DEC_DEPTH;
1045 return newRV_noinc ((SV *)av); 1104 return newRV_noinc ((SV *)av);
1046 1105
1062 if (*dec->cur == '}') 1121 if (*dec->cur == '}')
1063 ++dec->cur; 1122 ++dec->cur;
1064 else 1123 else
1065 for (;;) 1124 for (;;)
1066 { 1125 {
1067 decode_ws (dec); EXPECT_CH ('"'); 1126 EXPECT_CH ('"');
1068 1127
1069 // heuristic: assume that 1128 // heuristic: assume that
1070 // a) decode_str + hv_store_ent are abysmally slow. 1129 // a) decode_str + hv_store_ent are abysmally slow.
1071 // b) most hash keys are short, simple ascii text. 1130 // b) most hash keys are short, simple ascii text.
1072 // => try to "fast-match" such strings to avoid 1131 // => try to "fast-match" such strings to avoid
1086 if (!key) 1145 if (!key)
1087 goto fail; 1146 goto fail;
1088 1147
1089 decode_ws (dec); EXPECT_CH (':'); 1148 decode_ws (dec); EXPECT_CH (':');
1090 1149
1150 decode_ws (dec);
1091 value = decode_sv (dec); 1151 value = decode_sv (dec);
1092 if (!value) 1152 if (!value)
1093 { 1153 {
1094 SvREFCNT_dec (key); 1154 SvREFCNT_dec (key);
1095 goto fail; 1155 goto fail;
1107 int len = p - key; 1167 int len = p - key;
1108 dec->cur = p + 1; 1168 dec->cur = p + 1;
1109 1169
1110 decode_ws (dec); EXPECT_CH (':'); 1170 decode_ws (dec); EXPECT_CH (':');
1111 1171
1172 decode_ws (dec);
1112 value = decode_sv (dec); 1173 value = decode_sv (dec);
1113 if (!value) 1174 if (!value)
1114 goto fail; 1175 goto fail;
1115 1176
1116 hv_store (hv, key, len, value, 0); 1177 hv_store (hv, key, len, value, 0);
1132 1193
1133 if (*dec->cur != ',') 1194 if (*dec->cur != ',')
1134 ERR (", or } expected while parsing object/hash"); 1195 ERR (", or } expected while parsing object/hash");
1135 1196
1136 ++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 }
1137 } 1206 }
1138 1207
1139 DEC_DEC_DEPTH; 1208 DEC_DEC_DEPTH;
1140 sv = newRV_noinc ((SV *)hv); 1209 sv = newRV_noinc ((SV *)hv);
1141 1210
1206} 1275}
1207 1276
1208static SV * 1277static SV *
1209decode_sv (dec_t *dec) 1278decode_sv (dec_t *dec)
1210{ 1279{
1211 decode_ws (dec);
1212
1213 // the beauty of JSON: you need exactly one character lookahead 1280 // the beauty of JSON: you need exactly one character lookahead
1214 // to parse anything. 1281 // to parse anything.
1215 switch (*dec->cur) 1282 switch (*dec->cur)
1216 { 1283 {
1217 case '"': ++dec->cur; return decode_str (dec); 1284 case '"': ++dec->cur; return decode_str (dec);
1301 1368
1302 if (dec.json.cb_object || dec.json.cb_sk_object) 1369 if (dec.json.cb_object || dec.json.cb_sk_object)
1303 dec.json.flags |= F_HOOK; 1370 dec.json.flags |= F_HOOK;
1304 1371
1305 *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);
1306 sv = decode_sv (&dec); 1375 sv = decode_sv (&dec);
1307 1376
1308 if (!(offset_return || !sv)) 1377 if (!(offset_return || !sv))
1309 { 1378 {
1310 // check for trailing garbage 1379 // check for trailing garbage
1390{ 1459{
1391 SV *pv = NEWSV (0, sizeof (JSON)); 1460 SV *pv = NEWSV (0, sizeof (JSON));
1392 SvPOK_only (pv); 1461 SvPOK_only (pv);
1393 Zero (SvPVX (pv), 1, JSON); 1462 Zero (SvPVX (pv), 1, JSON);
1394 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1463 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1395 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 )));
1396} 1468}
1397 1469
1398void ascii (JSON *self, int enable = 1) 1470void ascii (JSON *self, int enable = 1)
1399 ALIAS: 1471 ALIAS:
1400 ascii = F_ASCII 1472 ascii = F_ASCII
1407 pretty = F_PRETTY 1479 pretty = F_PRETTY
1408 allow_nonref = F_ALLOW_NONREF 1480 allow_nonref = F_ALLOW_NONREF
1409 shrink = F_SHRINK 1481 shrink = F_SHRINK
1410 allow_blessed = F_ALLOW_BLESSED 1482 allow_blessed = F_ALLOW_BLESSED
1411 convert_blessed = F_CONV_BLESSED 1483 convert_blessed = F_CONV_BLESSED
1484 relaxed = F_RELAXED
1412 PPCODE: 1485 PPCODE:
1413{ 1486{
1414 if (enable) 1487 if (enable)
1415 self->flags |= ix; 1488 self->flags |= ix;
1416 else 1489 else
1417 self->flags &= ~ix; 1490 self->flags &= ~ix;
1418 1491
1419 XPUSHs (ST (0)); 1492 XPUSHs (ST (0));
1420} 1493}
1421 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
1422void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1512void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1423 PPCODE: 1513 PPCODE:
1424{ 1514{
1425 UV log2 = 0; 1515 UV log2 = 0;
1426 1516
1432 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1522 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1433 1523
1434 XPUSHs (ST (0)); 1524 XPUSHs (ST (0));
1435} 1525}
1436 1526
1527U32 get_max_depth (JSON *self)
1528 CODE:
1529 RETVAL = DEC_DEPTH (self->flags);
1530 OUTPUT:
1531 RETVAL
1532
1437void max_size (JSON *self, UV max_size = 0) 1533void max_size (JSON *self, UV max_size = 0)
1438 PPCODE: 1534 PPCODE:
1439{ 1535{
1440 UV log2 = 0; 1536 UV log2 = 0;
1441 1537
1447 1543
1448 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1544 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1449 1545
1450 XPUSHs (ST (0)); 1546 XPUSHs (ST (0));
1451} 1547}
1548
1549int get_max_size (JSON *self)
1550 CODE:
1551 RETVAL = DEC_SIZE (self->flags);
1552 OUTPUT:
1553 RETVAL
1452 1554
1453void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1555void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1454 PPCODE: 1556 PPCODE:
1455{ 1557{
1456 SvREFCNT_dec (self->cb_object); 1558 SvREFCNT_dec (self->cb_object);
1503 SvREFCNT_dec (self->cb_sk_object); 1605 SvREFCNT_dec (self->cb_sk_object);
1504 SvREFCNT_dec (self->cb_object); 1606 SvREFCNT_dec (self->cb_object);
1505 1607
1506PROTOTYPES: ENABLE 1608PROTOTYPES: ENABLE
1507 1609
1508void to_json (SV *scalar) 1610void encode_json (SV *scalar)
1509 PPCODE: 1611 PPCODE:
1510{ 1612{
1511 JSON json = { F_DEFAULT | F_UTF8 }; 1613 JSON json = { F_DEFAULT | F_UTF8 };
1512 XPUSHs (encode_json (scalar, &json)); 1614 XPUSHs (encode_json (scalar, &json));
1513} 1615}
1514 1616
1515void from_json (SV *jsonstr) 1617void decode_json (SV *jsonstr)
1516 PPCODE: 1618 PPCODE:
1517{ 1619{
1518 JSON json = { F_DEFAULT | F_UTF8 }; 1620 JSON json = { F_DEFAULT | F_UTF8 };
1519 XPUSHs (decode_json (jsonstr, &json, 0)); 1621 XPUSHs (decode_json (jsonstr, &json, 0));
1520} 1622}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines