serde_json/de.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697
//! Deserialize JSON data to a Rust data structure.
use crate::error::{Error, ErrorCode, Result};
#[cfg(feature = "float_roundtrip")]
use crate::lexical;
use crate::number::Number;
use crate::read::{self, Fused, Reference};
use alloc::string::String;
use alloc::vec::Vec;
#[cfg(feature = "float_roundtrip")]
use core::iter;
use core::iter::FusedIterator;
use core::marker::PhantomData;
use core::result;
use core::str::FromStr;
use serde::de::{self, Expected, Unexpected};
use serde::forward_to_deserialize_any;
#[cfg(feature = "arbitrary_precision")]
use crate::number::NumberDeserializer;
pub use crate::read::{Read, SliceRead, StrRead};
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub use crate::read::IoRead;
//////////////////////////////////////////////////////////////////////////////
/// A structure that deserializes JSON into Rust values.
pub struct Deserializer<R> {
read: R,
scratch: Vec<u8>,
remaining_depth: u8,
#[cfg(feature = "float_roundtrip")]
single_precision: bool,
#[cfg(feature = "unbounded_depth")]
disable_recursion_limit: bool,
}
impl<'de, R> Deserializer<R>
where
R: read::Read<'de>,
{
/// Create a JSON deserializer from one of the possible serde_json input
/// sources.
///
/// Typically it is more convenient to use one of these methods instead:
///
/// - Deserializer::from_str
/// - Deserializer::from_slice
/// - Deserializer::from_reader
pub fn new(read: R) -> Self {
Deserializer {
read,
scratch: Vec::new(),
remaining_depth: 128,
#[cfg(feature = "float_roundtrip")]
single_precision: false,
#[cfg(feature = "unbounded_depth")]
disable_recursion_limit: false,
}
}
}
#[cfg(feature = "std")]
impl<R> Deserializer<read::IoRead<R>>
where
R: crate::io::Read,
{
/// Creates a JSON deserializer from an `io::Read`.
///
/// Reader-based deserializers do not support deserializing borrowed types
/// like `&str`, since the `std::io::Read` trait has no non-copying methods
/// -- everything it does involves copying bytes out of the data source.
pub fn from_reader(reader: R) -> Self {
Deserializer::new(read::IoRead::new(reader))
}
}
impl<'a> Deserializer<read::SliceRead<'a>> {
/// Creates a JSON deserializer from a `&[u8]`.
pub fn from_slice(bytes: &'a [u8]) -> Self {
Deserializer::new(read::SliceRead::new(bytes))
}
}
impl<'a> Deserializer<read::StrRead<'a>> {
/// Creates a JSON deserializer from a `&str`.
pub fn from_str(s: &'a str) -> Self {
Deserializer::new(read::StrRead::new(s))
}
}
macro_rules! overflow {
($a:ident * 10 + $b:ident, $c:expr) => {
match $c {
c => $a >= c / 10 && ($a > c / 10 || $b > c % 10),
}
};
}
pub(crate) enum ParserNumber {
F64(f64),
U64(u64),
I64(i64),
#[cfg(feature = "arbitrary_precision")]
String(String),
}
impl ParserNumber {
fn visit<'de, V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
match self {
ParserNumber::F64(x) => visitor.visit_f64(x),
ParserNumber::U64(x) => visitor.visit_u64(x),
ParserNumber::I64(x) => visitor.visit_i64(x),
#[cfg(feature = "arbitrary_precision")]
ParserNumber::String(x) => visitor.visit_map(NumberDeserializer { number: x.into() }),
}
}
fn invalid_type(self, exp: &dyn Expected) -> Error {
match self {
ParserNumber::F64(x) => de::Error::invalid_type(Unexpected::Float(x), exp),
ParserNumber::U64(x) => de::Error::invalid_type(Unexpected::Unsigned(x), exp),
ParserNumber::I64(x) => de::Error::invalid_type(Unexpected::Signed(x), exp),
#[cfg(feature = "arbitrary_precision")]
ParserNumber::String(_) => de::Error::invalid_type(Unexpected::Other("number"), exp),
}
}
}
impl<'de, R: Read<'de>> Deserializer<R> {
/// The `Deserializer::end` method should be called after a value has been fully deserialized.
/// This allows the `Deserializer` to validate that the input stream is at the end or that it
/// only has trailing whitespace.
pub fn end(&mut self) -> Result<()> {
match tri!(self.parse_whitespace()) {
Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)),
None => Ok(()),
}
}
/// Turn a JSON deserializer into an iterator over values of type T.
pub fn into_iter<T>(self) -> StreamDeserializer<'de, R, T>
where
T: de::Deserialize<'de>,
{
// This cannot be an implementation of std::iter::IntoIterator because
// we need the caller to choose what T is.
let offset = self.read.byte_offset();
StreamDeserializer {
de: self,
offset,
failed: false,
output: PhantomData,
lifetime: PhantomData,
}
}
/// Parse arbitrarily deep JSON structures without any consideration for
/// overflowing the stack.
///
/// You will want to provide some other way to protect against stack
/// overflows, such as by wrapping your Deserializer in the dynamically
/// growing stack adapter provided by the serde_stacker crate. Additionally
/// you will need to be careful around other recursive operations on the
/// parsed result which may overflow the stack after deserialization has
/// completed, including, but not limited to, Display and Debug and Drop
/// impls.
///
/// *This method is only available if serde_json is built with the
/// `"unbounded_depth"` feature.*
///
/// # Examples
///
/// ```
/// use serde::Deserialize;
/// use serde_json::Value;
///
/// fn main() {
/// let mut json = String::new();
/// for _ in 0..10000 {
/// json = format!("[{}]", json);
/// }
///
/// let mut deserializer = serde_json::Deserializer::from_str(&json);
/// deserializer.disable_recursion_limit();
/// let deserializer = serde_stacker::Deserializer::new(&mut deserializer);
/// let value = Value::deserialize(deserializer).unwrap();
///
/// carefully_drop_nested_arrays(value);
/// }
///
/// fn carefully_drop_nested_arrays(value: Value) {
/// let mut stack = vec![value];
/// while let Some(value) = stack.pop() {
/// if let Value::Array(array) = value {
/// stack.extend(array);
/// }
/// }
/// }
/// ```
#[cfg(feature = "unbounded_depth")]
#[cfg_attr(docsrs, doc(cfg(feature = "unbounded_depth")))]
pub fn disable_recursion_limit(&mut self) {
self.disable_recursion_limit = true;
}
pub(crate) fn peek(&mut self) -> Result<Option<u8>> {
self.read.peek()
}
fn peek_or_null(&mut self) -> Result<u8> {
Ok(tri!(self.peek()).unwrap_or(b'\x00'))
}
fn eat_char(&mut self) {
self.read.discard();
}
fn next_char(&mut self) -> Result<Option<u8>> {
self.read.next()
}
fn next_char_or_null(&mut self) -> Result<u8> {
Ok(tri!(self.next_char()).unwrap_or(b'\x00'))
}
/// Error caused by a byte from next_char().
#[cold]
fn error(&self, reason: ErrorCode) -> Error {
let position = self.read.position();
Error::syntax(reason, position.line, position.column)
}
/// Error caused by a byte from peek().
#[cold]
fn peek_error(&self, reason: ErrorCode) -> Error {
let position = self.read.peek_position();
Error::syntax(reason, position.line, position.column)
}
/// Returns the first non-whitespace byte without consuming it, or `None` if
/// EOF is encountered.
fn parse_whitespace(&mut self) -> Result<Option<u8>> {
loop {
match tri!(self.peek()) {
Some(b' ' | b'\n' | b'\t' | b'\r') => {
self.eat_char();
}
other => {
return Ok(other);
}
}
}
}
#[cold]
fn peek_invalid_type(&mut self, exp: &dyn Expected) -> Error {
let err = match self.peek_or_null().unwrap_or(b'\x00') {
b'n' => {
self.eat_char();
if let Err(err) = self.parse_ident(b"ull") {
return err;
}
de::Error::invalid_type(Unexpected::Unit, exp)
}
b't' => {
self.eat_char();
if let Err(err) = self.parse_ident(b"rue") {
return err;
}
de::Error::invalid_type(Unexpected::Bool(true), exp)
}
b'f' => {
self.eat_char();
if let Err(err) = self.parse_ident(b"alse") {
return err;
}
de::Error::invalid_type(Unexpected::Bool(false), exp)
}
b'-' => {
self.eat_char();
match self.parse_any_number(false) {
Ok(n) => n.invalid_type(exp),
Err(err) => return err,
}
}
b'0'..=b'9' => match self.parse_any_number(true) {
Ok(n) => n.invalid_type(exp),
Err(err) => return err,
},
b'"' => {
self.eat_char();
self.scratch.clear();
match self.read.parse_str(&mut self.scratch) {
Ok(s) => de::Error::invalid_type(Unexpected::Str(&s), exp),
Err(err) => return err,
}
}
b'[' => de::Error::invalid_type(Unexpected::Seq, exp),
b'{' => de::Error::invalid_type(Unexpected::Map, exp),
_ => self.peek_error(ErrorCode::ExpectedSomeValue),
};
self.fix_position(err)
}
pub(crate) fn deserialize_number<'any, V>(&mut self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'any>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'-' => {
self.eat_char();
tri!(self.parse_integer(false)).visit(visitor)
}
b'0'..=b'9' => tri!(self.parse_integer(true)).visit(visitor),
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
#[cfg(feature = "float_roundtrip")]
pub(crate) fn do_deserialize_f32<'any, V>(&mut self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'any>,
{
self.single_precision = true;
let val = self.deserialize_number(visitor);
self.single_precision = false;
val
}
pub(crate) fn do_deserialize_i128<'any, V>(&mut self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'any>,
{
let mut buf = String::new();
match tri!(self.parse_whitespace()) {
Some(b'-') => {
self.eat_char();
buf.push('-');
}
Some(_) => {}
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
tri!(self.scan_integer128(&mut buf));
let value = match buf.parse() {
Ok(int) => visitor.visit_i128(int),
Err(_) => {
return Err(self.error(ErrorCode::NumberOutOfRange));
}
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
pub(crate) fn do_deserialize_u128<'any, V>(&mut self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'any>,
{
match tri!(self.parse_whitespace()) {
Some(b'-') => {
return Err(self.peek_error(ErrorCode::NumberOutOfRange));
}
Some(_) => {}
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
}
let mut buf = String::new();
tri!(self.scan_integer128(&mut buf));
let value = match buf.parse() {
Ok(int) => visitor.visit_u128(int),
Err(_) => {
return Err(self.error(ErrorCode::NumberOutOfRange));
}
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
fn scan_integer128(&mut self, buf: &mut String) -> Result<()> {
match tri!(self.next_char_or_null()) {
b'0' => {
buf.push('0');
// There can be only one leading '0'.
match tri!(self.peek_or_null()) {
b'0'..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)),
_ => Ok(()),
}
}
c @ b'1'..=b'9' => {
buf.push(c as char);
while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
self.eat_char();
buf.push(c as char);
}
Ok(())
}
_ => Err(self.error(ErrorCode::InvalidNumber)),
}
}
#[cold]
fn fix_position(&self, err: Error) -> Error {
err.fix_position(move |code| self.error(code))
}
fn parse_ident(&mut self, ident: &[u8]) -> Result<()> {
for expected in ident {
match tri!(self.next_char()) {
None => {
return Err(self.error(ErrorCode::EofWhileParsingValue));
}
Some(next) => {
if next != *expected {
return Err(self.error(ErrorCode::ExpectedSomeIdent));
}
}
}
}
Ok(())
}
fn parse_integer(&mut self, positive: bool) -> Result<ParserNumber> {
let next = match tri!(self.next_char()) {
Some(b) => b,
None => {
return Err(self.error(ErrorCode::EofWhileParsingValue));
}
};
match next {
b'0' => {
// There can be only one leading '0'.
match tri!(self.peek_or_null()) {
b'0'..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)),
_ => self.parse_number(positive, 0),
}
}
c @ b'1'..=b'9' => {
let mut significand = (c - b'0') as u64;
loop {
match tri!(self.peek_or_null()) {
c @ b'0'..=b'9' => {
let digit = (c - b'0') as u64;
// We need to be careful with overflow. If we can,
// try to keep the number as a `u64` until we grow
// too large. At that point, switch to parsing the
// value as a `f64`.
if overflow!(significand * 10 + digit, u64::MAX) {
return Ok(ParserNumber::F64(tri!(
self.parse_long_integer(positive, significand),
)));
}
self.eat_char();
significand = significand * 10 + digit;
}
_ => {
return self.parse_number(positive, significand);
}
}
}
}
_ => Err(self.error(ErrorCode::InvalidNumber)),
}
}
fn parse_number(&mut self, positive: bool, significand: u64) -> Result<ParserNumber> {
Ok(match tri!(self.peek_or_null()) {
b'.' => ParserNumber::F64(tri!(self.parse_decimal(positive, significand, 0))),
b'e' | b'E' => ParserNumber::F64(tri!(self.parse_exponent(positive, significand, 0))),
_ => {
if positive {
ParserNumber::U64(significand)
} else {
let neg = (significand as i64).wrapping_neg();
// Convert into a float if we underflow, or on `-0`.
if neg >= 0 {
ParserNumber::F64(-(significand as f64))
} else {
ParserNumber::I64(neg)
}
}
}
})
}
fn parse_decimal(
&mut self,
positive: bool,
mut significand: u64,
exponent_before_decimal_point: i32,
) -> Result<f64> {
self.eat_char();
let mut exponent_after_decimal_point = 0;
while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
let digit = (c - b'0') as u64;
if overflow!(significand * 10 + digit, u64::MAX) {
let exponent = exponent_before_decimal_point + exponent_after_decimal_point;
return self.parse_decimal_overflow(positive, significand, exponent);
}
self.eat_char();
significand = significand * 10 + digit;
exponent_after_decimal_point -= 1;
}
// Error if there is not at least one digit after the decimal point.
if exponent_after_decimal_point == 0 {
match tri!(self.peek()) {
Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)),
None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
}
}
let exponent = exponent_before_decimal_point + exponent_after_decimal_point;
match tri!(self.peek_or_null()) {
b'e' | b'E' => self.parse_exponent(positive, significand, exponent),
_ => self.f64_from_parts(positive, significand, exponent),
}
}
fn parse_exponent(
&mut self,
positive: bool,
significand: u64,
starting_exp: i32,
) -> Result<f64> {
self.eat_char();
let positive_exp = match tri!(self.peek_or_null()) {
b'+' => {
self.eat_char();
true
}
b'-' => {
self.eat_char();
false
}
_ => true,
};
let next = match tri!(self.next_char()) {
Some(b) => b,
None => {
return Err(self.error(ErrorCode::EofWhileParsingValue));
}
};
// Make sure a digit follows the exponent place.
let mut exp = match next {
c @ b'0'..=b'9' => (c - b'0') as i32,
_ => {
return Err(self.error(ErrorCode::InvalidNumber));
}
};
while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
self.eat_char();
let digit = (c - b'0') as i32;
if overflow!(exp * 10 + digit, i32::MAX) {
let zero_significand = significand == 0;
return self.parse_exponent_overflow(positive, zero_significand, positive_exp);
}
exp = exp * 10 + digit;
}
let final_exp = if positive_exp {
starting_exp.saturating_add(exp)
} else {
starting_exp.saturating_sub(exp)
};
self.f64_from_parts(positive, significand, final_exp)
}
#[cfg(feature = "float_roundtrip")]
fn f64_from_parts(&mut self, positive: bool, significand: u64, exponent: i32) -> Result<f64> {
let f = if self.single_precision {
lexical::parse_concise_float::<f32>(significand, exponent) as f64
} else {
lexical::parse_concise_float::<f64>(significand, exponent)
};
if f.is_infinite() {
Err(self.error(ErrorCode::NumberOutOfRange))
} else {
Ok(if positive { f } else { -f })
}
}
#[cfg(not(feature = "float_roundtrip"))]
fn f64_from_parts(
&mut self,
positive: bool,
significand: u64,
mut exponent: i32,
) -> Result<f64> {
let mut f = significand as f64;
loop {
match POW10.get(exponent.wrapping_abs() as usize) {
Some(&pow) => {
if exponent >= 0 {
f *= pow;
if f.is_infinite() {
return Err(self.error(ErrorCode::NumberOutOfRange));
}
} else {
f /= pow;
}
break;
}
None => {
if f == 0.0 {
break;
}
if exponent >= 0 {
return Err(self.error(ErrorCode::NumberOutOfRange));
}
f /= 1e308;
exponent += 308;
}
}
}
Ok(if positive { f } else { -f })
}
#[cfg(feature = "float_roundtrip")]
#[cold]
#[inline(never)]
fn parse_long_integer(&mut self, positive: bool, partial_significand: u64) -> Result<f64> {
// To deserialize floats we'll first push the integer and fraction
// parts, both as byte strings, into the scratch buffer and then feed
// both slices to lexical's parser. For example if the input is
// `12.34e5` we'll push b"1234" into scratch and then pass b"12" and
// b"34" to lexical. `integer_end` will be used to track where to split
// the scratch buffer.
//
// Note that lexical expects the integer part to contain *no* leading
// zeroes and the fraction part to contain *no* trailing zeroes. The
// first requirement is already handled by the integer parsing logic.
// The second requirement will be enforced just before passing the
// slices to lexical in f64_long_from_parts.
self.scratch.clear();
self.scratch
.extend_from_slice(itoa::Buffer::new().format(partial_significand).as_bytes());
loop {
match tri!(self.peek_or_null()) {
c @ b'0'..=b'9' => {
self.scratch.push(c);
self.eat_char();
}
b'.' => {
self.eat_char();
return self.parse_long_decimal(positive, self.scratch.len());
}
b'e' | b'E' => {
return self.parse_long_exponent(positive, self.scratch.len());
}
_ => {
return self.f64_long_from_parts(positive, self.scratch.len(), 0);
}
}
}
}
#[cfg(not(feature = "float_roundtrip"))]
#[cold]
#[inline(never)]
fn parse_long_integer(&mut self, positive: bool, significand: u64) -> Result<f64> {
let mut exponent = 0;
loop {
match tri!(self.peek_or_null()) {
b'0'..=b'9' => {
self.eat_char();
// This could overflow... if your integer is gigabytes long.
// Ignore that possibility.
exponent += 1;
}
b'.' => {
return self.parse_decimal(positive, significand, exponent);
}
b'e' | b'E' => {
return self.parse_exponent(positive, significand, exponent);
}
_ => {
return self.f64_from_parts(positive, significand, exponent);
}
}
}
}
#[cfg(feature = "float_roundtrip")]
#[cold]
fn parse_long_decimal(&mut self, positive: bool, integer_end: usize) -> Result<f64> {
let mut at_least_one_digit = integer_end < self.scratch.len();
while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
self.scratch.push(c);
self.eat_char();
at_least_one_digit = true;
}
if !at_least_one_digit {
match tri!(self.peek()) {
Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)),
None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
}
}
match tri!(self.peek_or_null()) {
b'e' | b'E' => self.parse_long_exponent(positive, integer_end),
_ => self.f64_long_from_parts(positive, integer_end, 0),
}
}
#[cfg(feature = "float_roundtrip")]
fn parse_long_exponent(&mut self, positive: bool, integer_end: usize) -> Result<f64> {
self.eat_char();
let positive_exp = match tri!(self.peek_or_null()) {
b'+' => {
self.eat_char();
true
}
b'-' => {
self.eat_char();
false
}
_ => true,
};
let next = match tri!(self.next_char()) {
Some(b) => b,
None => {
return Err(self.error(ErrorCode::EofWhileParsingValue));
}
};
// Make sure a digit follows the exponent place.
let mut exp = match next {
c @ b'0'..=b'9' => (c - b'0') as i32,
_ => {
return Err(self.error(ErrorCode::InvalidNumber));
}
};
while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
self.eat_char();
let digit = (c - b'0') as i32;
if overflow!(exp * 10 + digit, i32::MAX) {
let zero_significand = self.scratch.iter().all(|&digit| digit == b'0');
return self.parse_exponent_overflow(positive, zero_significand, positive_exp);
}
exp = exp * 10 + digit;
}
let final_exp = if positive_exp { exp } else { -exp };
self.f64_long_from_parts(positive, integer_end, final_exp)
}
// This cold code should not be inlined into the middle of the hot
// decimal-parsing loop above.
#[cfg(feature = "float_roundtrip")]
#[cold]
#[inline(never)]
fn parse_decimal_overflow(
&mut self,
positive: bool,
significand: u64,
exponent: i32,
) -> Result<f64> {
let mut buffer = itoa::Buffer::new();
let significand = buffer.format(significand);
let fraction_digits = -exponent as usize;
self.scratch.clear();
if let Some(zeros) = fraction_digits.checked_sub(significand.len() + 1) {
self.scratch.extend(iter::repeat(b'0').take(zeros + 1));
}
self.scratch.extend_from_slice(significand.as_bytes());
let integer_end = self.scratch.len() - fraction_digits;
self.parse_long_decimal(positive, integer_end)
}
#[cfg(not(feature = "float_roundtrip"))]
#[cold]
#[inline(never)]
fn parse_decimal_overflow(
&mut self,
positive: bool,
significand: u64,
exponent: i32,
) -> Result<f64> {
// The next multiply/add would overflow, so just ignore all further
// digits.
while let b'0'..=b'9' = tri!(self.peek_or_null()) {
self.eat_char();
}
match tri!(self.peek_or_null()) {
b'e' | b'E' => self.parse_exponent(positive, significand, exponent),
_ => self.f64_from_parts(positive, significand, exponent),
}
}
// This cold code should not be inlined into the middle of the hot
// exponent-parsing loop above.
#[cold]
#[inline(never)]
fn parse_exponent_overflow(
&mut self,
positive: bool,
zero_significand: bool,
positive_exp: bool,
) -> Result<f64> {
// Error instead of +/- infinity.
if !zero_significand && positive_exp {
return Err(self.error(ErrorCode::NumberOutOfRange));
}
while let b'0'..=b'9' = tri!(self.peek_or_null()) {
self.eat_char();
}
Ok(if positive { 0.0 } else { -0.0 })
}
#[cfg(feature = "float_roundtrip")]
fn f64_long_from_parts(
&mut self,
positive: bool,
integer_end: usize,
exponent: i32,
) -> Result<f64> {
let integer = &self.scratch[..integer_end];
let fraction = &self.scratch[integer_end..];
let f = if self.single_precision {
lexical::parse_truncated_float::<f32>(integer, fraction, exponent) as f64
} else {
lexical::parse_truncated_float::<f64>(integer, fraction, exponent)
};
if f.is_infinite() {
Err(self.error(ErrorCode::NumberOutOfRange))
} else {
Ok(if positive { f } else { -f })
}
}
fn parse_any_signed_number(&mut self) -> Result<ParserNumber> {
let peek = match tri!(self.peek()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'-' => {
self.eat_char();
self.parse_any_number(false)
}
b'0'..=b'9' => self.parse_any_number(true),
_ => Err(self.peek_error(ErrorCode::InvalidNumber)),
};
let value = match tri!(self.peek()) {
Some(_) => Err(self.peek_error(ErrorCode::InvalidNumber)),
None => value,
};
match value {
Ok(value) => Ok(value),
// The de::Error impl creates errors with unknown line and column.
// Fill in the position here by looking at the current index in the
// input. There is no way to tell whether this should call `error`
// or `peek_error` so pick the one that seems correct more often.
// Worst case, the position is off by one character.
Err(err) => Err(self.fix_position(err)),
}
}
#[cfg(not(feature = "arbitrary_precision"))]
fn parse_any_number(&mut self, positive: bool) -> Result<ParserNumber> {
self.parse_integer(positive)
}
#[cfg(feature = "arbitrary_precision")]
fn parse_any_number(&mut self, positive: bool) -> Result<ParserNumber> {
let mut buf = String::with_capacity(16);
if !positive {
buf.push('-');
}
tri!(self.scan_integer(&mut buf));
if positive {
if let Ok(unsigned) = buf.parse() {
return Ok(ParserNumber::U64(unsigned));
}
} else {
if let Ok(signed) = buf.parse() {
return Ok(ParserNumber::I64(signed));
}
}
Ok(ParserNumber::String(buf))
}
#[cfg(feature = "arbitrary_precision")]
fn scan_or_eof(&mut self, buf: &mut String) -> Result<u8> {
match tri!(self.next_char()) {
Some(b) => {
buf.push(b as char);
Ok(b)
}
None => Err(self.error(ErrorCode::EofWhileParsingValue)),
}
}
#[cfg(feature = "arbitrary_precision")]
fn scan_integer(&mut self, buf: &mut String) -> Result<()> {
match tri!(self.scan_or_eof(buf)) {
b'0' => {
// There can be only one leading '0'.
match tri!(self.peek_or_null()) {
b'0'..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)),
_ => self.scan_number(buf),
}
}
b'1'..=b'9' => loop {
match tri!(self.peek_or_null()) {
c @ b'0'..=b'9' => {
self.eat_char();
buf.push(c as char);
}
_ => {
return self.scan_number(buf);
}
}
},
_ => Err(self.error(ErrorCode::InvalidNumber)),
}
}
#[cfg(feature = "arbitrary_precision")]
fn scan_number(&mut self, buf: &mut String) -> Result<()> {
match tri!(self.peek_or_null()) {
b'.' => self.scan_decimal(buf),
e @ (b'e' | b'E') => self.scan_exponent(e as char, buf),
_ => Ok(()),
}
}
#[cfg(feature = "arbitrary_precision")]
fn scan_decimal(&mut self, buf: &mut String) -> Result<()> {
self.eat_char();
buf.push('.');
let mut at_least_one_digit = false;
while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
self.eat_char();
buf.push(c as char);
at_least_one_digit = true;
}
if !at_least_one_digit {
match tri!(self.peek()) {
Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)),
None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
}
}
match tri!(self.peek_or_null()) {
e @ (b'e' | b'E') => self.scan_exponent(e as char, buf),
_ => Ok(()),
}
}
#[cfg(feature = "arbitrary_precision")]
fn scan_exponent(&mut self, e: char, buf: &mut String) -> Result<()> {
self.eat_char();
buf.push(e);
match tri!(self.peek_or_null()) {
b'+' => {
self.eat_char();
buf.push('+');
}
b'-' => {
self.eat_char();
buf.push('-');
}
_ => {}
}
// Make sure a digit follows the exponent place.
match tri!(self.scan_or_eof(buf)) {
b'0'..=b'9' => {}
_ => {
return Err(self.error(ErrorCode::InvalidNumber));
}
}
while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
self.eat_char();
buf.push(c as char);
}
Ok(())
}
fn parse_object_colon(&mut self) -> Result<()> {
match tri!(self.parse_whitespace()) {
Some(b':') => {
self.eat_char();
Ok(())
}
Some(_) => Err(self.peek_error(ErrorCode::ExpectedColon)),
None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
}
}
fn end_seq(&mut self) -> Result<()> {
match tri!(self.parse_whitespace()) {
Some(b']') => {
self.eat_char();
Ok(())
}
Some(b',') => {
self.eat_char();
match self.parse_whitespace() {
Ok(Some(b']')) => Err(self.peek_error(ErrorCode::TrailingComma)),
_ => Err(self.peek_error(ErrorCode::TrailingCharacters)),
}
}
Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)),
None => Err(self.peek_error(ErrorCode::EofWhileParsingList)),
}
}
fn end_map(&mut self) -> Result<()> {
match tri!(self.parse_whitespace()) {
Some(b'}') => {
self.eat_char();
Ok(())
}
Some(b',') => Err(self.peek_error(ErrorCode::TrailingComma)),
Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)),
None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
}
}
fn ignore_value(&mut self) -> Result<()> {
self.scratch.clear();
let mut enclosing = None;
loop {
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let frame = match peek {
b'n' => {
self.eat_char();
tri!(self.parse_ident(b"ull"));
None
}
b't' => {
self.eat_char();
tri!(self.parse_ident(b"rue"));
None
}
b'f' => {
self.eat_char();
tri!(self.parse_ident(b"alse"));
None
}
b'-' => {
self.eat_char();
tri!(self.ignore_integer());
None
}
b'0'..=b'9' => {
tri!(self.ignore_integer());
None
}
b'"' => {
self.eat_char();
tri!(self.read.ignore_str());
None
}
frame @ (b'[' | b'{') => {
self.scratch.extend(enclosing.take());
self.eat_char();
Some(frame)
}
_ => return Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
};
let (mut accept_comma, mut frame) = match frame {
Some(frame) => (false, frame),
None => match enclosing.take() {
Some(frame) => (true, frame),
None => match self.scratch.pop() {
Some(frame) => (true, frame),
None => return Ok(()),
},
},
};
loop {
match tri!(self.parse_whitespace()) {
Some(b',') if accept_comma => {
self.eat_char();
break;
}
Some(b']') if frame == b'[' => {}
Some(b'}') if frame == b'{' => {}
Some(_) => {
if accept_comma {
return Err(self.peek_error(match frame {
b'[' => ErrorCode::ExpectedListCommaOrEnd,
b'{' => ErrorCode::ExpectedObjectCommaOrEnd,
_ => unreachable!(),
}));
} else {
break;
}
}
None => {
return Err(self.peek_error(match frame {
b'[' => ErrorCode::EofWhileParsingList,
b'{' => ErrorCode::EofWhileParsingObject,
_ => unreachable!(),
}));
}
}
self.eat_char();
frame = match self.scratch.pop() {
Some(frame) => frame,
None => return Ok(()),
};
accept_comma = true;
}
if frame == b'{' {
match tri!(self.parse_whitespace()) {
Some(b'"') => self.eat_char(),
Some(_) => return Err(self.peek_error(ErrorCode::KeyMustBeAString)),
None => return Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
}
tri!(self.read.ignore_str());
match tri!(self.parse_whitespace()) {
Some(b':') => self.eat_char(),
Some(_) => return Err(self.peek_error(ErrorCode::ExpectedColon)),
None => return Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
}
}
enclosing = Some(frame);
}
}
fn ignore_integer(&mut self) -> Result<()> {
match tri!(self.next_char_or_null()) {
b'0' => {
// There can be only one leading '0'.
if let b'0'..=b'9' = tri!(self.peek_or_null()) {
return Err(self.peek_error(ErrorCode::InvalidNumber));
}
}
b'1'..=b'9' => {
while let b'0'..=b'9' = tri!(self.peek_or_null()) {
self.eat_char();
}
}
_ => {
return Err(self.error(ErrorCode::InvalidNumber));
}
}
match tri!(self.peek_or_null()) {
b'.' => self.ignore_decimal(),
b'e' | b'E' => self.ignore_exponent(),
_ => Ok(()),
}
}
fn ignore_decimal(&mut self) -> Result<()> {
self.eat_char();
let mut at_least_one_digit = false;
while let b'0'..=b'9' = tri!(self.peek_or_null()) {
self.eat_char();
at_least_one_digit = true;
}
if !at_least_one_digit {
return Err(self.peek_error(ErrorCode::InvalidNumber));
}
match tri!(self.peek_or_null()) {
b'e' | b'E' => self.ignore_exponent(),
_ => Ok(()),
}
}
fn ignore_exponent(&mut self) -> Result<()> {
self.eat_char();
match tri!(self.peek_or_null()) {
b'+' | b'-' => self.eat_char(),
_ => {}
}
// Make sure a digit follows the exponent place.
match tri!(self.next_char_or_null()) {
b'0'..=b'9' => {}
_ => {
return Err(self.error(ErrorCode::InvalidNumber));
}
}
while let b'0'..=b'9' = tri!(self.peek_or_null()) {
self.eat_char();
}
Ok(())
}
#[cfg(feature = "raw_value")]
fn deserialize_raw_value<V>(&mut self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
tri!(self.parse_whitespace());
self.read.begin_raw_buffering();
tri!(self.ignore_value());
self.read.end_raw_buffering(visitor)
}
}
impl FromStr for Number {
type Err = Error;
fn from_str(s: &str) -> result::Result<Self, Self::Err> {
Deserializer::from_str(s)
.parse_any_signed_number()
.map(Into::into)
}
}
#[cfg(not(feature = "float_roundtrip"))]
static POW10: [f64; 309] = [
1e000, 1e001, 1e002, 1e003, 1e004, 1e005, 1e006, 1e007, 1e008, 1e009, //
1e010, 1e011, 1e012, 1e013, 1e014, 1e015, 1e016, 1e017, 1e018, 1e019, //
1e020, 1e021, 1e022, 1e023, 1e024, 1e025, 1e026, 1e027, 1e028, 1e029, //
1e030, 1e031, 1e032, 1e033, 1e034, 1e035, 1e036, 1e037, 1e038, 1e039, //
1e040, 1e041, 1e042, 1e043, 1e044, 1e045, 1e046, 1e047, 1e048, 1e049, //
1e050, 1e051, 1e052, 1e053, 1e054, 1e055, 1e056, 1e057, 1e058, 1e059, //
1e060, 1e061, 1e062, 1e063, 1e064, 1e065, 1e066, 1e067, 1e068, 1e069, //
1e070, 1e071, 1e072, 1e073, 1e074, 1e075, 1e076, 1e077, 1e078, 1e079, //
1e080, 1e081, 1e082, 1e083, 1e084, 1e085, 1e086, 1e087, 1e088, 1e089, //
1e090, 1e091, 1e092, 1e093, 1e094, 1e095, 1e096, 1e097, 1e098, 1e099, //
1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109, //
1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119, //
1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129, //
1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139, //
1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149, //
1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159, //
1e160, 1e161, 1e162, 1e163, 1e164, 1e165, 1e166, 1e167, 1e168, 1e169, //
1e170, 1e171, 1e172, 1e173, 1e174, 1e175, 1e176, 1e177, 1e178, 1e179, //
1e180, 1e181, 1e182, 1e183, 1e184, 1e185, 1e186, 1e187, 1e188, 1e189, //
1e190, 1e191, 1e192, 1e193, 1e194, 1e195, 1e196, 1e197, 1e198, 1e199, //
1e200, 1e201, 1e202, 1e203, 1e204, 1e205, 1e206, 1e207, 1e208, 1e209, //
1e210, 1e211, 1e212, 1e213, 1e214, 1e215, 1e216, 1e217, 1e218, 1e219, //
1e220, 1e221, 1e222, 1e223, 1e224, 1e225, 1e226, 1e227, 1e228, 1e229, //
1e230, 1e231, 1e232, 1e233, 1e234, 1e235, 1e236, 1e237, 1e238, 1e239, //
1e240, 1e241, 1e242, 1e243, 1e244, 1e245, 1e246, 1e247, 1e248, 1e249, //
1e250, 1e251, 1e252, 1e253, 1e254, 1e255, 1e256, 1e257, 1e258, 1e259, //
1e260, 1e261, 1e262, 1e263, 1e264, 1e265, 1e266, 1e267, 1e268, 1e269, //
1e270, 1e271, 1e272, 1e273, 1e274, 1e275, 1e276, 1e277, 1e278, 1e279, //
1e280, 1e281, 1e282, 1e283, 1e284, 1e285, 1e286, 1e287, 1e288, 1e289, //
1e290, 1e291, 1e292, 1e293, 1e294, 1e295, 1e296, 1e297, 1e298, 1e299, //
1e300, 1e301, 1e302, 1e303, 1e304, 1e305, 1e306, 1e307, 1e308,
];
macro_rules! deserialize_number {
($method:ident) => {
deserialize_number!($method, deserialize_number);
};
($method:ident, $using:ident) => {
fn $method<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.$using(visitor)
}
};
}
#[cfg(not(feature = "unbounded_depth"))]
macro_rules! if_checking_recursion_limit {
($($body:tt)*) => {
$($body)*
};
}
#[cfg(feature = "unbounded_depth")]
macro_rules! if_checking_recursion_limit {
($this:ident $($body:tt)*) => {
if !$this.disable_recursion_limit {
$this $($body)*
}
};
}
macro_rules! check_recursion {
($this:ident $($body:tt)*) => {
if_checking_recursion_limit! {
$this.remaining_depth -= 1;
if $this.remaining_depth == 0 {
return Err($this.peek_error(ErrorCode::RecursionLimitExceeded));
}
}
$this $($body)*
if_checking_recursion_limit! {
$this.remaining_depth += 1;
}
};
}
impl<'de, R: Read<'de>> de::Deserializer<'de> for &mut Deserializer<R> {
type Error = Error;
#[inline]
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'n' => {
self.eat_char();
tri!(self.parse_ident(b"ull"));
visitor.visit_unit()
}
b't' => {
self.eat_char();
tri!(self.parse_ident(b"rue"));
visitor.visit_bool(true)
}
b'f' => {
self.eat_char();
tri!(self.parse_ident(b"alse"));
visitor.visit_bool(false)
}
b'-' => {
self.eat_char();
tri!(self.parse_any_number(false)).visit(visitor)
}
b'0'..=b'9' => tri!(self.parse_any_number(true)).visit(visitor),
b'"' => {
self.eat_char();
self.scratch.clear();
match tri!(self.read.parse_str(&mut self.scratch)) {
Reference::Borrowed(s) => visitor.visit_borrowed_str(s),
Reference::Copied(s) => visitor.visit_str(s),
}
}
b'[' => {
check_recursion! {
self.eat_char();
let ret = visitor.visit_seq(SeqAccess::new(self));
}
match (ret, self.end_seq()) {
(Ok(ret), Ok(())) => Ok(ret),
(Err(err), _) | (_, Err(err)) => Err(err),
}
}
b'{' => {
check_recursion! {
self.eat_char();
let ret = visitor.visit_map(MapAccess::new(self));
}
match (ret, self.end_map()) {
(Ok(ret), Ok(())) => Ok(ret),
(Err(err), _) | (_, Err(err)) => Err(err),
}
}
_ => Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
};
match value {
Ok(value) => Ok(value),
// The de::Error impl creates errors with unknown line and column.
// Fill in the position here by looking at the current index in the
// input. There is no way to tell whether this should call `error`
// or `peek_error` so pick the one that seems correct more often.
// Worst case, the position is off by one character.
Err(err) => Err(self.fix_position(err)),
}
}
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b't' => {
self.eat_char();
tri!(self.parse_ident(b"rue"));
visitor.visit_bool(true)
}
b'f' => {
self.eat_char();
tri!(self.parse_ident(b"alse"));
visitor.visit_bool(false)
}
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
deserialize_number!(deserialize_i8);
deserialize_number!(deserialize_i16);
deserialize_number!(deserialize_i32);
deserialize_number!(deserialize_i64);
deserialize_number!(deserialize_u8);
deserialize_number!(deserialize_u16);
deserialize_number!(deserialize_u32);
deserialize_number!(deserialize_u64);
#[cfg(not(feature = "float_roundtrip"))]
deserialize_number!(deserialize_f32);
deserialize_number!(deserialize_f64);
#[cfg(feature = "float_roundtrip")]
deserialize_number!(deserialize_f32, do_deserialize_f32);
deserialize_number!(deserialize_i128, do_deserialize_i128);
deserialize_number!(deserialize_u128, do_deserialize_u128);
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_str(visitor)
}
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'"' => {
self.eat_char();
self.scratch.clear();
match tri!(self.read.parse_str(&mut self.scratch)) {
Reference::Borrowed(s) => visitor.visit_borrowed_str(s),
Reference::Copied(s) => visitor.visit_str(s),
}
}
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_str(visitor)
}
/// Parses a JSON string as bytes. Note that this function does not check
/// whether the bytes represent a valid UTF-8 string.
///
/// The relevant part of the JSON specification is Section 8.2 of [RFC
/// 7159]:
///
/// > When all the strings represented in a JSON text are composed entirely
/// > of Unicode characters (however escaped), then that JSON text is
/// > interoperable in the sense that all software implementations that
/// > parse it will agree on the contents of names and of string values in
/// > objects and arrays.
/// >
/// > However, the ABNF in this specification allows member names and string
/// > values to contain bit sequences that cannot encode Unicode characters;
/// > for example, "\uDEAD" (a single unpaired UTF-16 surrogate). Instances
/// > of this have been observed, for example, when a library truncates a
/// > UTF-16 string without checking whether the truncation split a
/// > surrogate pair. The behavior of software that receives JSON texts
/// > containing such values is unpredictable; for example, implementations
/// > might return different values for the length of a string value or even
/// > suffer fatal runtime exceptions.
///
/// [RFC 7159]: https://tools.ietf.org/html/rfc7159
///
/// The behavior of serde_json is specified to fail on non-UTF-8 strings
/// when deserializing into Rust UTF-8 string types such as String, and
/// succeed with the bytes representing the [WTF-8] encoding of code points
/// when deserializing using this method.
///
/// [WTF-8]: https://simonsapin.github.io/wtf-8
///
/// Escape sequences are processed as usual, and for `\uXXXX` escapes it is
/// still checked if the hex number represents a valid Unicode code point.
///
/// # Examples
///
/// You can use this to parse JSON strings containing invalid UTF-8 bytes,
/// or unpaired surrogates.
///
/// ```
/// use serde_bytes::ByteBuf;
///
/// fn look_at_bytes() -> Result<(), serde_json::Error> {
/// let json_data = b"\"some bytes: \xe5\x00\xe5\"";
/// let bytes: ByteBuf = serde_json::from_slice(json_data)?;
///
/// assert_eq!(b'\xe5', bytes[12]);
/// assert_eq!(b'\0', bytes[13]);
/// assert_eq!(b'\xe5', bytes[14]);
///
/// Ok(())
/// }
/// #
/// # look_at_bytes().unwrap();
/// ```
///
/// Backslash escape sequences like `\n` are still interpreted and required
/// to be valid. `\u` escape sequences are required to represent a valid
/// Unicode code point or lone surrogate.
///
/// ```
/// use serde_bytes::ByteBuf;
///
/// fn look_at_bytes() -> Result<(), serde_json::Error> {
/// let json_data = b"\"lone surrogate: \\uD801\"";
/// let bytes: ByteBuf = serde_json::from_slice(json_data)?;
/// let expected = b"lone surrogate: \xED\xA0\x81";
/// assert_eq!(expected, bytes.as_slice());
/// Ok(())
/// }
/// #
/// # look_at_bytes();
/// ```
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'"' => {
self.eat_char();
self.scratch.clear();
match tri!(self.read.parse_str_raw(&mut self.scratch)) {
Reference::Borrowed(b) => visitor.visit_borrowed_bytes(b),
Reference::Copied(b) => visitor.visit_bytes(b),
}
}
b'[' => self.deserialize_seq(visitor),
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
#[inline]
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_bytes(visitor)
}
/// Parses a `null` as a None, and any other values as a `Some(...)`.
#[inline]
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
match tri!(self.parse_whitespace()) {
Some(b'n') => {
self.eat_char();
tri!(self.parse_ident(b"ull"));
visitor.visit_none()
}
_ => visitor.visit_some(self),
}
}
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'n' => {
self.eat_char();
tri!(self.parse_ident(b"ull"));
visitor.visit_unit()
}
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_unit(visitor)
}
/// Parses a newtype struct as the underlying value.
#[inline]
fn deserialize_newtype_struct<V>(self, name: &str, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
#[cfg(feature = "raw_value")]
{
if name == crate::raw::TOKEN {
return self.deserialize_raw_value(visitor);
}
}
let _ = name;
visitor.visit_newtype_struct(self)
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'[' => {
check_recursion! {
self.eat_char();
let ret = visitor.visit_seq(SeqAccess::new(self));
}
match (ret, self.end_seq()) {
(Ok(ret), Ok(())) => Ok(ret),
(Err(err), _) | (_, Err(err)) => Err(err),
}
}
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_seq(visitor)
}
fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
_len: usize,
visitor: V,
) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_seq(visitor)
}
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'{' => {
check_recursion! {
self.eat_char();
let ret = visitor.visit_map(MapAccess::new(self));
}
match (ret, self.end_map()) {
(Ok(ret), Ok(())) => Ok(ret),
(Err(err), _) | (_, Err(err)) => Err(err),
}
}
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
fn deserialize_struct<V>(
self,
_name: &'static str,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'[' => {
check_recursion! {
self.eat_char();
let ret = visitor.visit_seq(SeqAccess::new(self));
}
match (ret, self.end_seq()) {
(Ok(ret), Ok(())) => Ok(ret),
(Err(err), _) | (_, Err(err)) => Err(err),
}
}
b'{' => {
check_recursion! {
self.eat_char();
let ret = visitor.visit_map(MapAccess::new(self));
}
match (ret, self.end_map()) {
(Ok(ret), Ok(())) => Ok(ret),
(Err(err), _) | (_, Err(err)) => Err(err),
}
}
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
/// Parses an enum as an object like `{"$KEY":$VALUE}`, where $VALUE is either a straight
/// value, a `[..]`, or a `{..}`.
#[inline]
fn deserialize_enum<V>(
self,
_name: &str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
match tri!(self.parse_whitespace()) {
Some(b'{') => {
check_recursion! {
self.eat_char();
let ret = visitor.visit_enum(VariantAccess::new(self));
}
let value = tri!(ret);
match tri!(self.parse_whitespace()) {
Some(b'}') => {
self.eat_char();
Ok(value)
}
Some(_) => Err(self.error(ErrorCode::ExpectedSomeValue)),
None => Err(self.error(ErrorCode::EofWhileParsingObject)),
}
}
Some(b'"') => visitor.visit_enum(UnitVariantAccess::new(self)),
Some(_) => Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
None => Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
}
}
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_str(visitor)
}
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
tri!(self.ignore_value());
visitor.visit_unit()
}
}
struct SeqAccess<'a, R: 'a> {
de: &'a mut Deserializer<R>,
first: bool,
}
impl<'a, R: 'a> SeqAccess<'a, R> {
fn new(de: &'a mut Deserializer<R>) -> Self {
SeqAccess { de, first: true }
}
}
impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
type Error = Error;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
where
T: de::DeserializeSeed<'de>,
{
fn has_next_element<'de, 'a, R: Read<'de> + 'a>(
seq: &mut SeqAccess<'a, R>,
) -> Result<bool> {
let peek = match tri!(seq.de.parse_whitespace()) {
Some(b) => b,
None => {
return Err(seq.de.peek_error(ErrorCode::EofWhileParsingList));
}
};
if peek == b']' {
Ok(false)
} else if seq.first {
seq.first = false;
Ok(true)
} else if peek == b',' {
seq.de.eat_char();
match tri!(seq.de.parse_whitespace()) {
Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Ok(true),
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)),
}
} else {
Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd))
}
}
if tri!(has_next_element(self)) {
Ok(Some(tri!(seed.deserialize(&mut *self.de))))
} else {
Ok(None)
}
}
}
struct MapAccess<'a, R: 'a> {
de: &'a mut Deserializer<R>,
first: bool,
}
impl<'a, R: 'a> MapAccess<'a, R> {
fn new(de: &'a mut Deserializer<R>) -> Self {
MapAccess { de, first: true }
}
}
impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
type Error = Error;
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
where
K: de::DeserializeSeed<'de>,
{
fn has_next_key<'de, 'a, R: Read<'de> + 'a>(map: &mut MapAccess<'a, R>) -> Result<bool> {
let peek = match tri!(map.de.parse_whitespace()) {
Some(b) => b,
None => {
return Err(map.de.peek_error(ErrorCode::EofWhileParsingObject));
}
};
if peek == b'}' {
Ok(false)
} else if map.first {
map.first = false;
if peek == b'"' {
Ok(true)
} else {
Err(map.de.peek_error(ErrorCode::KeyMustBeAString))
}
} else if peek == b',' {
map.de.eat_char();
match tri!(map.de.parse_whitespace()) {
Some(b'"') => Ok(true),
Some(b'}') => Err(map.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Err(map.de.peek_error(ErrorCode::KeyMustBeAString)),
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
}
} else {
Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd))
}
}
if tri!(has_next_key(self)) {
Ok(Some(tri!(seed.deserialize(MapKey { de: &mut *self.de }))))
} else {
Ok(None)
}
}
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
where
V: de::DeserializeSeed<'de>,
{
tri!(self.de.parse_object_colon());
seed.deserialize(&mut *self.de)
}
}
struct VariantAccess<'a, R: 'a> {
de: &'a mut Deserializer<R>,
}
impl<'a, R: 'a> VariantAccess<'a, R> {
fn new(de: &'a mut Deserializer<R>) -> Self {
VariantAccess { de }
}
}
impl<'de, 'a, R: Read<'de> + 'a> de::EnumAccess<'de> for VariantAccess<'a, R> {
type Error = Error;
type Variant = Self;
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)>
where
V: de::DeserializeSeed<'de>,
{
let val = tri!(seed.deserialize(&mut *self.de));
tri!(self.de.parse_object_colon());
Ok((val, self))
}
}
impl<'de, 'a, R: Read<'de> + 'a> de::VariantAccess<'de> for VariantAccess<'a, R> {
type Error = Error;
fn unit_variant(self) -> Result<()> {
de::Deserialize::deserialize(self.de)
}
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
where
T: de::DeserializeSeed<'de>,
{
seed.deserialize(self.de)
}
fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
de::Deserializer::deserialize_seq(self.de, visitor)
}
fn struct_variant<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
de::Deserializer::deserialize_struct(self.de, "", fields, visitor)
}
}
struct UnitVariantAccess<'a, R: 'a> {
de: &'a mut Deserializer<R>,
}
impl<'a, R: 'a> UnitVariantAccess<'a, R> {
fn new(de: &'a mut Deserializer<R>) -> Self {
UnitVariantAccess { de }
}
}
impl<'de, 'a, R: Read<'de> + 'a> de::EnumAccess<'de> for UnitVariantAccess<'a, R> {
type Error = Error;
type Variant = Self;
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)>
where
V: de::DeserializeSeed<'de>,
{
let variant = tri!(seed.deserialize(&mut *self.de));
Ok((variant, self))
}
}
impl<'de, 'a, R: Read<'de> + 'a> de::VariantAccess<'de> for UnitVariantAccess<'a, R> {
type Error = Error;
fn unit_variant(self) -> Result<()> {
Ok(())
}
fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value>
where
T: de::DeserializeSeed<'de>,
{
Err(de::Error::invalid_type(
Unexpected::UnitVariant,
&"newtype variant",
))
}
fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
Err(de::Error::invalid_type(
Unexpected::UnitVariant,
&"tuple variant",
))
}
fn struct_variant<V>(self, _fields: &'static [&'static str], _visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
Err(de::Error::invalid_type(
Unexpected::UnitVariant,
&"struct variant",
))
}
}
/// Only deserialize from this after peeking a '"' byte! Otherwise it may
/// deserialize invalid JSON successfully.
struct MapKey<'a, R: 'a> {
de: &'a mut Deserializer<R>,
}
macro_rules! deserialize_numeric_key {
($method:ident) => {
fn $method<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_number(visitor)
}
};
($method:ident, $delegate:ident) => {
fn $method<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.de.eat_char();
match tri!(self.de.peek()) {
Some(b'0'..=b'9' | b'-') => {}
_ => return Err(self.de.error(ErrorCode::ExpectedNumericKey)),
}
let value = tri!(self.de.$delegate(visitor));
match tri!(self.de.peek()) {
Some(b'"') => self.de.eat_char(),
_ => return Err(self.de.peek_error(ErrorCode::ExpectedDoubleQuote)),
}
Ok(value)
}
};
}
impl<'de, 'a, R> MapKey<'a, R>
where
R: Read<'de>,
{
deserialize_numeric_key!(deserialize_number, deserialize_number);
}
impl<'de, 'a, R> de::Deserializer<'de> for MapKey<'a, R>
where
R: Read<'de>,
{
type Error = Error;
#[inline]
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.de.eat_char();
self.de.scratch.clear();
match tri!(self.de.read.parse_str(&mut self.de.scratch)) {
Reference::Borrowed(s) => visitor.visit_borrowed_str(s),
Reference::Copied(s) => visitor.visit_str(s),
}
}
deserialize_numeric_key!(deserialize_i8);
deserialize_numeric_key!(deserialize_i16);
deserialize_numeric_key!(deserialize_i32);
deserialize_numeric_key!(deserialize_i64);
deserialize_numeric_key!(deserialize_i128, deserialize_i128);
deserialize_numeric_key!(deserialize_u8);
deserialize_numeric_key!(deserialize_u16);
deserialize_numeric_key!(deserialize_u32);
deserialize_numeric_key!(deserialize_u64);
deserialize_numeric_key!(deserialize_u128, deserialize_u128);
#[cfg(not(feature = "float_roundtrip"))]
deserialize_numeric_key!(deserialize_f32);
#[cfg(feature = "float_roundtrip")]
deserialize_numeric_key!(deserialize_f32, deserialize_f32);
deserialize_numeric_key!(deserialize_f64);
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.de.eat_char();
let peek = match tri!(self.de.next_char()) {
Some(b) => b,
None => {
return Err(self.de.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b't' => {
tri!(self.de.parse_ident(b"rue\""));
visitor.visit_bool(true)
}
b'f' => {
tri!(self.de.parse_ident(b"alse\""));
visitor.visit_bool(false)
}
_ => {
self.de.scratch.clear();
let s = tri!(self.de.read.parse_str(&mut self.de.scratch));
Err(de::Error::invalid_type(Unexpected::Str(&s), &visitor))
}
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.de.fix_position(err)),
}
}
#[inline]
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
// Map keys cannot be null.
visitor.visit_some(self)
}
#[inline]
fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
#[cfg(feature = "raw_value")]
{
if name == crate::raw::TOKEN {
return self.de.deserialize_raw_value(visitor);
}
}
let _ = name;
visitor.visit_newtype_struct(self)
}
#[inline]
fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.de.deserialize_enum(name, variants, visitor)
}
#[inline]
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.de.deserialize_bytes(visitor)
}
#[inline]
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.de.deserialize_bytes(visitor)
}
forward_to_deserialize_any! {
char str string unit unit_struct seq tuple tuple_struct map struct
identifier ignored_any
}
}
//////////////////////////////////////////////////////////////////////////////
/// Iterator that deserializes a stream into multiple JSON values.
///
/// A stream deserializer can be created from any JSON deserializer using the
/// `Deserializer::into_iter` method.
///
/// The data can consist of any JSON value. Values need to be a self-delineating value e.g.
/// arrays, objects, or strings, or be followed by whitespace or a self-delineating value.
///
/// ```
/// use serde_json::{Deserializer, Value};
///
/// fn main() {
/// let data = "{\"k\": 3}1\"cool\"\"stuff\" 3{} [0, 1, 2]";
///
/// let stream = Deserializer::from_str(data).into_iter::<Value>();
///
/// for value in stream {
/// println!("{}", value.unwrap());
/// }
/// }
/// ```
pub struct StreamDeserializer<'de, R, T> {
de: Deserializer<R>,
offset: usize,
failed: bool,
output: PhantomData<T>,
lifetime: PhantomData<&'de ()>,
}
impl<'de, R, T> StreamDeserializer<'de, R, T>
where
R: read::Read<'de>,
T: de::Deserialize<'de>,
{
/// Create a JSON stream deserializer from one of the possible serde_json
/// input sources.
///
/// Typically it is more convenient to use one of these methods instead:
///
/// - Deserializer::from_str(...).into_iter()
/// - Deserializer::from_slice(...).into_iter()
/// - Deserializer::from_reader(...).into_iter()
pub fn new(read: R) -> Self {
let offset = read.byte_offset();
StreamDeserializer {
de: Deserializer::new(read),
offset,
failed: false,
output: PhantomData,
lifetime: PhantomData,
}
}
/// Returns the number of bytes so far deserialized into a successful `T`.
///
/// If a stream deserializer returns an EOF error, new data can be joined to
/// `old_data[stream.byte_offset()..]` to try again.
///
/// ```
/// let data = b"[0] [1] [";
///
/// let de = serde_json::Deserializer::from_slice(data);
/// let mut stream = de.into_iter::<Vec<i32>>();
/// assert_eq!(0, stream.byte_offset());
///
/// println!("{:?}", stream.next()); // [0]
/// assert_eq!(3, stream.byte_offset());
///
/// println!("{:?}", stream.next()); // [1]
/// assert_eq!(7, stream.byte_offset());
///
/// println!("{:?}", stream.next()); // error
/// assert_eq!(8, stream.byte_offset());
///
/// // If err.is_eof(), can join the remaining data to new data and continue.
/// let remaining = &data[stream.byte_offset()..];
/// ```
///
/// *Note:* In the future this method may be changed to return the number of
/// bytes so far deserialized into a successful T *or* syntactically valid
/// JSON skipped over due to a type error. See [serde-rs/json#70] for an
/// example illustrating this.
///
/// [serde-rs/json#70]: https://github.com/serde-rs/json/issues/70
pub fn byte_offset(&self) -> usize {
self.offset
}
fn peek_end_of_value(&mut self) -> Result<()> {
match tri!(self.de.peek()) {
Some(b' ' | b'\n' | b'\t' | b'\r' | b'"' | b'[' | b']' | b'{' | b'}' | b',' | b':')
| None => Ok(()),
Some(_) => {
let position = self.de.read.peek_position();
Err(Error::syntax(
ErrorCode::TrailingCharacters,
position.line,
position.column,
))
}
}
}
}
impl<'de, R, T> Iterator for StreamDeserializer<'de, R, T>
where
R: Read<'de>,
T: de::Deserialize<'de>,
{
type Item = Result<T>;
fn next(&mut self) -> Option<Result<T>> {
if R::should_early_return_if_failed && self.failed {
return None;
}
// skip whitespaces, if any
// this helps with trailing whitespaces, since whitespaces between
// values are handled for us.
match self.de.parse_whitespace() {
Ok(None) => {
self.offset = self.de.read.byte_offset();
None
}
Ok(Some(b)) => {
// If the value does not have a clear way to show the end of the value
// (like numbers, null, true etc.) we have to look for whitespace or
// the beginning of a self-delineated value.
let self_delineated_value = match b {
b'[' | b'"' | b'{' => true,
_ => false,
};
self.offset = self.de.read.byte_offset();
let result = de::Deserialize::deserialize(&mut self.de);
Some(match result {
Ok(value) => {
self.offset = self.de.read.byte_offset();
if self_delineated_value {
Ok(value)
} else {
self.peek_end_of_value().map(|()| value)
}
}
Err(e) => {
self.de.read.set_failed(&mut self.failed);
Err(e)
}
})
}
Err(e) => {
self.de.read.set_failed(&mut self.failed);
Some(Err(e))
}
}
}
}
impl<'de, R, T> FusedIterator for StreamDeserializer<'de, R, T>
where
R: Read<'de> + Fused,
T: de::Deserialize<'de>,
{
}
//////////////////////////////////////////////////////////////////////////////
fn from_trait<'de, R, T>(read: R) -> Result<T>
where
R: Read<'de>,
T: de::Deserialize<'de>,
{
let mut de = Deserializer::new(read);
let value = tri!(de::Deserialize::deserialize(&mut de));
// Make sure the whole stream has been consumed.
tri!(de.end());
Ok(value)
}
/// Deserialize an instance of type `T` from an I/O stream of JSON.
///
/// The content of the I/O stream is deserialized directly from the stream
/// without being buffered in memory by serde_json.
///
/// When reading from a source against which short reads are not efficient, such
/// as a [`File`], you will want to apply your own buffering because serde_json
/// will not buffer the input. See [`std::io::BufReader`].
///
/// It is expected that the input stream ends after the deserialized object.
/// If the stream does not end, such as in the case of a persistent socket connection,
/// this function will not return. It is possible instead to deserialize from a prefix of an input
/// stream without looking for EOF by managing your own [`Deserializer`].
///
/// Note that counter to intuition, this function is usually slower than
/// reading a file completely into memory and then applying [`from_str`]
/// or [`from_slice`] on it. See [issue #160].
///
/// [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html
/// [`std::io::BufReader`]: https://doc.rust-lang.org/std/io/struct.BufReader.html
/// [`from_str`]: ./fn.from_str.html
/// [`from_slice`]: ./fn.from_slice.html
/// [issue #160]: https://github.com/serde-rs/json/issues/160
///
/// # Example
///
/// Reading the contents of a file.
///
/// ```
/// use serde::Deserialize;
///
/// use std::error::Error;
/// use std::fs::File;
/// use std::io::BufReader;
/// use std::path::Path;
///
/// #[derive(Deserialize, Debug)]
/// struct User {
/// fingerprint: String,
/// location: String,
/// }
///
/// fn read_user_from_file<P: AsRef<Path>>(path: P) -> Result<User, Box<dyn Error>> {
/// // Open the file in read-only mode with buffer.
/// let file = File::open(path)?;
/// let reader = BufReader::new(file);
///
/// // Read the JSON contents of the file as an instance of `User`.
/// let u = serde_json::from_reader(reader)?;
///
/// // Return the `User`.
/// Ok(u)
/// }
///
/// fn main() {
/// # }
/// # fn fake_main() {
/// let u = read_user_from_file("test.json").unwrap();
/// println!("{:#?}", u);
/// }
/// ```
///
/// Reading from a persistent socket connection.
///
/// ```
/// use serde::Deserialize;
///
/// use std::error::Error;
/// use std::net::{TcpListener, TcpStream};
///
/// #[derive(Deserialize, Debug)]
/// struct User {
/// fingerprint: String,
/// location: String,
/// }
///
/// fn read_user_from_stream(tcp_stream: TcpStream) -> Result<User, Box<dyn Error>> {
/// let mut de = serde_json::Deserializer::from_reader(tcp_stream);
/// let u = User::deserialize(&mut de)?;
///
/// Ok(u)
/// }
///
/// fn main() {
/// # }
/// # fn fake_main() {
/// let listener = TcpListener::bind("127.0.0.1:4000").unwrap();
///
/// for stream in listener.incoming() {
/// println!("{:#?}", read_user_from_stream(stream.unwrap()));
/// }
/// }
/// ```
///
/// # Errors
///
/// This conversion can fail if the structure of the input does not match the
/// structure expected by `T`, for example if `T` is a struct type but the input
/// contains something other than a JSON map. It can also fail if the structure
/// is correct but `T`'s implementation of `Deserialize` decides that something
/// is wrong with the data, for example required struct fields are missing from
/// the JSON map or some number is too big to fit in the expected primitive
/// type.
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn from_reader<R, T>(rdr: R) -> Result<T>
where
R: crate::io::Read,
T: de::DeserializeOwned,
{
from_trait(read::IoRead::new(rdr))
}
/// Deserialize an instance of type `T` from bytes of JSON text.
///
/// # Example
///
/// ```
/// use serde::Deserialize;
///
/// #[derive(Deserialize, Debug)]
/// struct User {
/// fingerprint: String,
/// location: String,
/// }
///
/// fn main() {
/// // The type of `j` is `&[u8]`
/// let j = b"
/// {
/// \"fingerprint\": \"0xF9BA143B95FF6D82\",
/// \"location\": \"Menlo Park, CA\"
/// }";
///
/// let u: User = serde_json::from_slice(j).unwrap();
/// println!("{:#?}", u);
/// }
/// ```
///
/// # Errors
///
/// This conversion can fail if the structure of the input does not match the
/// structure expected by `T`, for example if `T` is a struct type but the input
/// contains something other than a JSON map. It can also fail if the structure
/// is correct but `T`'s implementation of `Deserialize` decides that something
/// is wrong with the data, for example required struct fields are missing from
/// the JSON map or some number is too big to fit in the expected primitive
/// type.
pub fn from_slice<'a, T>(v: &'a [u8]) -> Result<T>
where
T: de::Deserialize<'a>,
{
from_trait(read::SliceRead::new(v))
}
/// Deserialize an instance of type `T` from a string of JSON text.
///
/// # Example
///
/// ```
/// use serde::Deserialize;
///
/// #[derive(Deserialize, Debug)]
/// struct User {
/// fingerprint: String,
/// location: String,
/// }
///
/// fn main() {
/// // The type of `j` is `&str`
/// let j = "
/// {
/// \"fingerprint\": \"0xF9BA143B95FF6D82\",
/// \"location\": \"Menlo Park, CA\"
/// }";
///
/// let u: User = serde_json::from_str(j).unwrap();
/// println!("{:#?}", u);
/// }
/// ```
///
/// # Errors
///
/// This conversion can fail if the structure of the input does not match the
/// structure expected by `T`, for example if `T` is a struct type but the input
/// contains something other than a JSON map. It can also fail if the structure
/// is correct but `T`'s implementation of `Deserialize` decides that something
/// is wrong with the data, for example required struct fields are missing from
/// the JSON map or some number is too big to fit in the expected primitive
/// type.
pub fn from_str<'a, T>(s: &'a str) -> Result<T>
where
T: de::Deserialize<'a>,
{
from_trait(read::StrRead::new(s))
}