jsonx-lib 0.5.2

Strict RFC 8259 JSON: a borrowing parser, a pull reader, and a deterministic writer, with budgets checked before allocation and no recursion. No dependencies, no_std.
Documentation
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
//! Typed input: [`FromJson`], read straight from a [`Reader`].
//!
//! A `FromJson` type reads itself from the reader's events: no [`Value`] is
//! built, strings borrow from the input unless they had escapes, and a
//! mismatch is a [`DecodeError`] that says what was expected, what was
//! found, at which byte, and where in the document (a JSON Pointer path).

#[cfg(target_has_atomic = "ptr")]
use alloc::sync::Arc;
use alloc::{
    borrow::Cow,
    boxed::Box,
    collections::{BTreeMap, BTreeSet, VecDeque},
    format,
    rc::Rc,
    string::String,
    vec::Vec,
};
use core::fmt::{self, Write as _};

use crate::{
    Array, DuplicateKeys, Kind, Limits, Number, Numbers, Object, Parser, Reader, ToJson, Value,
    ValueWriter, Written,
    error::{Error, ErrorKind},
    reader::Event,
};

// ---- Errors ------------------------------------------------------------------

/// One step of the path to a value: an object member's key, or an array
/// item's index.
///
/// # Examples
///
/// ```
/// use jsonx_lib::PathSegment;
///
/// assert_eq!(PathSegment::Key("line".into()).to_string(), "line");
/// assert_eq!(PathSegment::Index(3).to_string(), "3");
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum PathSegment {
    /// The member with this key.
    Key(String),
    /// The item at this index.
    Index(usize),
}

impl fmt::Display for PathSegment {
    /// The segment as it reads in a path: the key, or the index in decimal.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Key(key) => f.write_str(key),
            Self::Index(index) => write!(f, "{index}"),
        }
    }
}

/// What went wrong reading a typed value; see [`DecodeError`].
///
/// The enum is `#[non_exhaustive]`: match with a wildcard arm.
///
/// # Examples
///
/// ```
/// use jsonx_lib::{DecodeErrorKind, Kind};
///
/// let err = jsonx_lib::from_str::<u8>("\"7\"").unwrap_err();
/// assert_eq!(err.kind(), &DecodeErrorKind::WrongType { expected: "u8", found: Kind::String });
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum DecodeErrorKind {
    /// The text is not valid JSON, or is over a budget: the [`ErrorKind`] a
    /// parse of the same text reports.
    Syntax(ErrorKind),
    /// The value is of the wrong kind: `found` where `expected` (a type
    /// name such as `"u32"`, or a description such as `"an object"`)
    /// belongs.
    WrongType {
        /// What the target type accepts.
        expected: &'static str,
        /// What the document has.
        found: Kind,
    },
    /// A number that does not fit the target type: out of its range, or not
    /// an integer for an integer type (`1.5` or `1e3` for a `u32`; integers
    /// are read from integer text only, as [`Number::as_i64`] does).
    OutOfRange {
        /// The target type.
        expected: &'static str,
    },
    /// A string with escape sequences was read into a borrowed `&str`, which
    /// can only point into the input. Read it into `String` or `Cow<str>`.
    StringNotBorrowable,
    /// An array of the wrong length for a fixed-size array or a tuple.
    WrongLength {
        /// The length the target needs.
        expected: usize,
        /// The length the document has.
        found: usize,
    },
    /// An object lacks a member its type requires. The offset is the
    /// object's opening brace.
    MissingField(&'static str),
    /// An object has a member its type does not know, refused with
    /// [`ObjectReader::unknown_field`]. The key is the last path segment.
    UnknownField,
    /// An object repeats a member, found by [`ObjectReader::field`] or a map
    /// under [`DuplicateKeys::Error`]. The key is the last path segment.
    DuplicateField,
    /// The value has the right kind but not an acceptable value; the text
    /// says what was expected (`"a single character"`, `"a URI"`).
    InvalidValue(&'static str),
    /// An error of the implementation's own, with its message.
    Custom(Cow<'static, str>),
}

impl fmt::Display for DecodeErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Syntax(kind) => write!(f, "{kind}"),
            Self::WrongType { expected, found } => write!(f, "expected {expected}, found {found}"),
            Self::OutOfRange { expected } => write!(f, "number out of range for {expected}"),
            Self::StringNotBorrowable => {
                f.write_str("a string with escapes cannot be borrowed as `&str`")
            }
            Self::WrongLength { expected, found } => {
                write!(f, "expected {expected} items, found {found}")
            }
            Self::MissingField(name) => write!(f, "missing field `{name}`"),
            Self::UnknownField => f.write_str("unknown field"),
            Self::DuplicateField => f.write_str("duplicate field"),
            Self::InvalidValue(expected) => write!(f, "invalid value: expected {expected}"),
            Self::Custom(message) => f.write_str(message),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct Inner {
    kind: DecodeErrorKind,
    offset: usize,
    /// Outermost first.
    path: Vec<PathSegment>,
}

/// Why a typed value could not be read: what went wrong, at which byte, and
/// where in the document.
///
/// The path is built as the error travels out through the containers it
/// was found in, so it costs nothing until something fails.
/// [`pointer`](Self::pointer) gives it as an RFC 6901 JSON Pointer, which is
/// what [`Display`](fmt::Display) shows after the offset:
/// `expected u32, found string at byte 31 (at /position/line)`. That is a
/// ready-made message for a JSON-RPC `-32602 Invalid params` reply.
///
/// The keys in the path come from the input, so the message shows them
/// made safe to log: a control character, a line or paragraph separator,
/// or a bidirectional formatting character is written as its Rust escape
/// (`\u{a}` for a newline), a backslash as `\\`, and a key longer than 64
/// characters is cut there and ends in `...`. The message is therefore
/// always one line, and its length is bounded by the depth of the path.
/// [`path`](Self::path) and [`pointer`](Self::pointer) keep every key exact.
///
/// The error is one pointer wide, so `Result<T, DecodeError>` stays small.
///
/// # Examples
///
/// ```
/// use jsonx_lib::{DecodeErrorKind, Kind, PathSegment};
///
/// let err = jsonx_lib::from_str::<Vec<Vec<u8>>>("[[1], [2, true]]").unwrap_err();
/// assert_eq!(err.kind(), &DecodeErrorKind::WrongType { expected: "u8", found: Kind::Bool });
/// assert_eq!(err.offset(), 10);
/// assert_eq!(err.path(), [PathSegment::Index(1), PathSegment::Index(1)]);
/// assert_eq!(err.pointer(), "/1/1");
/// assert_eq!(err.to_string(), "expected u8, found boolean at byte 10 (at /1/1)");
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[must_use]
pub struct DecodeError(Box<Inner>);

impl DecodeError {
    /// An error of `kind` at byte `offset`, with an empty path.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{DecodeError, DecodeErrorKind};
    ///
    /// let err = DecodeError::new(DecodeErrorKind::InvalidValue("a port"), 12);
    /// assert_eq!(err.to_string(), "invalid value: expected a port at byte 12");
    /// ```
    #[cold]
    pub fn new(kind: DecodeErrorKind, offset: usize) -> Self {
        Self(Box::new(Inner {
            kind,
            offset,
            path: Vec::new(),
        }))
    }

    /// A [`DecodeErrorKind::WrongType`] error.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{DecodeError, Kind};
    ///
    /// let err = DecodeError::wrong_type("a position", Kind::Array, 4);
    /// assert_eq!(err.to_string(), "expected a position, found array at byte 4");
    /// ```
    #[cold]
    pub fn wrong_type(expected: &'static str, found: Kind, offset: usize) -> Self {
        Self::new(DecodeErrorKind::WrongType { expected, found }, offset)
    }

    /// A [`DecodeErrorKind::InvalidValue`] error.
    ///
    /// # Examples
    ///
    /// ```
    /// let err = jsonx_lib::DecodeError::invalid_value("a URI", 0);
    /// assert_eq!(err.to_string(), "invalid value: expected a URI at byte 0");
    /// ```
    #[cold]
    pub fn invalid_value(expected: &'static str, offset: usize) -> Self {
        Self::new(DecodeErrorKind::InvalidValue(expected), offset)
    }

    /// A [`DecodeErrorKind::Custom`] error with `message`.
    ///
    /// # Examples
    ///
    /// ```
    /// let err = jsonx_lib::DecodeError::custom("end before start", 9);
    /// assert_eq!(err.to_string(), "end before start at byte 9");
    /// ```
    pub fn custom(message: impl Into<Cow<'static, str>>, offset: usize) -> Self {
        Self::new(DecodeErrorKind::Custom(message.into()), offset)
    }

    /// A [`DecodeErrorKind::MissingField`] error.
    ///
    /// # Examples
    ///
    /// ```
    /// let err = jsonx_lib::DecodeError::missing_field("uri", 0);
    /// assert_eq!(err.to_string(), "missing field `uri` at byte 0");
    /// ```
    #[cold]
    pub fn missing_field(name: &'static str, offset: usize) -> Self {
        Self::new(DecodeErrorKind::MissingField(name), offset)
    }

    /// The error as seen from the object that holds it under `key`: the key
    /// goes in front of the path. The object readers do this for you.
    ///
    /// # Examples
    ///
    /// ```
    /// let err = jsonx_lib::DecodeError::invalid_value("a URI", 7).at_key("uri").at_key("textDocument");
    /// assert_eq!(err.pointer(), "/textDocument/uri");
    /// ```
    #[cold]
    pub fn at_key(mut self, key: impl Into<String>) -> Self {
        self.0.path.insert(0, PathSegment::Key(key.into()));
        self
    }

    /// The error as seen from the array that holds it at `index`.
    ///
    /// # Examples
    ///
    /// ```
    /// let err = jsonx_lib::DecodeError::invalid_value("a URI", 7).at_index(2);
    /// assert_eq!(err.pointer(), "/2");
    /// ```
    #[cold]
    pub fn at_index(mut self, index: usize) -> Self {
        self.0.path.insert(0, PathSegment::Index(index));
        self
    }

    /// What went wrong.
    #[must_use]
    pub fn kind(&self) -> &DecodeErrorKind {
        &self.0.kind
    }

    /// The byte offset in the input where it went wrong: the value that did
    /// not fit, or for a syntax error the byte a parse reports.
    #[must_use]
    pub fn offset(&self) -> usize {
        self.0.offset
    }

    /// Where in the document, outermost first; empty for the top-level
    /// value.
    #[must_use]
    pub fn path(&self) -> &[PathSegment] {
        &self.0.path
    }

    /// The path as an [RFC 6901](https://www.rfc-editor.org/rfc/rfc6901)
    /// JSON Pointer (`/params/items/3`, `""` for the top level), usable with
    /// [`Value::pointer`].
    ///
    /// # Examples
    ///
    /// ```
    /// let err = jsonx_lib::DecodeError::invalid_value("x", 0).at_key("a/b").at_key("~");
    /// assert_eq!(err.pointer(), "/~0/a~1b");
    /// ```
    #[must_use]
    pub fn pointer(&self) -> String {
        let mut out = String::new();
        for segment in &self.0.path {
            out.push('/');
            match segment {
                PathSegment::Key(key) => {
                    for ch in key.chars() {
                        match ch {
                            '~' => out.push_str("~0"),
                            '/' => out.push_str("~1"),
                            _ => out.push(ch),
                        }
                    }
                }
                PathSegment::Index(index) => out.push_str(&format!("{index}")),
            }
        }
        out
    }

    /// The error's line and column in `input`, from 1, the column in
    /// characters; see [`Error::line_col`].
    ///
    /// # Examples
    ///
    /// ```
    /// let text = "{\n  \"n\": \"seven\"\n}";
    /// let err = jsonx_lib::from_str::<std::collections::BTreeMap<String, u8>>(text).unwrap_err();
    /// assert_eq!(err.line_col(text), (2, 8));
    /// ```
    #[must_use]
    pub fn line_col(&self, input: impl AsRef<[u8]>) -> (usize, usize) {
        let at = crate::error::locate(input.as_ref(), self.0.offset);
        (at.line + 1, at.chars + 1)
    }

    /// The error's position in `input` as LSP counts it (from 0, the
    /// character in UTF-16 units); see [`Error::utf16_position`].
    ///
    /// # Examples
    ///
    /// ```
    /// let text = "[1,\n \"x\"]";
    /// let err = jsonx_lib::from_str::<Vec<u8>>(text).unwrap_err();
    /// assert_eq!(err.utf16_position(text), (1, 1));
    /// ```
    #[must_use]
    pub fn utf16_position(&self, input: impl AsRef<[u8]>) -> (usize, usize) {
        let at = crate::error::locate(input.as_ref(), self.0.offset);
        (at.line, at.utf16)
    }

    /// The parse error, for a [`DecodeErrorKind::Syntax`] error.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::ErrorKind;
    ///
    /// let err = jsonx_lib::from_str::<Vec<u8>>("[1,]").unwrap_err();
    /// assert_eq!(err.syntax_error().map(|e| e.kind()), Some(ErrorKind::TrailingComma));
    /// ```
    #[must_use]
    pub fn syntax_error(&self) -> Option<Error> {
        match self.0.kind {
            DecodeErrorKind::Syntax(kind) => Some(Error::new(kind, self.0.offset)),
            _ => None,
        }
    }

    /// The same error `by` bytes further into a larger input.
    pub(crate) fn shifted(mut self, by: usize) -> Self {
        self.0.offset += by;
        self
    }
}

impl From<Error> for DecodeError {
    #[cold]
    fn from(e: Error) -> Self {
        Self::new(DecodeErrorKind::Syntax(e.kind()), e.offset())
    }
}

impl fmt::Display for DecodeError {
    /// The message, with the path's keys made safe to log (see
    /// [`DecodeError`]).
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} at byte {}", self.0.kind, self.0.offset)?;
        if !self.0.path.is_empty() {
            f.write_str(" (at ")?;
            for segment in &self.0.path {
                f.write_char('/')?;
                match segment {
                    PathSegment::Key(key) => write_message_key(f, key)?,
                    PathSegment::Index(index) => write!(f, "{index}")?,
                }
            }
            f.write_char(')')?;
        }
        Ok(())
    }
}

/// The most characters of one key that a [`DecodeError`] message shows.
const MESSAGE_KEY_CHARS: usize = 64;

/// Writes `key` as a segment of the pointer in a [`DecodeError`] message:
/// escaped as RFC 6901 escapes it, with every character that could break a
/// log line or hide text escaped too, and cut after
/// [`MESSAGE_KEY_CHARS`] characters.
fn write_message_key(f: &mut fmt::Formatter<'_>, key: &str) -> fmt::Result {
    for (count, ch) in key.chars().enumerate() {
        if count == MESSAGE_KEY_CHARS {
            return f.write_str("...");
        }
        match ch {
            '~' => f.write_str("~0")?,
            '/' => f.write_str("~1")?,
            '\\' => f.write_str("\\\\")?,
            _ if hides_text(ch) => write!(f, "{}", ch.escape_unicode())?,
            _ => f.write_char(ch)?,
        }
    }
    Ok(())
}

/// Whether `ch` can break a line of text or hide part of it when shown:
/// control characters, the line and paragraph separators, and the
/// bidirectional formatting characters.
fn hides_text(ch: char) -> bool {
    ch.is_control()
        || matches!(
            ch,
            '\u{2028}'
                | '\u{2029}'
                | '\u{200e}'
                | '\u{200f}'
                | '\u{202a}'..='\u{202e}'
                | '\u{2066}'..='\u{2069}'
        )
}

impl core::error::Error for DecodeError {}

// ---- The traits --------------------------------------------------------------

/// A type that reads itself from JSON, straight from a [`Reader`].
///
/// The explicit, hand-written counterpart of [`ToJson`]: no derive and no
/// reflection, so an implementation says in code which members a type has
/// and how each is read. Nothing is built in between, and the lifetime
/// `'de` is the input's, so a type can borrow from it: a `&'de str` or
/// `Cow<'de, str>` field points into the text when the string had no
/// escapes.
///
/// # Implementing it
///
/// `from_json` is called with the reader positioned where a value comes
/// next, and must read exactly that one value. Read a scalar with
/// [`reader.decode()`](Reader::decode); an object with
/// [`read_object`](Reader::read_object), which hands out its members; an
/// array with [`read_array`](Reader::read_array). For a struct:
///
/// ```
/// use std::borrow::Cow;
/// use jsonx_lib::{DecodeError, FromJson, Reader};
///
/// struct Position { line: u32, character: u32 }
///
/// impl<'de> FromJson<'de> for Position {
///     fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
///         let (mut line, mut character) = (None, None);
///         let mut object = reader.read_object()?;
///         while let Some(key) = object.next_key()? {
///             match &*key {
///                 "line" => object.field(&mut line)?,
///                 "character" => object.field(&mut character)?,
///                 _ => {} // unknown members are skipped (and still validated)
///             }
///         }
///         Ok(Position {
///             line: object.required(line, "line")?,
///             character: object.required(character, "character")?,
///         })
///     }
/// }
///
/// struct Location<'a> { uri: Cow<'a, str>, position: Position, label: Option<String> }
///
/// impl<'de> FromJson<'de> for Location<'de> {
///     fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
///         let (mut uri, mut position, mut label) = (None, None, None);
///         let mut object = reader.read_object()?;
///         while let Some(key) = object.next_key()? {
///             match &*key {
///                 "uri" => object.field(&mut uri)?,
///                 "position" => object.field(&mut position)?,
///                 "label" => object.field(&mut label)?,
///                 _ => {}
///             }
///         }
///         Ok(Location {
///             uri: object.required(uri, "uri")?,
///             position: object.required(position, "position")?,
///             label: object.required(label, "label")?, // absent reads as None
///         })
///     }
/// }
///
/// let text = r#"{"uri": "file:///a.rs", "position": {"line": 3, "character": 14}}"#;
/// let loc: Location = jsonx_lib::from_str(text)?;
/// assert!(matches!(loc.uri, Cow::Borrowed("file:///a.rs"))); // no copy
/// assert_eq!((loc.position.line, loc.position.character), (3, 14));
/// assert_eq!(loc.label, None);
///
/// let err = jsonx_lib::from_str::<Location>(r#"{"uri": "x", "position": {"line": "3"}}"#).err();
/// assert_eq!(
///     err.map(|e| e.to_string()).as_deref(),
///     Some("expected u32, found string at byte 34 (at /position/line)")
/// );
/// # Ok::<(), DecodeError>(())
/// ```
///
/// What the object reader does for you:
///
/// - **Duplicates are refused.** [`field`](ObjectReader::field) refuses a
///   second value for a field it already has
///   ([`DecodeErrorKind::DuplicateField`]), and under the default
///   [`DuplicateKeys::Error`] the reader refuses any repeated key, known or
///   not, when the object closes. A second value can never be smuggled
///   past the first.
/// - **Unknown members are skipped**, after being validated, which is what
///   LSP and MCP ask of receivers. To refuse them, return
///   [`unknown_field`](ObjectReader::unknown_field) for the keys you do not
///   know.
/// - **Absent members:** [`required`](ObjectReader::required) turns a field
///   never seen into [`DecodeErrorKind::MissingField`], except for a type
///   whose [`absent`](Self::absent) gives a value: `Option<T>` (`None`) and
///   [`Nullable<T>`] (`Absent`).
/// - **Paths:** an error inside a member's value gets the member's key in
///   its path; an error inside an array item gets the index.
///
/// An enum whose variants have different shapes looks before it reads,
/// with [`peek_kind`](Reader::peek_kind):
///
/// ```
/// use jsonx_lib::{DecodeError, FromJson, Kind, Reader};
///
/// enum Id { Number(i64), Text(String) }
///
/// impl<'de> FromJson<'de> for Id {
///     fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
///         match reader.peek_kind()? {
///             Some(Kind::Number) => Ok(Id::Number(reader.decode()?)),
///             Some(Kind::String) => Ok(Id::Text(reader.decode()?)),
///             Some(found) => Err(DecodeError::wrong_type("a number or a string", found, reader.offset())),
///             None => Err(reader.decode::<()>().unwrap_err()), // let the reader say what is wrong
///         }
///     }
/// }
///
/// assert!(matches!(jsonx_lib::from_str::<Id>("7")?, Id::Number(7)));
/// assert!(matches!(jsonx_lib::from_str::<Id>("\"a\"")?, Id::Text(s) if s == "a"));
/// assert!(jsonx_lib::from_str::<Id>("true").is_err());
/// # Ok::<(), DecodeError>(())
/// ```
///
/// # Depth
///
/// A type that nests as deep as its data (a tree of its own nodes) recurses
/// once per level. The reader's [`Limits::max_depth`] (128 by default)
/// refuses anything deeper before `from_json` is called for it, so the
/// recursion is bounded by the budget, not by the input.
///
/// # Provided implementations
///
/// `bool`; every integer type (from integer text in range); `f32`, `f64`
/// (any finite number); `String`, `Box<str>`, `Cow<'de, str>`, `&'de str`
/// (only for strings without escapes), `char` (a one-character string);
/// `()` (`null`); `Option<T>` (`null` or absent is `None`), [`Nullable<T>`];
/// `Box<T>`, `Rc<T>`, `Arc<T>`; `Vec<T>`, `VecDeque<T>`, `Box<[T]>`,
/// `[T; N]`, `BTreeSet<T>` and with `std` `HashSet<T, S>` (arrays; a set
/// keeps one of equal items); tuples of up to eight (arrays of that
/// length); `BTreeMap<K, V>` and with `std` `HashMap<K, V, S>` (objects,
/// keys through [`FromJsonKey`], duplicates per the reader's
/// [`DuplicateKeys`] policy); and this crate's [`Value`], [`Number`],
/// [`Array`], [`Object`], and [`RawValue`](crate::RawValue).
pub trait FromJson<'de>: Sized {
    /// Reads one value from `reader`, which is positioned where a value
    /// comes next.
    ///
    /// # Errors
    ///
    /// A [`DecodeError`] for a value that is not of this type's shape, or
    /// any error of the reader.
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError>;

    /// The value for an object member that is absent, if absence is
    /// allowed: `None` (absence is an error) by default, `Some(None)` for
    /// `Option<T>`, and `Some(Nullable::Absent)` for [`Nullable<T>`].
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::FromJson;
    ///
    /// assert_eq!(<Option<u8> as FromJson>::absent(), Some(None));
    /// assert_eq!(<u8 as FromJson>::absent(), None);
    /// ```
    #[inline]
    fn absent() -> Option<Self> {
        None
    }
}

/// A type usable as a JSON object key, for [`FromJson`] on maps.
///
/// # Examples
///
/// ```
/// use std::{borrow::Cow, collections::BTreeMap};
/// use jsonx_lib::{DecodeErrorKind, FromJsonKey};
///
/// #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
/// struct Lang(String);
///
/// impl<'de> FromJsonKey<'de> for Lang {
///     fn from_json_key(key: Cow<'de, str>) -> Result<Self, DecodeErrorKind> {
///         if key.len() == 2 { Ok(Lang(key.into_owned())) } else { Err(DecodeErrorKind::InvalidValue("a language code")) }
///     }
/// }
///
/// let names: BTreeMap<Lang, String> = jsonx_lib::from_str(r#"{"en": "hello"}"#)?;
/// assert_eq!(names[&Lang("en".into())], "hello");
/// let err = jsonx_lib::from_str::<BTreeMap<Lang, String>>(r#"{"eng": "x"}"#).unwrap_err();
/// assert_eq!(err.pointer(), "/eng");
/// # Ok::<(), jsonx_lib::DecodeError>(())
/// ```
pub trait FromJsonKey<'de>: Sized {
    /// Makes the key from its decoded text.
    ///
    /// # Errors
    ///
    /// The reason the text is not a valid key; the map reports it at the
    /// key, with the key in the path.
    fn from_json_key(key: Cow<'de, str>) -> Result<Self, DecodeErrorKind>;
}

// ---- Reading through the reader ----------------------------------------------

/// Reads one value as `T`, then completes any container `T` left open, so
/// an implementation that stops early (returning once it has what it
/// wants) cannot leave the reader in the middle of a value.
pub(crate) fn decode_value<'de, T: FromJson<'de>>(
    reader: &mut Reader<'de>,
) -> Result<T, DecodeError> {
    let depth = reader.depth();
    let value = T::from_json(reader)?;
    while reader.depth() > depth {
        if reader.next_event()?.is_none() {
            break;
        }
    }
    Ok(value)
}

impl<'a> Reader<'a> {
    /// Reads the next value as a `T` ([`FromJson`]).
    ///
    /// # Errors
    ///
    /// The [`DecodeError`] of `T`, or of the reader.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{Event, Reader};
    ///
    /// let mut r = Reader::new(r#"{"id": 7, "tags": ["a", "b"]}"#);
    /// r.next_event()?; // {
    /// r.next_event()?; // "id"
    /// assert_eq!(r.decode::<u64>()?, 7);
    /// r.next_event()?; // "tags"
    /// assert_eq!(r.decode::<Vec<&str>>()?, ["a", "b"]);
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    pub fn decode<T: FromJson<'a>>(&mut self) -> Result<T, DecodeError> {
        self.begin_read();
        let value = decode_value(self);
        self.end_read();
        value
    }

    /// Charges `bytes` of heap that a typed read is about to hold to the
    /// current top-level read's
    /// [`Limits::max_memory`](crate::Limits::max_memory) count.
    #[inline]
    fn hold(&mut self, bytes: usize) -> Result<(), DecodeError> {
        let at = self.offset();
        self.charge(bytes, at).map_err(DecodeError::from)
    }

    /// Starts reading an object whose members are read one by one through
    /// the returned [`ObjectReader`]. Consumes the `{`.
    ///
    /// # Errors
    ///
    /// [`DecodeErrorKind::WrongType`] if the next value is not an object
    /// (nothing is consumed), or an error of the reader.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut r = jsonx_lib::Reader::new(r#"{"a": 1, "b": [2]}"#);
    /// let mut object = r.read_object()?;
    /// let mut keys = Vec::new();
    /// while let Some(key) = object.next_key()? {
    ///     keys.push(key.into_owned()); // values not read are skipped
    /// }
    /// assert_eq!(keys, ["a", "b"]);
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    pub fn read_object(&mut self) -> Result<ObjectReader<'_, 'a>, DecodeError> {
        let start = self.open(Kind::Object, "an object")?;
        Ok(ObjectReader {
            reader: self,
            start,
            key_offset: start,
            pending: false,
            done: false,
        })
    }

    /// Starts reading an array whose items are read one by one through the
    /// returned [`ArrayReader`]. Consumes the `[`.
    ///
    /// # Errors
    ///
    /// [`DecodeErrorKind::WrongType`] if the next value is not an array, or
    /// an error of the reader.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut r = jsonx_lib::Reader::new("[1, 2, 3]");
    /// let mut items = r.read_array()?;
    /// let mut sum = 0u32;
    /// while let Some(n) = items.next_item::<u32>()? {
    ///     sum += n;
    /// }
    /// assert_eq!(sum, 6);
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    pub fn read_array(&mut self) -> Result<ArrayReader<'_, 'a>, DecodeError> {
        let start = self.open(Kind::Array, "an array")?;
        Ok(ArrayReader {
            reader: self,
            start,
            index: 0,
            done: false,
        })
    }

    /// Reads the next value as a [`RawValue`](crate::RawValue): its exact
    /// source text, validated by this read and not scanned again. As
    /// [`read_raw`](Self::read_raw), `Ok(None)` when no value comes next.
    ///
    /// # Errors
    ///
    /// As [`read_raw`](Self::read_raw).
    ///
    /// # Examples
    ///
    /// ```
    /// let mut r = jsonx_lib::Reader::new(r#"{"params": {"line": 3}}"#);
    /// r.next_event()?;
    /// r.next_event()?;
    /// let params = r.read_raw_value()?.unwrap();
    /// assert_eq!(params.as_str(), r#"{"line": 3}"#);
    /// # Ok::<(), jsonx_lib::Error>(())
    /// ```
    pub fn read_raw_value(&mut self) -> Result<Option<crate::RawValue<'a>>, Error> {
        Ok(self.read_raw()?.map(crate::RawValue::trusted))
    }

    /// Checks that the next value is of `kind` and consumes its first event;
    /// returns its offset.
    fn open(&mut self, kind: Kind, expected: &'static str) -> Result<usize, DecodeError> {
        if self.enter(kind == Kind::Object)? {
            return Ok(self.event_offset());
        }
        // Looked at before it is read: a value of another type is left
        // unread.
        match self.peek_kind()? {
            Some(found) if found == kind => {}
            Some(found) => return Err(DecodeError::wrong_type(expected, found, self.offset())),
            None if !self.at_a_value()? => {
                // A container ends, or the document did: there is no value.
                return Err(DecodeError::new(
                    DecodeErrorKind::Syntax(ErrorKind::ExpectedValue),
                    self.offset(),
                ));
            }
            // A byte that cannot start a value: the step below says why.
            None => {}
        }
        match self.next_event()? {
            Some(Event::StartObject | Event::StartArray) => Ok(self.event_offset()),
            Some(other) => Err(DecodeError::wrong_type(
                expected,
                event_kind(&other),
                self.event_offset(),
            )),
            None => Err(Error::new(ErrorKind::UnexpectedEnd, self.offset()).into()),
        }
    }

    /// Reads a scalar value's event for a target described by `expected`:
    /// one step of the reader. A container is the wrong type (its first
    /// event is consumed, which does not matter: the read has failed); no
    /// value at all is [`ErrorKind::ExpectedValue`] (or the reader's own
    /// error, such as a trailing comma).
    #[inline]
    fn scalar(&mut self, expected: &'static str) -> Result<(Event<'a>, usize), DecodeError> {
        match self.next_event() {
            Ok(Some(
                event @ (Event::String(_) | Event::Number(_) | Event::Bool(_) | Event::Null),
            )) => Ok((event, self.event_offset())),
            other => Err(self.not_a_scalar(other, expected)),
        }
    }

    /// The error of a [`scalar`](Self::scalar) read that found no scalar.
    #[cold]
    #[inline(never)]
    fn not_a_scalar(
        &self,
        read: Result<Option<Event<'a>>, Error>,
        expected: &'static str,
    ) -> DecodeError {
        match read {
            Err(e) => e.into(),
            Ok(Some(event @ (Event::StartArray | Event::StartObject))) => {
                DecodeError::wrong_type(expected, event_kind(&event), self.event_offset())
            }
            Ok(Some(_)) => DecodeError::new(
                DecodeErrorKind::Syntax(ErrorKind::ExpectedValue),
                self.event_offset(),
            ),
            Ok(None) => DecodeError::new(
                DecodeErrorKind::Syntax(ErrorKind::ExpectedValue),
                self.offset(),
            ),
        }
    }
}

/// The kind of value an event starts.
fn event_kind(event: &Event<'_>) -> Kind {
    match event {
        Event::StartObject | Event::EndObject | Event::Key(_) => Kind::Object,
        Event::StartArray | Event::EndArray => Kind::Array,
        Event::String(_) => Kind::String,
        Event::Number(_) => Kind::Number,
        Event::Bool(_) => Kind::Bool,
        Event::Null => Kind::Null,
    }
}

/// Reads the members of an object one by one; from
/// [`Reader::read_object`].
///
/// Call [`next_key`](Self::next_key) until it returns `None`; after each
/// key, read its value with [`field`](Self::field), [`value`](Self::value),
/// [`value_with`](Self::value_with), [`raw`](Self::raw), or
/// [`skip`](Self::skip), or do nothing and the next `next_key` skips it.
/// Errors in a value get the member's key in their path. See [`FromJson`]
/// for the whole pattern.
pub struct ObjectReader<'r, 'de> {
    reader: &'r mut Reader<'de>,
    start: usize,
    /// The current key's opening quote (its text is read again only for an
    /// error's path).
    key_offset: usize,
    /// A key was read and its value not yet.
    pending: bool,
    done: bool,
}

impl<'de> ObjectReader<'_, 'de> {
    /// The next member's key (borrowed from the input unless it had
    /// escapes), or `None` after the last member, when the object's `}` has
    /// been read. A value left unread is skipped first (and validated).
    ///
    /// # Errors
    ///
    /// Any error of the reader, including a repeated key under
    /// [`DuplicateKeys::Error`] (reported when the object closes).
    ///
    /// # Examples
    ///
    /// ```
    /// let mut r = jsonx_lib::Reader::new(r#"{"a": [1, 2], "b": 3}"#);
    /// let mut o = r.read_object()?;
    /// assert_eq!(o.next_key()?.as_deref(), Some("a"));
    /// assert_eq!(o.next_key()?.as_deref(), Some("b")); // `[1, 2]` skipped
    /// assert_eq!(o.value::<u8>()?, 3);
    /// assert_eq!(o.next_key()?, None);
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    pub fn next_key(&mut self) -> Result<Option<Cow<'de, str>>, DecodeError> {
        if self.done {
            return Ok(None);
        }
        if self.pending {
            self.skip()?;
        }
        match self.reader.next_event()? {
            Some(Event::Key(key)) => {
                self.key_offset = self.reader.event_offset();
                self.pending = true;
                Ok(Some(key))
            }
            Some(Event::EndObject) => {
                self.done = true;
                Ok(None)
            }
            _ => Err(DecodeError::new(
                DecodeErrorKind::Syntax(ErrorKind::ExpectedKey),
                self.reader.event_offset(),
            )),
        }
    }

    /// Reads the current member's value as a `T`.
    ///
    /// # Errors
    ///
    /// The [`DecodeError`] of `T`, with this member's key in its path.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut r = jsonx_lib::Reader::new(r#"{"n": "x"}"#);
    /// let mut o = r.read_object()?;
    /// o.next_key()?;
    /// assert_eq!(o.value::<u8>().unwrap_err().pointer(), "/n");
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    #[inline]
    pub fn value<T: FromJson<'de>>(&mut self) -> Result<T, DecodeError> {
        self.value_with(T::from_json)
    }

    /// Reads the current member's value with `read`, for a value that needs
    /// more than a type (a choice of shapes, a value read in place). Errors
    /// get the key in their path.
    ///
    /// # Errors
    ///
    /// Whatever `read` returns, with this member's key in its path.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut r = jsonx_lib::Reader::new(r#"{"n": [1, 2, 3]}"#);
    /// let mut o = r.read_object()?;
    /// o.next_key()?;
    /// let count = o.value_with(|r| {
    ///     let mut items = r.read_array()?;
    ///     let mut count = 0;
    ///     while items.skip()? {
    ///         count += 1;
    ///     }
    ///     Ok(count)
    /// })?;
    /// assert_eq!(count, 3);
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    pub fn value_with<T>(
        &mut self,
        read: impl FnOnce(&mut Reader<'de>) -> Result<T, DecodeError>,
    ) -> Result<T, DecodeError> {
        self.pending = false;
        let depth = self.reader.depth();
        let result = read(self.reader).and_then(|value| {
            // Finish a container `read` left open.
            while self.reader.depth() > depth {
                if self.reader.next_event()?.is_none() {
                    break;
                }
            }
            Ok(value)
        });
        result.map_err(|e| self.in_member(e))
    }

    /// `e` with the current member's key in its path.
    #[cold]
    #[inline(never)]
    fn in_member(&self, e: DecodeError) -> DecodeError {
        e.at_key(self.reader.key_at(self.key_offset))
    }

    /// Reads the current member's value into `slot`, the pattern for a
    /// struct field: refused as [`DecodeErrorKind::DuplicateField`] (at the
    /// key) if `slot` already holds a value, so a second value for a field
    /// cannot override the first. Under [`DuplicateKeys::First`] the second
    /// value is skipped instead, under `Last` and `KeepAll` it replaces the
    /// first.
    ///
    /// # Errors
    ///
    /// [`DecodeErrorKind::DuplicateField`], or the [`DecodeError`] of `T`,
    /// with this member's key in the path.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::DecodeErrorKind;
    ///
    /// let mut r = jsonx_lib::Reader::new(r#"{"id": 1, "id": 2}"#);
    /// let mut o = r.read_object()?;
    /// let mut id: Option<u8> = None;
    /// o.next_key()?;
    /// o.field(&mut id)?;
    /// o.next_key()?;
    /// let err = o.field(&mut id).unwrap_err();
    /// assert_eq!((err.kind(), err.offset(), err.pointer()), (&DecodeErrorKind::DuplicateField, 10, "/id".into()));
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    pub fn field<T: FromJson<'de>>(&mut self, slot: &mut Option<T>) -> Result<(), DecodeError> {
        if slot.is_some() {
            match self.reader.duplicate_policy() {
                DuplicateKeys::First => return self.skip(),
                DuplicateKeys::Last | DuplicateKeys::KeepAll => {}
                _ => {
                    let e = DecodeError::new(DecodeErrorKind::DuplicateField, self.key_offset);
                    return Err(self.in_member(e));
                }
            }
        }
        *slot = Some(self.value()?);
        Ok(())
    }

    /// Skips the current member's value, validating it.
    ///
    /// # Errors
    ///
    /// Any error of the reader inside the value.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut r = jsonx_lib::Reader::new(r#"{"big": [[1], {}], "n": 1}"#);
    /// let mut o = r.read_object()?;
    /// o.next_key()?;
    /// o.skip()?;
    /// assert_eq!(o.next_key()?.as_deref(), Some("n"));
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    pub fn skip(&mut self) -> Result<(), DecodeError> {
        self.raw().map(|_| ())
    }

    /// The current member's value as its exact source text, validated.
    ///
    /// # Errors
    ///
    /// Any error of the reader inside the value.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut r = jsonx_lib::Reader::new(r#"{"params": {"line": 3}}"#);
    /// let mut o = r.read_object()?;
    /// o.next_key()?;
    /// assert_eq!(o.raw()?, r#"{"line": 3}"#);
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    pub fn raw(&mut self) -> Result<&'de str, DecodeError> {
        self.pending = false;
        match self.reader.read_raw() {
            Ok(Some(text)) => Ok(text),
            Ok(None) => {
                let e = DecodeError::new(
                    DecodeErrorKind::Syntax(ErrorKind::ExpectedValue),
                    self.reader.offset(),
                );
                Err(self.in_member(e))
            }
            Err(e) => Err(self.in_member(e.into())),
        }
    }

    /// The error for the current member being one this type does not know:
    /// [`DecodeErrorKind::UnknownField`] at its key, with the key in the
    /// path. For a type that refuses unknown members.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{DecodeError, FromJson, Reader};
    ///
    /// struct Strict { a: u8 }
    ///
    /// impl<'de> FromJson<'de> for Strict {
    ///     fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
    ///         let mut a = None;
    ///         let mut o = reader.read_object()?;
    ///         while let Some(key) = o.next_key()? {
    ///             match &*key {
    ///                 "a" => o.field(&mut a)?,
    ///                 _ => return Err(o.unknown_field()),
    ///             }
    ///         }
    ///         Ok(Strict { a: o.required(a, "a")? })
    ///     }
    /// }
    ///
    /// let err = jsonx_lib::from_str::<Strict>(r#"{"a": 1, "b": 2}"#).err();
    /// assert_eq!(err.map(|e| e.to_string()).as_deref(), Some("unknown field at byte 9 (at /b)"));
    /// ```
    pub fn unknown_field(&self) -> DecodeError {
        self.in_member(DecodeError::new(
            DecodeErrorKind::UnknownField,
            self.key_offset,
        ))
    }

    /// The value of a field read with [`field`](Self::field): `slot`'s
    /// value, or if the field was absent `T`'s [`absent`](FromJson::absent)
    /// value (`None` for an `Option`), or else
    /// [`DecodeErrorKind::MissingField`] at the object's `{`.
    ///
    /// # Errors
    ///
    /// [`DecodeErrorKind::MissingField`].
    ///
    /// # Examples
    ///
    /// ```
    /// let mut r = jsonx_lib::Reader::new("{}");
    /// let mut o = r.read_object()?;
    /// assert_eq!(o.next_key()?, None);
    /// assert_eq!(o.required::<Option<u8>>(None, "maybe")?, None);
    /// assert_eq!(o.required::<u8>(None, "must").unwrap_err().to_string(), "missing field `must` at byte 0");
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    pub fn required<T: FromJson<'de>>(
        &self,
        slot: Option<T>,
        name: &'static str,
    ) -> Result<T, DecodeError> {
        slot.or_else(T::absent)
            .ok_or_else(|| DecodeError::missing_field(name, self.start))
    }

    /// The byte offset of the object's `{`.
    #[must_use]
    pub fn offset(&self) -> usize {
        self.start
    }
}

/// Reads the items of an array one by one; from [`Reader::read_array`].
///
/// Errors in an item get its index in their path.
pub struct ArrayReader<'r, 'de> {
    reader: &'r mut Reader<'de>,
    start: usize,
    index: usize,
    done: bool,
}

impl<'de> ArrayReader<'_, 'de> {
    /// Whether another item comes; consumes the `]` when not.
    #[inline(always)]
    fn more(&mut self) -> Result<bool, DecodeError> {
        if self.done {
            return Ok(false);
        }
        if self.reader.at_item()? {
            return Ok(true);
        }
        match self.reader.next_event()? {
            Some(Event::EndArray) => {
                self.done = true;
                Ok(false)
            }
            _ => Err(DecodeError::new(
                DecodeErrorKind::Syntax(ErrorKind::ExpectedCommaOrBracket),
                self.reader.event_offset(),
            )),
        }
    }

    /// Reads the next item as a `T`, or `None` after the last one, when the
    /// array's `]` has been read.
    ///
    /// # Errors
    ///
    /// The [`DecodeError`] of `T`, with the item's index in its path.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut r = jsonx_lib::Reader::new(r#"[1, "x"]"#);
    /// let mut items = r.read_array()?;
    /// assert_eq!(items.next_item::<u8>()?, Some(1));
    /// assert_eq!(items.next_item::<u8>().unwrap_err().pointer(), "/1");
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    #[inline]
    pub fn next_item<T: FromJson<'de>>(&mut self) -> Result<Option<T>, DecodeError> {
        self.next_with(T::from_json)
    }

    /// Reads the next item with `read`, or returns `None` after the last.
    ///
    /// # Errors
    ///
    /// Whatever `read` returns, with the item's index in its path.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut r = jsonx_lib::Reader::new("[[1, 2], [3]]");
    /// let mut rows = r.read_array()?;
    /// let mut lengths = Vec::new();
    /// while let Some(n) = rows.next_with(|r| Ok(r.decode::<Vec<u8>>()?.len()))? {
    ///     lengths.push(n);
    /// }
    /// assert_eq!(lengths, [2, 1]);
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    pub fn next_with<T>(
        &mut self,
        read: impl FnOnce(&mut Reader<'de>) -> Result<T, DecodeError>,
    ) -> Result<Option<T>, DecodeError> {
        if !self.more()? {
            return Ok(None);
        }
        let index = self.index;
        self.index += 1;
        let depth = self.reader.depth();
        let result = read(self.reader).and_then(|value| {
            while self.reader.depth() > depth {
                if self.reader.next_event()?.is_none() {
                    break;
                }
            }
            Ok(value)
        });
        result.map(Some).map_err(|e| e.at_index(index))
    }

    /// Skips the next item, validating it; `false` after the last.
    ///
    /// # Errors
    ///
    /// Any error of the reader inside the item.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut r = jsonx_lib::Reader::new("[{}, []]");
    /// let mut items = r.read_array()?;
    /// assert!(items.skip()? && items.skip()?);
    /// assert!(!items.skip()?);
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    pub fn skip(&mut self) -> Result<bool, DecodeError> {
        let index = self.index;
        if !self.more()? {
            return Ok(false);
        }
        self.index += 1;
        match self.reader.read_raw() {
            Ok(_) => Ok(true),
            Err(e) => Err(DecodeError::from(e).at_index(index)),
        }
    }

    /// Checks that no item is left: for a fixed-length array or a tuple,
    /// after its items were read.
    ///
    /// # Errors
    ///
    /// [`DecodeErrorKind::WrongLength`] at the `[` if items remain (all of
    /// them are skipped and counted), or an error of the reader.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::DecodeErrorKind;
    ///
    /// let mut r = jsonx_lib::Reader::new("[1, 2, 3]");
    /// let mut items = r.read_array()?;
    /// items.next_item::<u8>()?;
    /// let err = items.end().unwrap_err();
    /// assert_eq!(err.kind(), &DecodeErrorKind::WrongLength { expected: 1, found: 3 });
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    pub fn end(mut self) -> Result<(), DecodeError> {
        let expected = self.index;
        if !self.more()? {
            return Ok(());
        }
        while self.skip()? {}
        Err(DecodeError::new(
            DecodeErrorKind::WrongLength {
                expected,
                found: self.index,
            },
            self.start,
        ))
    }

    /// How many items have been read or skipped.
    #[must_use]
    pub fn index(&self) -> usize {
        self.index
    }

    /// The byte offset of the array's `[`.
    #[must_use]
    pub fn offset(&self) -> usize {
        self.start
    }
}

// ---- Entry points ------------------------------------------------------------

/// Reads `text` as one `T` ([`FromJson`]), with the default [`Limits`] and
/// duplicate keys refused. The whole text must be that one value.
///
/// # Errors
///
/// The [`DecodeError`] of `T`, or the parse error of the text (including
/// the rest of the document after the value).
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
///
/// let scores: BTreeMap<String, Vec<u32>> = jsonx_lib::from_str(r#"{"ada": [3, 5]}"#)?;
/// assert_eq!(scores["ada"], [3, 5]);
///
/// let err = jsonx_lib::from_str::<u8>("300").unwrap_err();
/// assert_eq!(err.to_string(), "number out of range for u8 at byte 0");
/// # Ok::<(), jsonx_lib::DecodeError>(())
/// ```
pub fn from_str<'de, T: FromJson<'de>>(text: &'de str) -> Result<T, DecodeError> {
    Parser::new().decode(text)
}

/// Reads `bytes`, which must be UTF-8, as one `T`; see [`from_str`].
///
/// # Errors
///
/// As [`from_str`], plus [`ErrorKind::InvalidUtf8`] and
/// [`ErrorKind::InputTooLarge`] as for [`parse_bytes`](crate::parse_bytes).
///
/// # Examples
///
/// ```
/// let pair: (bool, u8) = jsonx_lib::from_bytes(b"[true, 2]")?;
/// assert_eq!(pair, (true, 2));
/// # Ok::<(), jsonx_lib::DecodeError>(())
/// ```
pub fn from_bytes<'de, T: FromJson<'de>>(bytes: &'de [u8]) -> Result<T, DecodeError> {
    Parser::new().decode_bytes(bytes)
}

/// Reads an already built [`Value`] as a `T`, for a type that owns its data.
///
/// The value is written as compact text and read back, so this costs a write
/// and a scan; byte offsets in an error point into that text (the path is
/// the useful part). The default [`Limits`] apply, which bounds the
/// recursion of a deeply nested type.
///
/// # Errors
///
/// The [`DecodeError`] of `T`.
///
/// # Examples
///
/// ```
/// let v = jsonx_lib::json!({ "list": [1, 2] });
/// let list: Vec<u8> = jsonx_lib::from_value(&v["list"])?;
/// assert_eq!(list, [1, 2]);
/// # Ok::<(), jsonx_lib::DecodeError>(())
/// ```
pub fn from_value<T: for<'de> FromJson<'de>>(value: &Value<'_>) -> Result<T, DecodeError> {
    let text = crate::to_string(value);
    from_str(&text)
}

/// Builds a [`Value`] from any [`ToJson`] type, by writing it and reading it
/// back (members exactly as written, duplicates included).
///
/// # Examples
///
/// ```
/// let v = jsonx_lib::to_value(&(1, "two", [true]));
/// assert_eq!(v[2][0], true);
/// ```
#[must_use]
pub fn to_value<T: ToJson + ?Sized>(value: &T) -> Value<'static> {
    let text = crate::to_string(value);
    Parser::new()
        .limits(Limits::unlimited())
        .duplicate_keys(DuplicateKeys::KeepAll)
        .parse(&text)
        .map(Value::into_owned)
        .unwrap_or_default()
}

impl Parser {
    /// Reads `text` as one `T` with this parser's budgets and duplicate-key
    /// policy; see [`from_str`].
    ///
    /// # Errors
    ///
    /// As [`from_str`].
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{ErrorKind, Limits, Parser};
    ///
    /// let shallow = Parser::new().limits(Limits::new().with_max_depth(1));
    /// assert_eq!(shallow.decode::<Vec<u8>>("[1]")?, [1]);
    /// let err = shallow.decode::<Vec<Vec<u8>>>("[[1]]").unwrap_err();
    /// assert_eq!(err.syntax_error().map(|e| e.kind()), Some(ErrorKind::DepthLimitExceeded));
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    pub fn decode<'de, T: FromJson<'de>>(&self, text: &'de str) -> Result<T, DecodeError> {
        let mut reader = self.reader(text);
        let value = reader.decode()?;
        reader.read_to_end()?;
        Ok(value)
    }

    /// Reads `bytes`, which must be UTF-8, as one `T`; see
    /// [`decode`](Self::decode).
    ///
    /// # Errors
    ///
    /// As [`from_bytes`].
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(jsonx_lib::Parser::new().decode_bytes::<&str>(b"\"x\"")?, "x");
    /// # Ok::<(), jsonx_lib::DecodeError>(())
    /// ```
    pub fn decode_bytes<'de, T: FromJson<'de>>(&self, bytes: &'de [u8]) -> Result<T, DecodeError> {
        let mut reader = self.reader_bytes(bytes)?;
        let value = reader.decode()?;
        reader.read_to_end()?;
        Ok(value)
    }
}

// ---- Nullable ----------------------------------------------------------------

/// A member that may be absent, `null`, or a value, keeping the three apart.
///
/// LSP and MCP use both TypeScript's optional `key?: T` and nullable
/// `key: T | null`, and sometimes `key?: T | null`, where "left out" and
/// "`null`" mean different things (`null` often clears a setting). An
/// `Option<T>` reads both as `None`; a `Nullable<T>` tells them apart, and
/// writes back what it read: [`ObjectWriter::optional`](crate::ObjectWriter::optional)
/// leaves out `Absent` and writes `Null` as `null`.
///
/// # Examples
///
/// ```
/// use jsonx_lib::{DecodeError, FromJson, Nullable, Reader, ToJson, ValueWriter, Written};
///
/// struct Settings { tab_size: Nullable<u8> }
///
/// impl<'de> FromJson<'de> for Settings {
///     fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
///         let mut tab_size = None;
///         let mut o = reader.read_object()?;
///         while let Some(key) = o.next_key()? {
///             if key == "tabSize" { o.field(&mut tab_size)?; }
///         }
///         Ok(Settings { tab_size: o.required(tab_size, "tabSize")? })
///     }
/// }
///
/// impl ToJson for Settings {
///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
///         out.object(|o| o.optional("tabSize", &self.tab_size))
///     }
/// }
///
/// for text in ["{}", r#"{"tabSize":null}"#, r#"{"tabSize":4}"#] {
///     let s: Settings = jsonx_lib::from_str(text)?;
///     assert_eq!(jsonx_lib::to_string(&s), text);
/// }
/// # Ok::<(), DecodeError>(())
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Nullable<T> {
    /// The member is not there.
    #[default]
    Absent,
    /// The member is there, and `null`.
    Null,
    /// The member is there, with a value.
    Value(T),
}

impl<T> Nullable<T> {
    /// The value, if there is one: `Absent` and `Null` are both `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::Nullable;
    ///
    /// assert_eq!(Nullable::Value(3).into_option(), Some(3));
    /// assert_eq!(Nullable::<u8>::Null.into_option(), None);
    /// ```
    #[inline]
    pub fn into_option(self) -> Option<T> {
        match self {
            Self::Value(value) => Some(value),
            Self::Absent | Self::Null => None,
        }
    }

    /// A reference to the value, if there is one.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(jsonx_lib::Nullable::Value(3).as_ref(), jsonx_lib::Nullable::Value(&3));
    /// ```
    #[inline]
    pub const fn as_ref(&self) -> Nullable<&T> {
        match self {
            Self::Value(value) => Nullable::Value(value),
            Self::Null => Nullable::Null,
            Self::Absent => Nullable::Absent,
        }
    }

    /// Whether the member is there with `null`.
    ///
    /// # Examples
    ///
    /// ```
    /// assert!(jsonx_lib::Nullable::<u8>::Null.is_null());
    /// ```
    #[inline]
    #[must_use]
    pub const fn is_null(&self) -> bool {
        matches!(self, Self::Null)
    }
}

impl<'de, T: FromJson<'de>> FromJson<'de> for Nullable<T> {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        if reader.peek_kind()? == Some(Kind::Null) {
            let _null = reader.next_event()?;
            Ok(Self::Null)
        } else {
            decode_value(reader).map(Self::Value)
        }
    }

    #[inline]
    fn absent() -> Option<Self> {
        Some(Self::Absent)
    }
}

impl<T: ToJson> ToJson for Nullable<T> {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        match self {
            Self::Value(value) => value.to_json(out),
            Self::Absent | Self::Null => out.null(),
        }
    }

    #[inline]
    fn is_absent(&self) -> bool {
        matches!(self, Self::Absent)
    }
}

// ---- Implementations ---------------------------------------------------------

impl<'de> FromJson<'de> for bool {
    #[inline]
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        match reader.scalar("a boolean")? {
            (Event::Bool(b), _) => Ok(b),
            (other, at) => Err(DecodeError::wrong_type("a boolean", event_kind(&other), at)),
        }
    }
}

/// Reads a number value for the target type `name`.
#[inline]
fn number<'de>(
    reader: &mut Reader<'de>,
    name: &'static str,
) -> Result<(Number<'de>, usize), DecodeError> {
    match reader.scalar(name)? {
        (Event::Number(n), at) => Ok((n, at)),
        (other, at) => Err(DecodeError::wrong_type(name, event_kind(&other), at)),
    }
}

/// An integer read whose text is not an integer in range: under
/// [`Numbers::WholeValues`], the value of any spelling of a whole number;
/// otherwise (or out of range) [`DecodeErrorKind::OutOfRange`].
#[cold]
#[inline(never)]
fn whole_value<T: TryFrom<i128>>(
    n: &Number<'_>,
    lenient: bool,
    at: usize,
    expected: &'static str,
) -> Result<T, DecodeError> {
    lenient
        .then(|| n.whole_value())
        .flatten()
        .and_then(|w| T::try_from(w).ok())
        .ok_or_else(|| DecodeError::new(DecodeErrorKind::OutOfRange { expected }, at))
}

macro_rules! from_json_integer {
    ($($t:ty => $via:ident),* $(,)?) => {$(
        impl<'de> FromJson<'de> for $t {
            #[inline]
            fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
                let lenient = reader.number_policy() == Numbers::WholeValues;
                let (n, at) = number(reader, stringify!($t))?;
                match n.$via().and_then(|wide| <$t>::try_from(wide).ok()) {
                    Some(value) => Ok(value),
                    None => whole_value(&n, lenient, at, stringify!($t)),
                }
            }
        }
    )*};
}

from_json_integer!(
    i8 => as_i64, i16 => as_i64, i32 => as_i64, i64 => as_i64, isize => as_i128, i128 => as_i128,
    u8 => as_u64, u16 => as_u64, u32 => as_u64, u64 => as_u64, usize => as_u128, u128 => as_u128,
);

impl<'de> FromJson<'de> for f64 {
    #[inline]
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let (n, at) = number(reader, "f64")?;
        n.as_f64()
            .ok_or_else(|| DecodeError::new(DecodeErrorKind::OutOfRange { expected: "f64" }, at))
    }
}

impl<'de> FromJson<'de> for f32 {
    #[inline]
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let (n, at) = number(reader, "f32")?;
        n.as_f32()
            .ok_or_else(|| DecodeError::new(DecodeErrorKind::OutOfRange { expected: "f32" }, at))
    }
}

/// Reads a string value for the target type `name`.
#[inline]
fn string<'de>(
    reader: &mut Reader<'de>,
    name: &'static str,
) -> Result<(Cow<'de, str>, usize), DecodeError> {
    match reader.scalar(name)? {
        (Event::String(s), at) => Ok((s, at)),
        (other, at) => Err(DecodeError::wrong_type(name, event_kind(&other), at)),
    }
}

impl<'de> FromJson<'de> for Cow<'de, str> {
    #[inline]
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let (s, _) = string(reader, "a string")?;
        if let Cow::Owned(decoded) = &s {
            reader.hold(decoded.capacity())?;
        }
        Ok(s)
    }
}

impl<'de> FromJson<'de> for &'de str {
    #[inline]
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        match string(reader, "a string")? {
            (Cow::Borrowed(s), _) => Ok(s),
            (Cow::Owned(_), at) => Err(DecodeError::new(DecodeErrorKind::StringNotBorrowable, at)),
        }
    }
}

impl<'de> FromJson<'de> for String {
    #[inline]
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let (s, _) = string(reader, "a string")?;
        reader.hold(match &s {
            Cow::Owned(decoded) => decoded.capacity(),
            Cow::Borrowed(text) => text.len(),
        })?;
        Ok(s.into_owned())
    }
}

impl<'de> FromJson<'de> for Box<str> {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        String::from_json(reader).map(String::into_boxed_str)
    }
}

impl<'de> FromJson<'de> for char {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let (s, at) = string(reader, "a character")?;
        let mut chars = s.chars();
        match (chars.next(), chars.next()) {
            (Some(c), None) => Ok(c),
            _ => Err(DecodeError::invalid_value("a single character", at)),
        }
    }
}

impl<'de> FromJson<'de> for () {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        match reader.scalar("null")? {
            (Event::Null, _) => Ok(()),
            (other, at) => Err(DecodeError::wrong_type("null", event_kind(&other), at)),
        }
    }
}

impl<'de, T: FromJson<'de>> FromJson<'de> for Option<T> {
    #[inline]
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        if reader.peek_kind()? == Some(Kind::Null) {
            let _null = reader.next_event()?;
            Ok(None)
        } else {
            decode_value(reader).map(Some)
        }
    }

    #[inline]
    fn absent() -> Option<Self> {
        Some(None)
    }
}

impl<'de, T: FromJson<'de>> FromJson<'de> for Box<T> {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let value = T::from_json(reader)?;
        reader.hold(size_of::<T>())?;
        Ok(Box::new(value))
    }

    fn absent() -> Option<Self> {
        T::absent().map(Box::new)
    }
}

impl<'de, T: FromJson<'de>> FromJson<'de> for Rc<T> {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let value = T::from_json(reader)?;
        reader.hold(size_of::<T>() + 2 * size_of::<usize>())?;
        Ok(Rc::new(value))
    }

    fn absent() -> Option<Self> {
        T::absent().map(Rc::new)
    }
}

#[cfg(target_has_atomic = "ptr")]
impl<'de, T: FromJson<'de>> FromJson<'de> for Arc<T> {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let value = T::from_json(reader)?;
        reader.hold(size_of::<T>() + 2 * size_of::<usize>())?;
        Ok(Arc::new(value))
    }

    fn absent() -> Option<Self> {
        T::absent().map(Arc::new)
    }
}

/// Reads every item of an array into `push`.
/// What a collection holds per item, for the memory budget.
#[derive(Clone, Copy)]
enum Cost {
    /// A vector of items of this size: its buffer, counted as it doubles.
    Buffer(usize),
    /// A tree or hash set: about this many bytes per item.
    PerItem(usize),
}

/// The bytes a hash or tree collection holds per entry of `entry` bytes: an
/// estimate (a B-tree node's spare slots and links, a hash table's spare
/// capacity and control bytes), at about twice the entries themselves.
const fn per_entry(entry: usize) -> usize {
    2 * entry + size_of::<usize>()
}

fn items<'de, T: FromJson<'de>>(
    reader: &mut Reader<'de>,
    expected: &'static str,
    cost: Cost,
    mut push: impl FnMut(T),
) -> Result<(), DecodeError> {
    let start = reader.open(Kind::Array, expected)?;
    let mut array = ArrayReader {
        reader,
        start,
        index: 0,
        done: false,
    };
    // The buffer a vector of `count` items has grown to (they double, from
    // four), so the charge is made before each growth, at its new size.
    let mut capacity = 0usize;
    while let Some(item) = array.next_item()? {
        let more = match cost {
            Cost::Buffer(size) if array.index > capacity => {
                let grown = capacity.saturating_mul(2).max(4);
                let bytes = (grown - capacity).saturating_mul(size);
                capacity = grown;
                bytes
            }
            Cost::Buffer(_) => 0,
            Cost::PerItem(bytes) => bytes,
        };
        array.reader.hold(more)?;
        push(item);
    }
    Ok(())
}

impl<'de, T: FromJson<'de>> FromJson<'de> for Vec<T> {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let mut out = Vec::new();
        items(reader, "an array", Cost::Buffer(size_of::<T>()), |item| {
            out.push(item)
        })?;
        Ok(out)
    }
}

impl<'de, T: FromJson<'de>> FromJson<'de> for Box<[T]> {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        Vec::from_json(reader).map(Vec::into_boxed_slice)
    }
}

impl<'de, T: FromJson<'de>> FromJson<'de> for VecDeque<T> {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let mut out = VecDeque::new();
        items(reader, "an array", Cost::Buffer(size_of::<T>()), |item| {
            out.push_back(item);
        })?;
        Ok(out)
    }
}

impl<'de, T: FromJson<'de> + Ord> FromJson<'de> for BTreeSet<T> {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let mut out = BTreeSet::new();
        items(
            reader,
            "an array",
            Cost::PerItem(per_entry(size_of::<T>())),
            |item| {
                let _new = out.insert(item);
            },
        )?;
        Ok(out)
    }
}

#[cfg(feature = "std")]
impl<'de, T, S> FromJson<'de> for std::collections::HashSet<T, S>
where
    T: FromJson<'de> + Eq + core::hash::Hash,
    S: core::hash::BuildHasher + Default,
{
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let mut out = Self::default();
        items(
            reader,
            "an array",
            Cost::PerItem(per_entry(size_of::<T>())),
            |item| {
                let _new = out.insert(item);
            },
        )?;
        Ok(out)
    }
}

impl<'de, T: FromJson<'de>, const N: usize> FromJson<'de> for [T; N] {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let mut array = reader.read_array()?;
        let mut out = Vec::with_capacity(N.min(1024));
        while out.len() < N {
            match array.next_item()? {
                Some(item) => out.push(item),
                None => {
                    return Err(DecodeError::new(
                        DecodeErrorKind::WrongLength {
                            expected: N,
                            found: out.len(),
                        },
                        array.offset(),
                    ));
                }
            }
        }
        array.end()?;
        out.try_into().map_err(|_unreachable: Vec<T>| {
            DecodeError::new(
                DecodeErrorKind::WrongLength {
                    expected: N,
                    found: N,
                },
                0,
            )
        })
    }
}

macro_rules! from_json_tuple {
    ($(($len:literal: $($name:ident),+))*) => {$(
        impl<'de, $($name: FromJson<'de>),+> FromJson<'de> for ($($name,)+) {
            fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
                let mut array = reader.read_array()?;
                let tuple = ($(
                    match array.next_item::<$name>()? {
                        Some(item) => item,
                        None => {
                            return Err(DecodeError::new(
                                DecodeErrorKind::WrongLength { expected: $len, found: array.index() },
                                array.offset(),
                            ));
                        }
                    },
                )+);
                array.end()?;
                Ok(tuple)
            }
        }
    )*};
}

from_json_tuple! {
    (1: A)
    (2: A, B)
    (3: A, B, C)
    (4: A, B, C, D)
    (5: A, B, C, D, E)
    (6: A, B, C, D, E, F)
    (7: A, B, C, D, E, F, G)
    (8: A, B, C, D, E, F, G, H)
}

/// A map a JSON object can be read into.
trait Map<K, V> {
    fn has(&self, key: &K) -> bool;
    fn put(&mut self, key: K, value: V);
}

impl<K: Ord, V> Map<K, V> for BTreeMap<K, V> {
    fn has(&self, key: &K) -> bool {
        self.contains_key(key)
    }
    fn put(&mut self, key: K, value: V) {
        let _old = self.insert(key, value);
    }
}

#[cfg(feature = "std")]
impl<K: Eq + core::hash::Hash, V, S: core::hash::BuildHasher> Map<K, V>
    for std::collections::HashMap<K, V, S>
{
    fn has(&self, key: &K) -> bool {
        self.contains_key(key)
    }
    fn put(&mut self, key: K, value: V) {
        let _old = self.insert(key, value);
    }
}

/// Reads every member of an object into `map`. A key that repeats one
/// already in the map (two spellings of one key, or two texts one
/// [`FromJsonKey`] makes equal) follows the reader's policy: refused under
/// `Error`, the later value skipped under `First`, kept under `Last` and
/// `KeepAll`.
fn members<'de, K: FromJsonKey<'de>, V: FromJson<'de>>(
    reader: &mut Reader<'de>,
    map: &mut impl Map<K, V>,
) -> Result<(), DecodeError> {
    let policy = reader.duplicate_policy();
    let mut object = reader.read_object()?;
    while let Some(text) = object.next_key()? {
        let key = K::from_json_key(text)
            .map_err(|kind| object.in_member(DecodeError::new(kind, object.key_offset)))?;
        if map.has(&key) {
            match policy {
                DuplicateKeys::First => {
                    object.skip()?;
                    continue;
                }
                DuplicateKeys::Last | DuplicateKeys::KeepAll => {}
                _ => {
                    let e = DecodeError::new(DecodeErrorKind::DuplicateField, object.key_offset);
                    return Err(object.in_member(e));
                }
            }
        }
        let value = object.value()?;
        object.reader.hold(per_entry(size_of::<(K, V)>()))?;
        map.put(key, value);
    }
    Ok(())
}

impl<'de, K: FromJsonKey<'de> + Ord, V: FromJson<'de>> FromJson<'de> for BTreeMap<K, V> {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let mut out = BTreeMap::new();
        members(reader, &mut out)?;
        Ok(out)
    }
}

#[cfg(feature = "std")]
impl<'de, K, V, S> FromJson<'de> for std::collections::HashMap<K, V, S>
where
    K: FromJsonKey<'de> + Eq + core::hash::Hash,
    V: FromJson<'de>,
    S: core::hash::BuildHasher + Default,
{
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        let mut out = Self::default();
        members(reader, &mut out)?;
        Ok(out)
    }
}

impl<'de> FromJsonKey<'de> for String {
    #[inline]
    fn from_json_key(key: Cow<'de, str>) -> Result<Self, DecodeErrorKind> {
        Ok(key.into_owned())
    }
}

impl<'de> FromJsonKey<'de> for Box<str> {
    #[inline]
    fn from_json_key(key: Cow<'de, str>) -> Result<Self, DecodeErrorKind> {
        Ok(key.into_owned().into_boxed_str())
    }
}

impl<'de> FromJsonKey<'de> for Cow<'de, str> {
    #[inline]
    fn from_json_key(key: Cow<'de, str>) -> Result<Self, DecodeErrorKind> {
        Ok(key)
    }
}

impl<'de> FromJsonKey<'de> for &'de str {
    #[inline]
    fn from_json_key(key: Cow<'de, str>) -> Result<Self, DecodeErrorKind> {
        match key {
            Cow::Borrowed(key) => Ok(key),
            Cow::Owned(_) => Err(DecodeErrorKind::StringNotBorrowable),
        }
    }
}

impl<'de> FromJson<'de> for Value<'de> {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        match reader.read_value()? {
            Some(value) => Ok(value),
            None => Err(DecodeError::new(
                DecodeErrorKind::Syntax(ErrorKind::ExpectedValue),
                reader.offset(),
            )),
        }
    }
}

impl<'de> FromJson<'de> for Number<'de> {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        number(reader, "a number").map(|(n, _)| n)
    }
}

/// Reads a whole value of `kind` as a [`Value`].
fn tree<'de>(
    reader: &mut Reader<'de>,
    kind: Kind,
    expected: &'static str,
) -> Result<Value<'de>, DecodeError> {
    match reader.peek_kind()? {
        Some(found) if found != kind => {
            Err(DecodeError::wrong_type(expected, found, reader.offset()))
        }
        _ => Value::from_json(reader),
    }
}

impl<'de> FromJson<'de> for Array<'de> {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        match tree(reader, Kind::Array, "an array")? {
            Value::Array(array) => Ok(array),
            other => Err(DecodeError::wrong_type(
                "an array",
                other.kind(),
                reader.event_offset(),
            )),
        }
    }
}

impl<'de> FromJson<'de> for Object<'de> {
    fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
        match tree(reader, Kind::Object, "an object")? {
            Value::Object(object) => Ok(object),
            other => Err(DecodeError::wrong_type(
                "an object",
                other.kind(),
                reader.event_offset(),
            )),
        }
    }
}