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.64 by root, Mon Aug 27 02:03:23 2007 UTC vs.
Revision 1.70 by root, Wed Mar 19 00:44:54 2008 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)
989 1000
990 if (!is_nv) 1001 if (!is_nv)
991 { 1002 {
992 int len = dec->cur - start; 1003 int len = dec->cur - start;
993 1004
994 // 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
995 if (*start == '-') 1006 if (*start == '-')
996 switch (len) 1007 switch (len)
997 { 1008 {
998 case 2: return newSViv (-( start [1] - '0' * 1)); 1009 case 2: return newSViv (-( start [1] - '0' * 1));
999 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1010 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
1000 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));
1001 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));
1002 } 1014 }
1003 else 1015 else
1004 switch (len) 1016 switch (len)
1005 { 1017 {
1006 case 1: return newSViv ( start [0] - '0' * 1); 1018 case 1: return newSViv ( start [0] - '0' * 1);
1007 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1019 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
1008 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);
1009 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);
1010 } 1023 }
1011 1024
1012 { 1025 {
1013 UV uv; 1026 UV uv;
1014 int numtype = grok_number (start, len, &uv); 1027 int numtype = grok_number (start, len, &uv);
1446{ 1459{
1447 SV *pv = NEWSV (0, sizeof (JSON)); 1460 SV *pv = NEWSV (0, sizeof (JSON));
1448 SvPOK_only (pv); 1461 SvPOK_only (pv);
1449 Zero (SvPVX (pv), 1, JSON); 1462 Zero (SvPVX (pv), 1, JSON);
1450 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1463 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1451 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 )));
1452} 1468}
1453 1469
1454void ascii (JSON *self, int enable = 1) 1470void ascii (JSON *self, int enable = 1)
1455 ALIAS: 1471 ALIAS:
1456 ascii = F_ASCII 1472 ascii = F_ASCII
1474 self->flags &= ~ix; 1490 self->flags &= ~ix;
1475 1491
1476 XPUSHs (ST (0)); 1492 XPUSHs (ST (0));
1477} 1493}
1478 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
1479void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1512void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1480 PPCODE: 1513 PPCODE:
1481{ 1514{
1482 UV log2 = 0; 1515 UV log2 = 0;
1483 1516
1489 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1522 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1490 1523
1491 XPUSHs (ST (0)); 1524 XPUSHs (ST (0));
1492} 1525}
1493 1526
1527U32 get_max_depth (JSON *self)
1528 CODE:
1529 RETVAL = DEC_DEPTH (self->flags);
1530 OUTPUT:
1531 RETVAL
1532
1494void max_size (JSON *self, UV max_size = 0) 1533void max_size (JSON *self, UV max_size = 0)
1495 PPCODE: 1534 PPCODE:
1496{ 1535{
1497 UV log2 = 0; 1536 UV log2 = 0;
1498 1537
1504 1543
1505 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1544 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1506 1545
1507 XPUSHs (ST (0)); 1546 XPUSHs (ST (0));
1508} 1547}
1548
1549int get_max_size (JSON *self)
1550 CODE:
1551 RETVAL = DEC_SIZE (self->flags);
1552 OUTPUT:
1553 RETVAL
1509 1554
1510void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1555void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1511 PPCODE: 1556 PPCODE:
1512{ 1557{
1513 SvREFCNT_dec (self->cb_object); 1558 SvREFCNT_dec (self->cb_object);
1560 SvREFCNT_dec (self->cb_sk_object); 1605 SvREFCNT_dec (self->cb_sk_object);
1561 SvREFCNT_dec (self->cb_object); 1606 SvREFCNT_dec (self->cb_object);
1562 1607
1563PROTOTYPES: ENABLE 1608PROTOTYPES: ENABLE
1564 1609
1565void to_json (SV *scalar) 1610void encode_json (SV *scalar)
1566 PPCODE: 1611 PPCODE:
1567{ 1612{
1568 JSON json = { F_DEFAULT | F_UTF8 }; 1613 JSON json = { F_DEFAULT | F_UTF8 };
1569 XPUSHs (encode_json (scalar, &json)); 1614 XPUSHs (encode_json (scalar, &json));
1570} 1615}
1571 1616
1572void from_json (SV *jsonstr) 1617void decode_json (SV *jsonstr)
1573 PPCODE: 1618 PPCODE:
1574{ 1619{
1575 JSON json = { F_DEFAULT | F_UTF8 }; 1620 JSON json = { F_DEFAULT | F_UTF8 };
1576 XPUSHs (decode_json (jsonstr, &json, 0)); 1621 XPUSHs (decode_json (jsonstr, &json, 0));
1577} 1622}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines