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.80 by root, Wed Mar 26 01:32:53 2008 UTC vs.
Revision 1.84 by root, Thu Mar 27 06:37:35 2008 UTC

31#define F_ALLOW_NONREF 0x00000100UL 31#define F_ALLOW_NONREF 0x00000100UL
32#define F_SHRINK 0x00000200UL 32#define F_SHRINK 0x00000200UL
33#define F_ALLOW_BLESSED 0x00000400UL 33#define F_ALLOW_BLESSED 0x00000400UL
34#define F_CONV_BLESSED 0x00000800UL 34#define F_CONV_BLESSED 0x00000800UL
35#define F_RELAXED 0x00001000UL 35#define F_RELAXED 0x00001000UL
36#define F_ALLOW_UNKNOWN 0x00002000UL
36 37
37#define F_MAXDEPTH 0xf8000000UL 38#define F_MAXDEPTH 0xf8000000UL
38#define S_MAXDEPTH 27 39#define S_MAXDEPTH 27
39#define F_MAXSIZE 0x01f00000UL 40#define F_MAXSIZE 0x01f00000UL
40#define S_MAXSIZE 20 41#define S_MAXSIZE 20
101 int incr_mode; 102 int incr_mode;
102} JSON; 103} JSON;
103 104
104///////////////////////////////////////////////////////////////////////////// 105/////////////////////////////////////////////////////////////////////////////
105// utility functions 106// utility functions
107
108INLINE SV *
109get_bool (const char *name)
110{
111 SV *sv = get_sv (name, 1);
112
113 SvREADONLY_on (sv);
114 SvREADONLY_on (SvRV (sv));
115
116 return sv;
117}
106 118
107INLINE void 119INLINE void
108shrink (SV *sv) 120shrink (SV *sv)
109{ 121{
110 sv_utf8_downgrade (sv, 1); 122 sv_utf8_downgrade (sv, 1);
631 643
632 if (len == 1 && *pv == '1') 644 if (len == 1 && *pv == '1')
633 encode_str (enc, "true", 4, 0); 645 encode_str (enc, "true", 4, 0);
634 else if (len == 1 && *pv == '0') 646 else if (len == 1 && *pv == '0')
635 encode_str (enc, "false", 5, 0); 647 encode_str (enc, "false", 5, 0);
648 else if (enc->json.flags & F_ALLOW_UNKNOWN)
649 encode_str (enc, "null", 4, 0);
636 else 650 else
637 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", 651 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
638 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 652 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
639 } 653 }
654 else if (enc->json.flags & F_ALLOW_UNKNOWN)
655 encode_str (enc, "null", 4, 0);
640 else 656 else
641 croak ("encountered %s, but JSON can only represent references to arrays or hashes", 657 croak ("encountered %s, but JSON can only represent references to arrays or hashes",
642 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 658 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
643} 659}
644 660
706 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); 722 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv));
707 } 723 }
708 } 724 }
709 else if (SvROK (sv)) 725 else if (SvROK (sv))
710 encode_rv (enc, SvRV (sv)); 726 encode_rv (enc, SvRV (sv));
711 else if (!SvOK (sv)) 727 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN)
712 encode_str (enc, "null", 4, 0); 728 encode_str (enc, "null", 4, 0);
713 else 729 else
714 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 730 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this",
715 SvPV_nolen (sv), SvFLAGS (sv)); 731 SvPV_nolen (sv), SvFLAGS (sv));
716} 732}
1343 case 't': 1359 case 't':
1344 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1360 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1345 { 1361 {
1346 dec->cur += 4; 1362 dec->cur += 4;
1347#if JSON_SLOW 1363#if JSON_SLOW
1348 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); 1364 json_true = get_bool ("JSON::XS::true");
1349#endif 1365#endif
1350 return SvREFCNT_inc (json_true); 1366 return newSVsv (json_true);
1351 } 1367 }
1352 else 1368 else
1353 ERR ("'true' expected"); 1369 ERR ("'true' expected");
1354 1370
1355 break; 1371 break;
1357 case 'f': 1373 case 'f':
1358 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1374 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1359 { 1375 {
1360 dec->cur += 5; 1376 dec->cur += 5;
1361#if JSON_SLOW 1377#if JSON_SLOW
1362 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1378 json_false = get_bool ("JSON::XS::false");
1363#endif 1379#endif
1364 return SvREFCNT_inc (json_false); 1380 return newSVsv (json_false);
1365 } 1381 }
1366 else 1382 else
1367 ERR ("'false' expected"); 1383 ERR ("'false' expected");
1368 1384
1369 break; 1385 break;
1607 : -1; 1623 : -1;
1608 1624
1609 json_stash = gv_stashpv ("JSON::XS" , 1); 1625 json_stash = gv_stashpv ("JSON::XS" , 1);
1610 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1626 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1611 1627
1612 json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); 1628 json_true = get_bool ("JSON::XS::true");
1613 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1629 json_false = get_bool ("JSON::XS::false");
1614} 1630}
1615 1631
1616PROTOTYPES: DISABLE 1632PROTOTYPES: DISABLE
1617 1633
1618void CLONE (...) 1634void CLONE (...)
1646 allow_nonref = F_ALLOW_NONREF 1662 allow_nonref = F_ALLOW_NONREF
1647 shrink = F_SHRINK 1663 shrink = F_SHRINK
1648 allow_blessed = F_ALLOW_BLESSED 1664 allow_blessed = F_ALLOW_BLESSED
1649 convert_blessed = F_CONV_BLESSED 1665 convert_blessed = F_CONV_BLESSED
1650 relaxed = F_RELAXED 1666 relaxed = F_RELAXED
1667 allow_unknown = F_ALLOW_UNKNOWN
1651 PPCODE: 1668 PPCODE:
1652{ 1669{
1653 if (enable) 1670 if (enable)
1654 self->flags |= ix; 1671 self->flags |= ix;
1655 else 1672 else
1670 get_allow_nonref = F_ALLOW_NONREF 1687 get_allow_nonref = F_ALLOW_NONREF
1671 get_shrink = F_SHRINK 1688 get_shrink = F_SHRINK
1672 get_allow_blessed = F_ALLOW_BLESSED 1689 get_allow_blessed = F_ALLOW_BLESSED
1673 get_convert_blessed = F_CONV_BLESSED 1690 get_convert_blessed = F_CONV_BLESSED
1674 get_relaxed = F_RELAXED 1691 get_relaxed = F_RELAXED
1692 get_allow_unknown = F_ALLOW_UNKNOWN
1675 PPCODE: 1693 PPCODE:
1676 XPUSHs (boolSV (self->flags & ix)); 1694 XPUSHs (boolSV (self->flags & ix));
1677 1695
1678void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1696void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1679 PPCODE: 1697 PPCODE:
1798 if (GIMME_V != G_VOID) 1816 if (GIMME_V != G_VOID)
1799 do 1817 do
1800 { 1818 {
1801 STRLEN offset; 1819 STRLEN offset;
1802 1820
1803 incr_parse (self);
1804
1805 if (!INCR_DONE (self)) 1821 if (!INCR_DONE (self))
1822 {
1823 incr_parse (self);
1824 if (!INCR_DONE (self))
1806 break; 1825 break;
1826 }
1807 1827
1808 XPUSHs (decode_json (self->incr_text, self, &offset)); 1828 XPUSHs (decode_json (self->incr_text, self, &offset));
1809 1829
1810 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); 1830 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1811 self->incr_pos -= offset; 1831 self->incr_pos -= offset;
1818SV *incr_text (JSON *self) 1838SV *incr_text (JSON *self)
1819 ATTRS: lvalue 1839 ATTRS: lvalue
1820 CODE: 1840 CODE:
1821{ 1841{
1822 if (self->incr_pos) 1842 if (self->incr_pos)
1823 croak ("incr_text can only be called after a successful incr_parse call in scalar context"); 1843 croak ("incr_text can not be called when the incremental parser already started parsing");
1824 1844
1825 RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef; 1845 RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef;
1826} 1846}
1827 OUTPUT: 1847 OUTPUT:
1828 RETVAL 1848 RETVAL
1829 1849
1830void incr_skip (JSON *self) 1850void incr_skip (JSON *self)
1831 CODE: 1851 CODE:
1832{ 1852{
1853 if (self->incr_pos)
1854 {
1833 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + self->incr_pos); 1855 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + self->incr_pos);
1834 self->incr_pos = 0; 1856 self->incr_pos = 0;
1835 self->incr_nest = 0; 1857 self->incr_nest = 0;
1836 self->incr_mode = 0; 1858 self->incr_mode = 0;
1859 }
1837} 1860}
1838 1861
1839void DESTROY (JSON *self) 1862void DESTROY (JSON *self)
1840 CODE: 1863 CODE:
1841 SvREFCNT_dec (self->cb_sk_object); 1864 SvREFCNT_dec (self->cb_sk_object);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines