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.63 by root, Mon Aug 27 01:49:01 2007 UTC vs.
Revision 1.69 by root, Wed Dec 5 10:59:28 2007 UTC

312 int i, len = av_len (av); 312 int i, len = av_len (av);
313 313
314 if (enc->indent >= enc->maxdepth) 314 if (enc->indent >= enc->maxdepth)
315 croak ("data structure too deep (hit recursion limit)"); 315 croak ("data structure too deep (hit recursion limit)");
316 316
317 encode_ch (enc, '['); encode_nl (enc); 317 encode_ch (enc, '[');
318 ++enc->indent; 318
319 if (len >= 0)
320 {
321 encode_nl (enc); ++enc->indent;
319 322
320 for (i = 0; i <= len; ++i) 323 for (i = 0; i <= len; ++i)
321 { 324 {
322 SV **svp = av_fetch (av, i, 0); 325 SV **svp = av_fetch (av, i, 0);
323 326
324 encode_indent (enc); 327 encode_indent (enc);
325 328
326 if (svp) 329 if (svp)
327 encode_sv (enc, *svp); 330 encode_sv (enc, *svp);
328 else 331 else
329 encode_str (enc, "null", 4, 0); 332 encode_str (enc, "null", 4, 0);
330 333
331 if (i < len) 334 if (i < len)
332 encode_comma (enc); 335 encode_comma (enc);
333 } 336 }
334 337
338 encode_nl (enc); --enc->indent; encode_indent (enc);
339 }
340
335 encode_nl (enc); 341 encode_ch (enc, ']');
336
337 --enc->indent;
338 encode_indent (enc); encode_ch (enc, ']');
339} 342}
340 343
341static void 344static void
342encode_hk (enc_t *enc, HE *he) 345encode_hk (enc_t *enc, HE *he)
343{ 346{
396 int count; 399 int count;
397 400
398 if (enc->indent >= enc->maxdepth) 401 if (enc->indent >= enc->maxdepth)
399 croak ("data structure too deep (hit recursion limit)"); 402 croak ("data structure too deep (hit recursion limit)");
400 403
401 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 404 encode_ch (enc, '{');
402 405
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
459 462
460 FREETMPS; 463 FREETMPS;
461 LEAVE; 464 LEAVE;
462 } 465 }
463 466
467 encode_nl (enc); ++enc->indent;
468
464 while (count--) 469 while (count--)
465 { 470 {
466 encode_indent (enc); 471 encode_indent (enc);
467 he = hes [count]; 472 he = hes [count];
468 encode_hk (enc, he); 473 encode_hk (enc, he);
469 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));
470 475
471 if (count) 476 if (count)
472 encode_comma (enc); 477 encode_comma (enc);
473 } 478 }
479
480 encode_nl (enc); --enc->indent; encode_indent (enc);
474 } 481 }
475 } 482 }
476 else 483 else
477 { 484 {
478 if (hv_iterinit (hv) || SvMAGICAL (hv)) 485 if (hv_iterinit (hv) || SvMAGICAL (hv))
479 if ((he = hv_iternext (hv))) 486 if ((he = hv_iternext (hv)))
487 {
488 encode_nl (enc); ++enc->indent;
489
480 for (;;) 490 for (;;)
481 { 491 {
482 encode_indent (enc); 492 encode_indent (enc);
483 encode_hk (enc, he); 493 encode_hk (enc, he);
484 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));
485 495
486 if (!(he = hv_iternext (hv))) 496 if (!(he = hv_iternext (hv)))
487 break; 497 break;
488 498
489 encode_comma (enc); 499 encode_comma (enc);
490 } 500 }
491 }
492 501
502 encode_nl (enc); --enc->indent; encode_indent (enc);
503 }
504 }
505
493 encode_nl (enc); 506 encode_ch (enc, '}');
494
495 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
496} 507}
497 508
498// encode objects, arrays and special \0=false and \1=true values. 509// encode objects, arrays and special \0=false and \1=true values.
499static void 510static void
500encode_rv (enc_t *enc, SV *sv) 511encode_rv (enc_t *enc, SV *sv)
700 U32 depth; // recursion depth 711 U32 depth; // recursion depth
701 U32 maxdepth; // recursion depth limit 712 U32 maxdepth; // recursion depth limit
702} dec_t; 713} dec_t;
703 714
704inline 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
705decode_ws (dec_t *dec) 725decode_ws (dec_t *dec)
706{ 726{
707 for (;;) 727 for (;;)
708 { 728 {
709 char ch = *dec->cur; 729 char ch = *dec->cur;
710 730
711 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 }
712 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 743 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
713 break; 744 break; // parse error, but let higher level handle it, gives better error messages
714
715 if (ch == '#' && dec->json.flags & F_RELAXED)
716 ++dec->cur;
717 745
718 ++dec->cur; 746 ++dec->cur;
719 } 747 }
720} 748}
721 749
1429{ 1457{
1430 SV *pv = NEWSV (0, sizeof (JSON)); 1458 SV *pv = NEWSV (0, sizeof (JSON));
1431 SvPOK_only (pv); 1459 SvPOK_only (pv);
1432 Zero (SvPVX (pv), 1, JSON); 1460 Zero (SvPVX (pv), 1, JSON);
1433 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1461 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1434 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 )));
1435} 1466}
1436 1467
1437void ascii (JSON *self, int enable = 1) 1468void ascii (JSON *self, int enable = 1)
1438 ALIAS: 1469 ALIAS:
1439 ascii = F_ASCII 1470 ascii = F_ASCII
1457 self->flags &= ~ix; 1488 self->flags &= ~ix;
1458 1489
1459 XPUSHs (ST (0)); 1490 XPUSHs (ST (0));
1460} 1491}
1461 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
1462void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1510void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1463 PPCODE: 1511 PPCODE:
1464{ 1512{
1465 UV log2 = 0; 1513 UV log2 = 0;
1466 1514
1472 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1520 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1473 1521
1474 XPUSHs (ST (0)); 1522 XPUSHs (ST (0));
1475} 1523}
1476 1524
1525U32 get_max_depth (JSON *self)
1526 CODE:
1527 RETVAL = DEC_DEPTH (self->flags);
1528 OUTPUT:
1529 RETVAL
1530
1477void max_size (JSON *self, UV max_size = 0) 1531void max_size (JSON *self, UV max_size = 0)
1478 PPCODE: 1532 PPCODE:
1479{ 1533{
1480 UV log2 = 0; 1534 UV log2 = 0;
1481 1535
1487 1541
1488 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1542 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1489 1543
1490 XPUSHs (ST (0)); 1544 XPUSHs (ST (0));
1491} 1545}
1546
1547int get_max_size (JSON *self)
1548 CODE:
1549 RETVAL = DEC_SIZE (self->flags);
1550 OUTPUT:
1551 RETVAL
1492 1552
1493void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1553void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1494 PPCODE: 1554 PPCODE:
1495{ 1555{
1496 SvREFCNT_dec (self->cb_object); 1556 SvREFCNT_dec (self->cb_object);
1543 SvREFCNT_dec (self->cb_sk_object); 1603 SvREFCNT_dec (self->cb_sk_object);
1544 SvREFCNT_dec (self->cb_object); 1604 SvREFCNT_dec (self->cb_object);
1545 1605
1546PROTOTYPES: ENABLE 1606PROTOTYPES: ENABLE
1547 1607
1548void to_json (SV *scalar) 1608void encode_json (SV *scalar)
1549 PPCODE: 1609 PPCODE:
1550{ 1610{
1551 JSON json = { F_DEFAULT | F_UTF8 }; 1611 JSON json = { F_DEFAULT | F_UTF8 };
1552 XPUSHs (encode_json (scalar, &json)); 1612 XPUSHs (encode_json (scalar, &json));
1553} 1613}
1554 1614
1555void from_json (SV *jsonstr) 1615void decode_json (SV *jsonstr)
1556 PPCODE: 1616 PPCODE:
1557{ 1617{
1558 JSON json = { F_DEFAULT | F_UTF8 }; 1618 JSON json = { F_DEFAULT | F_UTF8 };
1559 XPUSHs (decode_json (jsonstr, &json, 0)); 1619 XPUSHs (decode_json (jsonstr, &json, 0));
1560} 1620}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines