struson 0.7.2

A low-level streaming JSON reader and writer
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
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
//! JSON reader variant which is easier to use than [`JsonReader`]
//!
//! The main entrypoint is [`SimpleJsonReader`], all other structs and traits
//! are used by it for reading certain JSON value types.
//!
//! ----
//!
//! **🔬 Experimental**\
//! This API and the naming is currently experimental, please provide feedback [here](https://github.com/Marcono1234/struson/issues/34).
//! Any feedback is appreciated!

use std::{error::Error, io::Read, str::FromStr};

use self::{
    error_safe_reader::ErrorSafeJsonReader,
    multi_json_path::{MultiJsonPath, MultiJsonPathPiece},
};
use crate::{
    reader::{
        JsonReader, JsonReaderPosition, JsonStreamReader, JsonSyntaxError, ReaderError,
        SyntaxErrorKind, TransferError, ValueType,
        json_path::{JsonPath, JsonPathPiece},
        simple::error_safe_reader::create_dummy_error,
    },
    utf8,
    writer::JsonWriter,
};

/// Module for 'multi JSON path'
///
/// A 'multi JSON path' can point to multiple JSON values in a JSON document. It consists of zero or more
/// [`MultiJsonPathPiece`] elements which either represent JSON array items or JSON object members. These
/// pieces combined form the _path_ to the values in a JSON document.
///
/// This is not an implementation of the [JSONPath RFC 9535](https://www.rfc-editor.org/rfc/rfc9535) standard;
/// the implementation here uses a different syntax and only covers a subset of these features suitable
/// for the streaming JSON reader of this library. If you need more functionality, prefer a library
/// which fully implements the JSONPath standard.
///
/// If the path consists only of plain array indices and object member names, and therefore points to
/// exactly one value, the [`json_path` module](crate::reader::json_path) should be used instead.
///
/// The macro [`multi_json_path!`](multi_json_path::multi_json_path) can be used to create a JSON path in
/// a concise way. The path can then be used for [`ValueReader::read_seeked_multi`].
/*
 * Note: Multi JSON path is currently only provided for the Simple API but not for the Advanced API.
 * For the Simple API it can be easily verified that a single value was fully consumed; for the
 * Advanced API verifying this is more difficult. And the style of having a `FnMut` which is called
 * for all matched values also does not match the current style of the Advanced API.
 */
pub mod multi_json_path {
    /// A 'multi JSON path'
    ///
    /// A 'multi JSON path' as represented by this module are zero or more [`MultiJsonPathPiece`] elements.
    /// The macro [`multi_json_path!`] can be used to create a multi JSON path in a concise way.
    /* TODO: Check if it is somehow possible to implement Display for this */
    pub type MultiJsonPath = [MultiJsonPathPiece];

    /// A piece of a 'multi JSON path'
    ///
    /// A piece can either represent JSON array items or JSON object members.
    #[derive(PartialEq, Eq, Clone, Debug)]
    pub enum MultiJsonPathPiece {
        /// JSON array item at the specified index (starting at 0)
        ArrayItem(u32),
        /// All items of a JSON array
        AllArrayItems {
            /// Whether the array is allowed to be empty
            ///
            /// If `false` and an empty array is encountered, an error is returned. Regardless of this
            /// setting the JSON value must be present and must be a JSON array. If the value is missing
            /// or has a different value type, an error is returned.
            ///
            /// This only applies to the value matched by this path piece. If this path piece is not
            /// reached because a previous piece had no value, then this will not cause an error despite
            /// `allow_empty` being `false`.
            allow_empty: bool,
        },
        /// JSON object member with the specified name
        ObjectMember(String),
        /// Optional JSON object member with the specified name
        ///
        /// Indicates that the member (and any nested values) might not exist in a JSON object.
        /// This only covers the case where no member with the name exists; a member with explicit
        /// `null` JSON value will be considered present.
        OptionalObjectMember(String),
        /// All members of a JSON object, regardless of name
        ///
        /// If multiple members in a JSON object have the same name (for example `{"a": 1, "a": 2}`)
        /// this path piece matches all of them.
        AllObjectMembers {
            /// Whether the object is allowed to be empty
            ///
            /// If `false` and an empty object is encountered, an error is returned. Regardless of this
            /// setting the JSON value must be present and must be a JSON object. If the value is missing
            /// or has a different value type, an error is returned.
            ///
            /// This only applies to the value matched by this path piece. If this path piece is not
            /// reached because a previous piece had no value, then this will not cause an error despite
            /// `allow_empty` being `false`.
            allow_empty: bool,
        },
    }

    /// Creates a [`MultiJsonPathPiece::ArrayItem`] with the number as index
    impl From<u32> for MultiJsonPathPiece {
        fn from(v: u32) -> Self {
            MultiJsonPathPiece::ArrayItem(v)
        }
    }

    /// Creates a [`MultiJsonPathPiece::ObjectMember`] with the string as member name
    impl From<String> for MultiJsonPathPiece {
        fn from(v: String) -> Self {
            MultiJsonPathPiece::ObjectMember(v)
        }
    }

    /// Creates a [`MultiJsonPathPiece::ObjectMember`] with the string as member name
    impl From<&str> for MultiJsonPathPiece {
        fn from(v: &str) -> Self {
            MultiJsonPathPiece::ObjectMember(v.to_string())
        }
    }

    // TODO: Is there a built-in trait for this? `Into<String>` and `ToString` might allow too many unrelated types,
    //   and calling `expr.to_owned()` in the macro creates a redundant clone for `String::to_owned`
    #[doc(hidden)]
    pub trait __IntoString {
        fn into_string(self) -> String;
    }
    // Covers the same types supported for `MultiJsonPathPiece::ObjectMember`
    impl __IntoString for String {
        fn into_string(self) -> String {
            self
        }
    }
    impl __IntoString for &str {
        fn into_string(self) -> String {
            self.to_owned()
        }
    }

    /// Actual implementation of `multi_json_path!`
    /*
     * This is a separate macro because:
     * - It allows omitting all the verbose rules from the rustdoc
     * - It allows specifying internal rules without having to rely on verbose patterns such
     *   as 'Internal Rules' (https://veykril.github.io/tlborm/decl-macros/patterns/internal-rules.html)
     */
    #[doc(hidden)]
    #[macro_export] // must be exported, otherwise the public `multi_json_path!` cannot use it
    macro_rules! __multi_json_path_impl {
        // 'Incremental TT Muncher', see https://veykril.github.io/tlborm/decl-macros/patterns/tt-muncher.html
        // Based partially on serde_json's `json!` macro implementation

        // First cover special syntax `[*]`, `[+]`, `{*}` and `[+]`
        ( [$($pieces:expr,)*] [*] $($rest:tt)* ) => {
            $crate::__multi_json_path_impl!(
                [$($pieces,)* $crate::reader::simple::multi_json_path::MultiJsonPathPiece::AllArrayItems { allow_empty: true }] $($rest)*
            )
        };
        ( [$($pieces:expr,)*] [+] $($rest:tt)* ) => {
            $crate::__multi_json_path_impl!(
                [$($pieces,)* $crate::reader::simple::multi_json_path::MultiJsonPathPiece::AllArrayItems { allow_empty: false }] $($rest)*
            )
        };
        ( [$($pieces:expr,)*] {*} $($rest:tt)* ) => {
            $crate::__multi_json_path_impl!(
                [$($pieces,)* $crate::reader::simple::multi_json_path::MultiJsonPathPiece::AllObjectMembers { allow_empty: true }] $($rest)*
            )
        };
        ( [$($pieces:expr,)*] {+} $($rest:tt)* ) => {
            $crate::__multi_json_path_impl!(
                [$($pieces,)* $crate::reader::simple::multi_json_path::MultiJsonPathPiece::AllObjectMembers { allow_empty: false }] $($rest)*
            )
        };

        // Then cover optional object member piece `?name`
        // Needs two rules because macro_rules requires that `,` must follow `expr` here
        ( [$($pieces:expr,)*] ?$piece:expr, $($rest:tt)* ) => {
            $crate::__multi_json_path_impl!(
                // Include trailing `,` to disallow duplicated commas
                [$($pieces,)* $crate::reader::simple::multi_json_path::MultiJsonPathPiece::OptionalObjectMember($crate::reader::simple::multi_json_path::__IntoString::into_string($piece)),] $($rest)*
            )
        };
        // Last piece
        ( [$($pieces:expr,)*] ?$piece:expr) => {
            $crate::__multi_json_path_impl!(
                [$($pieces,)* $crate::reader::simple::multi_json_path::MultiJsonPathPiece::OptionalObjectMember($crate::reader::simple::multi_json_path::__IntoString::into_string($piece)),]
            )
        };

        // Then cover regular array item and object member pieces
        // Needs two rules because macro_rules requires that `,` must follow `expr` here
        ( [$($pieces:expr,)*] $piece:expr, $($rest:tt)* ) => {
            $crate::__multi_json_path_impl!(
                // Include trailing `,` to disallow duplicated commas
                [$($pieces,)* $crate::reader::simple::multi_json_path::MultiJsonPathPiece::from($piece),] $($rest)*
            )
        };
        // Last piece
        ( [$($pieces:expr,)*] $piece:expr) => {
            $crate::__multi_json_path_impl!(
                [$($pieces,)* $crate::reader::simple::multi_json_path::MultiJsonPathPiece::from($piece),]
            )
        };

        // Leading comma (only if there is at least one piece, and no trailing comma yet)
        ( [$($pieces:expr),+] , $($rest:tt)*) => {
            $crate::__multi_json_path_impl!(
                // Include trailing `,` to disallow duplicated commas
                [$($pieces),+ ,] $($rest)*
            )
        };

        // Finish
        ( [] ) => {
            // Specify the type to avoid it being used by accident as an empty array of any type
            [] as [$crate::reader::simple::multi_json_path::MultiJsonPathPiece; 0]
        };
        ( [$($pieces:expr),+ $(,)?] ) => {
            [$($pieces),+]
        };
    }

    /*
     * TODO: Ideally in the future not expose this at the crate root but only from the `multi_json_path` module
     *   however, that is apparently not easily possible yet.
     *   Therefore for now hide this macro and re-export it with the desired name below, see
     *   https://internals.rust-lang.org/t/pub-on-macro-rules/19358/16
     */
    #[doc(hidden)]
    #[macro_export]
    macro_rules! __multi_json_path {
        ( $($path:tt)* ) => {
            $crate::__multi_json_path_impl!([] $($path)*)
        };
    }

    /// Creates a 'multi JSON path' from path pieces, separated by commas
    ///
    /// The arguments to this macro represent the path pieces:
    ///
    /// | Argument | Path piece |
    /// | - | - |
    /// | number of type `u32` | [`ArrayItem`](MultiJsonPathPiece::ArrayItem) |
    /// | string | [`ObjectMember`](MultiJsonPathPiece::ObjectMember) |
    /// | '`?`string' | [`OptionalObjectMember`](MultiJsonPathPiece::OptionalObjectMember) |
    /// | '`[*]`' | [`AllArrayItems { allow_empty: true }`](MultiJsonPathPiece::AllArrayItems) |
    /// | '`[+]`' | [`AllArrayItems { allow_empty: false }`](MultiJsonPathPiece::AllArrayItems) |
    /// | '`{*}`' | [`AllObjectMembers { allow_empty: true }`](MultiJsonPathPiece::AllObjectMembers) |
    /// | '`{+}`' | [`AllObjectMembers { allow_empty: false }`](MultiJsonPathPiece::AllObjectMembers) |
    ///
    /// Providing no arguments creates an empty path without any path pieces.
    ///
    /// # Examples
    /// Let's assume you have the following JSON data representing a list of books where
    /// some of them have received awards, stored in the optional nested `awards` array.
    /// ```json
    /// {
    ///   "books": [
    ///     {
    ///       "title": "A normal day in the life of an ant",
    ///       "awards": [
    ///         "Best Thriller",
    ///         "Best Short Story"
    ///       ]
    ///     },
    ///     {
    ///       "title": "Tired of Potatoes"
    ///     }
    ///   ]  
    /// }
    /// ```
    ///
    /// Then the following multi JSON path can be used to match all awards any book has
    /// received:
    /// ```
    /// # use struson::reader::simple::multi_json_path::*;
    /// # use struson::reader::simple::multi_json_path::MultiJsonPathPiece::*;
    /// let json_path = multi_json_path![
    ///     "books",
    ///     [*], // match all books
    ///     ?"awards", // match the optional "awards" member
    ///     [+], // match all awards (requiring non-empty array)
    /// ];
    ///
    /// assert_eq!(
    ///     json_path,
    ///     [
    ///         ObjectMember("books".to_owned()),
    ///         AllArrayItems { allow_empty: true },
    ///         OptionalObjectMember("awards".to_owned()),
    ///         AllArrayItems { allow_empty: false },
    ///     ]
    /// );
    /// ```
    /* Re-export the macro to be available under the `struson::reader::simple::multi_json_path` module path */
    #[doc(inline)]
    pub use __multi_json_path as multi_json_path;

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn macro_multi_json_path() {
            assert_eq!(multi_json_path![], []);
            // Make sure the type is correct and it can be compared with a &MultiJsonPath
            assert_ne!(
                multi_json_path![],
                &[MultiJsonPathPiece::ArrayItem(1)] as &MultiJsonPath
            );
            assert_eq!(multi_json_path![3], [MultiJsonPathPiece::ArrayItem(3)]);
            // Make sure the type is correct and it can be compared with a &MultiJsonPath
            assert_eq!(
                multi_json_path![3],
                &[MultiJsonPathPiece::ArrayItem(3)] as &MultiJsonPath
            );
            assert_eq!(
                multi_json_path!["a"],
                [MultiJsonPathPiece::ObjectMember("a".to_owned())]
            );
            assert_eq!(
                multi_json_path!["a".to_owned()],
                [MultiJsonPathPiece::ObjectMember("a".to_owned())]
            );
            assert_eq!(
                // Trailing comma
                multi_json_path!["a",],
                [MultiJsonPathPiece::ObjectMember("a".to_owned())]
            );
            assert_eq!(
                // Trailing comma
                multi_json_path!["a", 1,],
                [
                    MultiJsonPathPiece::ObjectMember("a".to_owned()),
                    MultiJsonPathPiece::ArrayItem(1),
                ]
            );
            assert_eq!(
                multi_json_path!["outer", 1, "inner".to_owned(), 2],
                [
                    MultiJsonPathPiece::ObjectMember("outer".to_owned()),
                    MultiJsonPathPiece::ArrayItem(1),
                    MultiJsonPathPiece::ObjectMember("inner".to_owned()),
                    MultiJsonPathPiece::ArrayItem(2),
                ]
            );

            assert_eq!(
                multi_json_path![?"a"],
                [MultiJsonPathPiece::OptionalObjectMember("a".to_owned())]
            );
            assert_eq!(
                multi_json_path![?"a".to_owned()],
                [MultiJsonPathPiece::OptionalObjectMember("a".to_owned())]
            );
            // Trailing comma
            assert_eq!(
                multi_json_path![?"a",],
                [MultiJsonPathPiece::OptionalObjectMember("a".to_owned())]
            );
            assert_eq!(
                multi_json_path![?"a", "b"],
                [
                    MultiJsonPathPiece::OptionalObjectMember("a".to_owned()),
                    MultiJsonPathPiece::ObjectMember("b".to_owned()),
                ]
            );

            assert_eq!(
                multi_json_path![[*]],
                [MultiJsonPathPiece::AllArrayItems { allow_empty: true }]
            );
            // Trailing comma
            assert_eq!(
                multi_json_path![[*],],
                [MultiJsonPathPiece::AllArrayItems { allow_empty: true }]
            );
            assert_eq!(
                multi_json_path![1, [*]],
                [
                    MultiJsonPathPiece::ArrayItem(1),
                    MultiJsonPathPiece::AllArrayItems { allow_empty: true },
                ]
            );
            // Trailing comma
            assert_eq!(
                multi_json_path![1, [*],],
                [
                    MultiJsonPathPiece::ArrayItem(1),
                    MultiJsonPathPiece::AllArrayItems { allow_empty: true },
                ]
            );
            assert_eq!(
                multi_json_path![1, [*], 1],
                [
                    MultiJsonPathPiece::ArrayItem(1),
                    MultiJsonPathPiece::AllArrayItems { allow_empty: true },
                    MultiJsonPathPiece::ArrayItem(1),
                ]
            );
            assert_eq!(
                multi_json_path![[+]],
                [MultiJsonPathPiece::AllArrayItems { allow_empty: false }]
            );

            assert_eq!(
                multi_json_path![{*}],
                [MultiJsonPathPiece::AllObjectMembers { allow_empty: true }]
            );
            // Trailing comma
            assert_eq!(
                multi_json_path![{*},],
                [MultiJsonPathPiece::AllObjectMembers { allow_empty: true }]
            );
            assert_eq!(
                multi_json_path![1, {*}],
                [
                    MultiJsonPathPiece::ArrayItem(1),
                    MultiJsonPathPiece::AllObjectMembers { allow_empty: true },
                ]
            );
            // Trailing comma
            assert_eq!(
                multi_json_path![1, {*},],
                [
                    MultiJsonPathPiece::ArrayItem(1),
                    MultiJsonPathPiece::AllObjectMembers { allow_empty: true },
                ]
            );
            assert_eq!(
                multi_json_path![1, {*}, 1],
                [
                    MultiJsonPathPiece::ArrayItem(1),
                    MultiJsonPathPiece::AllObjectMembers { allow_empty: true },
                    MultiJsonPathPiece::ArrayItem(1),
                ]
            );
            assert_eq!(
                multi_json_path![{+}],
                [MultiJsonPathPiece::AllObjectMembers { allow_empty: false }]
            );

            assert_eq!(
                multi_json_path![[*], [+], {*}, {+}, 1],
                [
                    MultiJsonPathPiece::AllArrayItems { allow_empty: true },
                    MultiJsonPathPiece::AllArrayItems { allow_empty: false },
                    MultiJsonPathPiece::AllObjectMembers { allow_empty: true },
                    MultiJsonPathPiece::AllObjectMembers { allow_empty: false },
                    MultiJsonPathPiece::ArrayItem(1),
                ]
            );
        }
    }
}

/// Reader for a JSON value
pub trait ValueReader<J: JsonReader> {
    /// Peeks at the type of the next JSON value, without consuming it
    fn peek_value(&mut self) -> Result<ValueType, ReaderError>;

    /// Consumes a JSON null value
    fn read_null(self) -> Result<(), ReaderError>;

    /// Consumes and returns a JSON boolean value
    fn read_bool(self) -> Result<bool, ReaderError>;

    /// Consumes a JSON string value as borrowed `str`
    ///
    /// The function `f` is called with the read JSON string value as argument.
    ///
    /// To read a string value as owned `String` use [`read_string`](Self::read_string).\
    /// To read a large string value in a streaming way, use [`read_string_with_reader`](Self::read_string_with_reader).
    /*
     * Note: Ideally would return a `&str`, but that does not seem to be possible because `self`
     * is consumed. And for some of the `ValueReader` implementations below this would probably
     * also not work because they call additional methods after reading the value, e.g. `consume_trailing_whitespace()`
     */
    fn read_str<T>(
        self,
        f: impl FnOnce(&str) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>>;

    /// Consumes and returns a JSON string value as owned `String`
    ///
    /// To read a string value as borrowed `str` use [`read_str`](Self::read_str).\
    /// To read a large string value in a streaming way, use [`read_string_with_reader`](Self::read_string_with_reader).
    fn read_string(self) -> Result<String, ReaderError>;

    /// Consumes a JSON string using a [`Read`]
    ///
    /// The function `f` is called with a reader as argument which allows reading the JSON string
    /// value. Escape sequences will be automatically converted to the corresponding characters. If
    /// the function returns `Ok` but did not consume all bytes of the string value, the remaining
    /// bytes will be skipped automatically.
    ///
    /// This method is mainly intended for reading large JSON string values in a streaming way;
    /// for short string values prefer [`read_str`](Self::read_str) or [`read_string`](Self::read_string).
    ///
    /// # Examples
    /// ```
    /// # use struson::reader::simple::*;
    /// # use std::io::Read;
    /// let json_reader = SimpleJsonReader::new("\"text with \\\" quote\"".as_bytes());
    /// json_reader.read_string_with_reader(|mut string_reader| {
    ///     let mut buf = [0_u8; 1];
    ///     while string_reader.read(&mut buf)? > 0 {
    ///         println!("Read byte: {}", buf[0]);
    ///     }
    ///     Ok(())
    /// })?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// # Reader errors
    /// When the underlying reader encounters an error, or when malformed JSON data is encountered
    /// the reader will return a [`std::io::Error`].
    ///
    /// The error behavior of the string reader differs from the guarantees made by [`Read`]:
    /// - if an error is returned there are no guarantees about if or how many data has been
    ///   consumed from the underlying data source and been stored in the provided `buf`
    /// - if an error occurs, processing should be aborted, regardless of the kind of the error;
    ///   trying to use the string reader or the original JSON reader afterwards will lead
    ///   to unspecified behavior
    fn read_string_with_reader<T>(
        self,
        f: impl FnOnce(StringValueReader<'_>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>>;

    /// Consumes and returns a JSON number value
    ///
    /// The result is either the parsed number or the parse error. It might be necessary to
    /// help the Rust compiler a bit by explicitly specifying the number type in case it cannot
    /// be inferred automatically.
    ///
    /// # Examples
    /// ```
    /// # use struson::reader::simple::*;
    /// let json_reader = SimpleJsonReader::new("12".as_bytes());
    /// let number: u64 = json_reader.read_number()??;
    /// assert_eq!(number, 12);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    fn read_number<T: FromStr>(self) -> Result<Result<T, T::Err>, ReaderError>;

    /// Consumes and returns the string representation of a JSON number value
    ///
    /// # Examples
    /// ```
    /// # use struson::reader::simple::*;
    /// let json_reader = SimpleJsonReader::new("1.2e3".as_bytes());
    /// let number_string = json_reader.read_number_as_string()?;
    /// assert_eq!(number_string, "1.2e3");
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    fn read_number_as_string(self) -> Result<String, ReaderError>;

    /// Deserializes a Serde [`Deserialize`](serde_core::de::Deserialize) from the next value
    ///
    /// This method is part of the optional Serde integration feature, see the
    /// [`serde` module](crate::serde) of this crate for more information.
    ///
    /// # Examples
    /// ```
    /// # use struson::reader::simple::*;
    /// # use serde::*;
    /// let json_reader = SimpleJsonReader::new(
    ///     r#"{"text": "some text", "number": 5}"#.as_bytes()
    /// );
    ///
    /// #[derive(Deserialize, PartialEq, Debug)]
    /// struct MyStruct {
    ///     text: String,
    ///     number: u64,
    /// }
    ///
    /// let value: MyStruct = json_reader.read_deserialize()?;
    /// assert_eq!(
    ///     value,
    ///     MyStruct { text: "some text".to_owned(), number: 5 }
    /// );
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// # Security
    /// Since JSON data can have an arbitrary structure and can contain arbitrary
    /// data, care must be taken when processing untrusted data. See the documentation
    /// of [`JsonReaderDeserializer`](crate::serde::JsonReaderDeserializer) for
    /// security considerations.
    #[cfg(feature = "serde")]
    fn read_deserialize<'de, D: serde_core::de::Deserialize<'de>>(
        self,
    ) -> Result<D, crate::serde::DeserializerError>;

    /// Skips the next JSON value
    ///
    /// If the value is a JSON array or object, all the nested values will be skipped as well.
    /// This method is usually more efficient than reading the next JSON value using one of
    /// the other methods, but discarding the read result afterwards.
    fn skip_value(self) -> Result<(), ReaderError>;

    /// Consumes a JSON array
    ///
    /// This method is useful when arrays of fixed size or with heterogenous item types are read;
    /// otherwise prefer [`read_array_items`](Self::read_array_items).
    ///
    /// If the exact size is not known in advance, [`ArrayReader::has_next`] can be used to
    /// check if there are more items in the JSON array. If the value types are not known in
    /// advance, [`ArrayReader::peek_value`](ValueReader::peek_value) can be used to peek at
    /// the next item (optionally calling `has_next` before to make sure there is actually a
    /// next item).
    ///
    /// If the function `f` returns `Ok` it must have consumed all array items, either by
    /// reading them or by calling [`ArrayReader::skip_value`](ValueReader::skip_value) until
    /// there are no more items (can be checked using [`ArrayReader::has_next`]). Otherwise
    /// an error is returned.
    ///
    /// # Examples
    /// ```
    /// # use struson::reader::simple::*;
    /// let json_reader = SimpleJsonReader::new("[1, 2, 3]".as_bytes());
    /// let point: (u64, u64, u64) = json_reader.read_array(|array_reader| {
    ///     let x = array_reader.read_number()??;
    ///     let y = array_reader.read_number()??;
    ///     let z = array_reader.read_number()??;
    ///     Ok((x, y, z))
    /// })?;
    /// assert_eq!(point, (1, 2, 3));
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    fn read_array<T>(
        self,
        f: impl FnOnce(&mut ArrayReader<'_, J>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>>;

    /// Consumes a JSON array and all of its items
    ///
    /// This method is useful when arrays with varying size and homogenous item type are read;
    /// otherwise prefer [`read_array`](Self::read_array).
    ///
    /// The function `f` is called repeatedly for all items of the JSON array, if any.
    ///
    /// If the function returns `Ok` but did not consume the array item, either by
    /// reading it (e.g. using [`read_bool`](Self::read_bool)) or by using [`skip_value`](Self::skip_value),
    /// the item will be skipped automatically.
    ///
    /// # Examples
    /// ```
    /// # use struson::reader::simple::*;
    /// let json_reader = SimpleJsonReader::new(r#"["a", "short", "example"]"#.as_bytes());
    /// let mut words = Vec::<String>::new();
    /// json_reader.read_array_items(|item_reader| {
    ///     let word = item_reader.read_string()?;
    ///     words.push(word);
    ///     Ok(())
    /// })?;
    /// assert_eq!(words, vec!["a", "short", "example"]);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    fn read_array_items(
        self,
        mut f: impl FnMut(SingleValueReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>>
    where
        Self: Sized,
    {
        self.read_array(|array_reader| {
            while array_reader.has_next()? {
                read_value(array_reader.json_reader, &mut f)?;
            }
            Ok(())
        })
    }

    /// Consumes a JSON object and all of its members, reading names as borrowed `str`
    ///
    /// The function `f` is called repeatedly for all members of the JSON object, if any. It
    /// takes a [`MemberReader`] as argument which allows reading the name and the value of the
    /// member.
    ///
    /// If the function returns `Ok` but did not read the name or the value of the member, then
    /// name or value will be skipped automatically.
    ///
    /// If the name is needed as owned `String`, then [`read_object_owned_names`](Self::read_object_owned_names)
    /// should be used instead.
    ///
    /// # Examples
    /// ```
    /// # use struson::reader::simple::*;
    /// let json_reader = SimpleJsonReader::new(r#"{"a": 1, "b": 2}"#.as_bytes());
    ///
    /// let mut a: Option<u32> = None;
    /// json_reader.read_object_borrowed_names(|mut member_reader| {
    ///     let name = member_reader.read_name()?;
    ///     match name {
    ///         "a" => a = Some(member_reader.read_number()??),
    ///         _ => {
    ///             // ignore member value
    ///         }
    ///     }
    ///     Ok(())
    /// })?;
    /// assert_eq!(a, Some(1));
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    fn read_object_borrowed_names(
        self,
        f: impl FnMut(MemberReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>>;

    /// Consumes a JSON object and all of its members, reading names as owned `String`
    ///
    /// The function `f` is called repeatedly for all members of the JSON object, if any. It takes
    /// the member name as first argument and a [`SingleValueReader`] for reading the member value
    /// as second argument.
    ///
    /// If the function returns `Ok` but did not consume the value of the member, either by
    /// reading it (e.g. using [`read_bool`](Self::read_bool)) or by using [`skip_value`](Self::skip_value),
    /// the value will be skipped automatically.
    ///
    /// If it suffices to have the name as borrowed `str`, then [`read_object_borrowed_names`](Self::read_object_borrowed_names)
    /// should be used instead.
    ///
    /// # Examples
    /// ```
    /// # use std::collections::HashMap;
    /// # use struson::reader::simple::*;
    /// let json_reader = SimpleJsonReader::new(r#"{"a": 1, "b": 2}"#.as_bytes());
    /// let mut map = HashMap::<String, u64>::new();
    /// json_reader.read_object_owned_names(|name, value_reader| {
    ///     let member_value = value_reader.read_number()??;
    ///     map.insert(name, member_value);
    ///     Ok(())
    /// })?;
    ///
    /// assert_eq!(
    ///     map,
    ///     HashMap::from([
    ///         ("a".to_owned(), 1),
    ///         ("b".to_owned(), 2),
    ///     ])
    /// );
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    fn read_object_owned_names(
        self,
        f: impl FnMut(String, SingleValueReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>>;

    /// Seeks to a value and consumes it
    ///
    /// This method first seeks to the location specified by `path` as described by
    /// [`SimpleJsonReader::seek_to`], then calls the function `f` to consume the value.
    ///
    /// If the path matches a value, the function `f` will be called exactly once.
    /// Otherwise, if the structure of the JSON data does not match the path, for example
    /// when the JSON data contains an array but the path expects an object, an error
    /// is returned and `f` is not called.
    ///
    /// If the function `f` returns `Ok` but did not consume the value, it will be skipped
    /// automatically. After the function has been called this method traverses back to
    /// the original nesting level, therefore acting as if it only consumed one value
    /// (and its nested values) at that level.
    ///
    /// For seeking to and reading multiple values use [`read_seeked_multi`](Self::read_seeked_multi).
    ///
    /// # Examples
    /// ```
    /// # use struson::reader::simple::*;
    /// # use struson::reader::json_path::*;
    /// let json_reader = SimpleJsonReader::new(
    ///     r#"[{"bar": true, "foo": ["a", "b", "c"]}, "next"]"#.as_bytes()
    /// );
    ///
    /// json_reader.read_array(|array_reader| {
    ///     // First seek to the "foo" member, then within its value seek to the item at index 1
    ///     array_reader.read_seeked(&json_path!["foo", 1], |value_reader| {
    ///         // Read the value where the reader seeked to
    ///         assert_eq!(value_reader.read_string()?, "b");
    ///         Ok(())
    ///     })?;
    ///     
    ///     // Afterwards can continue reading at original nesting level
    ///     assert_eq!(array_reader.read_string()?, "next");
    ///     Ok(())
    /// })?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    /*
     * Note: Functionality-wise `read_seeked` might not be needed and a user could instead call
     * `read_seeked_multi` with a plain path without any wildcards. However, `read_seeked` has
     * these advantages:
     * - It guarantees that it either calls `f` or returns an error; for `read_seeked_multi` you
     *   would have to deduce that from the given path or use `at_least_one_match = true`
     * - It supports an `FnOnce` (instead of just an `FnMut`), and returning a result from `f`
     */
    fn read_seeked<T>(
        self,
        path: &JsonPath,
        f: impl FnOnce(SingleValueReader<'_, J>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>>;

    /// Seeks to multiple values and consumes them
    ///
    /// Based on the given `path` this method seeks to all matching values, if any, and calls the
    /// function `f` repeatedly for reading each value. If the function returns `Ok` but did not
    /// consume the value, it will be skipped automatically.
    ///
    /// Depending on what pieces the path consists of, it can match any number of values, including
    /// none. The `at_least_one_match` argument determines the behavior of this method in case the
    /// path matches no values. If `true` an error is returned; this can for example be useful when
    /// a JSON object member is [optional](MultiJsonPathPiece::OptionalObjectMember) but for multiple
    /// of such objects at least one is expected to have the member. If `false` no error is returned
    /// but `f` is also not called. This does not override the behavior for path pieces which themself
    /// require at least one match, such as [`AllArrayItems { allow_empty: false }`](MultiJsonPathPiece::AllArrayItems).
    ///
    /// For [`ObjectMember`](MultiJsonPathPiece::ObjectMember) and [`OptionalObjectMember`](MultiJsonPathPiece::OptionalObjectMember)
    /// pieces if multiple members in a JSON object have the same name (for example `{"a": 1, "a": 2}`)
    /// this method will only seek to the first occurrence in that object ignoring the others.
    ///
    /// Once this method returns, the reader is at the original nesting level again and can continue
    /// consuming values there. Calling this method therefore acts as if it only consumed one value
    /// (and its nested values) at the that level.
    ///
    /// If the structure of the JSON data does not match the path, for example the JSON data contains
    /// an array but the path expects an object, an error is returned.
    ///
    /// For seeking to and reading a single value at a fixed path prefer [`read_seeked`](Self::read_seeked).
    ///
    /// # Examples
    /// ```
    /// # use struson::reader::simple::*;
    /// # use struson::reader::simple::multi_json_path::*;
    /// let json = r#"{
    ///     "users": [
    ///         {"name": "John", "age": 32},
    ///         {"name": "Jane", "age": 41}
    ///     ]
    /// }"#;
    /// let json_reader = SimpleJsonReader::new(json.as_bytes());
    ///
    /// let mut ages = Vec::<u32>::new();
    /// // Select the ages of all users
    /// let json_path = multi_json_path!["users", [*], "age"];
    /// json_reader.read_seeked_multi(&json_path, false, |value_reader| {
    ///     let age = value_reader.read_number()??;
    ///     ages.push(age);
    ///     Ok(())
    /// })?;
    /// assert_eq!(ages, vec![32, 41]);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    fn read_seeked_multi(
        self,
        path: &MultiJsonPath,
        at_least_one_match: bool,
        f: impl FnMut(SingleValueReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>>;
}

fn read_array<J: JsonReader, T>(
    json_reader: &mut ErrorSafeJsonReader<J>,
    f: impl FnOnce(&mut ArrayReader<'_, J>) -> Result<T, Box<dyn Error>>,
) -> Result<T, Box<dyn Error>> {
    json_reader.begin_array()?;
    let mut array_reader = ArrayReader { json_reader };
    let result = f(&mut array_reader)?;
    json_reader.end_array()?;
    Ok(result)
}

fn read_object_borrowed_names<J: JsonReader>(
    json_reader: &mut ErrorSafeJsonReader<J>,
    mut f: impl FnMut(MemberReader<'_, J>) -> Result<(), Box<dyn Error>>,
) -> Result<(), Box<dyn Error>> {
    json_reader.begin_object()?;
    while json_reader.has_next()? {
        let mut consumed_name = false;
        let mut consumed_value = false;
        let member_reader = MemberReader {
            json_reader,
            consumed_name: &mut consumed_name,
            consumed_value: &mut consumed_value,
        };
        f(member_reader)?;
        // If the function did not consume the member name or value, skip them
        if !consumed_name {
            json_reader.skip_name()?;
        }
        if !consumed_value {
            json_reader.skip_value()?;
        }
    }
    json_reader.end_object()?;
    Ok(())
}

fn read_object_owned_names<J: JsonReader>(
    json_reader: &mut ErrorSafeJsonReader<J>,
    mut f: impl FnMut(String, SingleValueReader<'_, J>) -> Result<(), Box<dyn Error>>,
) -> Result<(), Box<dyn Error>> {
    json_reader.begin_object()?;
    while json_reader.has_next()? {
        let name = json_reader.next_name_owned()?;
        let mut consumed_value = false;
        let member_value_reader = SingleValueReader {
            json_reader,
            consumed_value: &mut consumed_value,
        };
        f(name, member_value_reader)?;
        // If the function did not consume the value, skip it
        if !consumed_value {
            json_reader.skip_value()?;
        }
    }
    json_reader.end_object()?;
    Ok(())
}

fn read_string_with_reader<J: JsonReader, T>(
    json_reader: &mut ErrorSafeJsonReader<J>,
    f: impl FnOnce(StringValueReader<'_>) -> Result<T, Box<dyn Error>>,
) -> Result<T, Box<dyn Error>> {
    let mut delegate = json_reader.next_string_reader()?;
    let string_reader: StringValueReader<'_> = StringValueReader {
        delegate: &mut delegate as &mut dyn Read,
    };
    let result = f(string_reader)?;

    // Check if there is error which was not propagated by function
    if let Some(error) = delegate.error {
        return Err(error.rough_clone().into());
    }

    // Skip remaining bytes, if any
    // First check with small buffer if there are any bytes to skip
    // TODO: Should this retry on `ErrorKind::Interrupted`?
    if delegate.read(&mut [0_u8; utf8::MAX_BYTES_PER_CHAR])? > 0 {
        // Then use larger buffer for skipping
        let mut buf = [0_u8; 512];

        while delegate.read(&mut buf)? > 0 {
            // Do nothing
        }
    }

    Ok(result)
}

/// Reads a value with `f`, implicitly skipping the value if `f` did not consume it
fn read_value<J: JsonReader, T>(
    json_reader: &mut ErrorSafeJsonReader<J>,
    f: impl FnOnce(SingleValueReader<'_, J>) -> Result<T, Box<dyn Error>>,
) -> Result<T, Box<dyn Error>> {
    let mut consumed_value = false;
    let value_reader = SingleValueReader {
        json_reader,
        consumed_value: &mut consumed_value,
    };

    let result = f(value_reader)?;
    // If the function did not consume the value, skip it
    if !consumed_value {
        json_reader.skip_value()?;
    }
    Ok(result)
}

fn read_seeked<J: JsonReader, T>(
    json_reader: &mut ErrorSafeJsonReader<J>,
    path: &JsonPath,
    f: impl FnOnce(SingleValueReader<'_, J>) -> Result<T, Box<dyn Error>>,
) -> Result<T, Box<dyn Error>> {
    json_reader.seek_to(path)?;
    let result = read_value(json_reader, f)?;
    json_reader.seek_back(path)?;
    Ok(result)
}

fn read_seeked_multi<J: JsonReader>(
    json_reader: &mut ErrorSafeJsonReader<J>,
    original_path: &MultiJsonPath,
    require_at_least_one_match: bool,
    mut f: impl FnMut(SingleValueReader<'_, J>) -> Result<(), Box<dyn Error>>,
) -> Result<(), Box<dyn Error>> {
    // Special case for empty path because the logic below only handles non-empty paths
    if original_path.is_empty() {
        return read_value(json_reader, &mut f);
    }

    enum PartialPath {
        JsonPath(Vec<JsonPathPiece>),
        AllArrayItems { allow_empty: bool },
        OptionalObjectMember(String),
        AllObjectMembers { allow_empty: bool },
    }

    /// Converts the path to an internal representation where 'plain' path pieces (i.e. no wildcards, ...)
    /// are merged together to a regular JSON path
    fn convert_path(path: &MultiJsonPath) -> Vec<PartialPath> {
        let mut converted_path = Vec::<PartialPath>::new();
        let mut current_section = None;

        for piece in path {
            match piece {
                MultiJsonPathPiece::ArrayItem(index) => {
                    let current_section = current_section.get_or_insert_with(Vec::new);
                    current_section.push(JsonPathPiece::ArrayItem(*index));
                }
                MultiJsonPathPiece::ObjectMember(name) => {
                    let current_section = current_section.get_or_insert_with(Vec::new);
                    current_section.push(JsonPathPiece::ObjectMember(name.clone()));
                }
                MultiJsonPathPiece::AllArrayItems { allow_empty } => {
                    if let Some(current_section) = current_section.take() {
                        converted_path.push(PartialPath::JsonPath(current_section));
                    }
                    converted_path.push(PartialPath::AllArrayItems {
                        allow_empty: *allow_empty,
                    });
                }
                MultiJsonPathPiece::OptionalObjectMember(name) => {
                    if let Some(current_section) = current_section.take() {
                        converted_path.push(PartialPath::JsonPath(current_section));
                    }
                    converted_path.push(PartialPath::OptionalObjectMember(name.clone()));
                }
                MultiJsonPathPiece::AllObjectMembers { allow_empty } => {
                    if let Some(current_section) = current_section.take() {
                        converted_path.push(PartialPath::JsonPath(current_section));
                    }
                    converted_path.push(PartialPath::AllObjectMembers {
                        allow_empty: *allow_empty,
                    });
                }
            }
        }

        if let Some(current_section) = current_section {
            converted_path.push(PartialPath::JsonPath(current_section));
        }
        converted_path
    }

    // Get the original location for error reporting
    let original_location = if require_at_least_one_match {
        // Include the JSON path, assuming that users of the Simple API prefer easier troubleshooting over performance
        Some(json_reader.current_position(true))
    } else {
        None
    };

    let path = convert_path(original_path);
    let mut path_index = 0;
    let mut has_match = false;

    // Whether the path is currently traversed downwards, i.e. going to deeper nesting levels
    // If `false` it is either staying at the same level or moving upwards towards top level
    let mut is_moving_down = true;
    loop {
        match &path[path_index] {
            PartialPath::JsonPath(path) => {
                if is_moving_down {
                    json_reader.seek_to(path)?;
                    path_index += 1;
                    // Continue moving down towards end of path
                } else {
                    json_reader.seek_back(path)?;
                    // Continue moving up towards top level
                }
            }
            PartialPath::AllArrayItems { allow_empty } => {
                if is_moving_down {
                    json_reader.begin_array()?;

                    // Check if array is allowed to be empty
                    // Only need to check this for `is_moving_down == true`, because otherwise at least one
                    // array item had already been read
                    if !allow_empty && !json_reader.has_next()? {
                        /*
                         * TODO: Use custom error; relying on `peek()` to trigger error might be too brittle?
                         * Would have to store it in `ErrorSafeJsonReader` then though
                         */
                        return Err(json_reader
                            // Call `peek()` to trigger a `FewerElementsThanExpected` error (since `has_next()` was `false`)
                            .peek()
                            .expect_err("should have failed with `FewerElementsThanExpected`")
                            .into());
                    }
                }

                if json_reader.has_next()? {
                    is_moving_down = true;
                    path_index += 1;
                } else {
                    json_reader.end_array()?;
                    // Move up
                    is_moving_down = false;
                }
            }
            PartialPath::OptionalObjectMember(name) => {
                if is_moving_down {
                    json_reader.begin_object()?;
                    let mut found_member = false;
                    while json_reader.has_next()? {
                        if json_reader.next_name()? == name {
                            found_member = true;
                            path_index += 1;
                            // Continue moving down towards end of path
                            break;
                        } else {
                            json_reader.skip_value()?;
                        }
                    }

                    if !found_member {
                        json_reader.end_object()?;
                        // Move up
                        is_moving_down = false;
                    }
                } else {
                    // Skip remaining members; ignore if there is duplicate member with same name
                    while json_reader.has_next()? {
                        json_reader.skip_name()?;
                        json_reader.skip_value()?;
                    }
                    json_reader.end_object()?;
                    // Continue moving up towards top level
                }
            }
            PartialPath::AllObjectMembers { allow_empty } => {
                if is_moving_down {
                    json_reader.begin_object()?;

                    // Check if object is allowed to be empty
                    // Only need to check this for `is_moving_down == true`, because otherwise at least one
                    // object member had already been read
                    if !allow_empty && !json_reader.has_next()? {
                        /*
                         * TODO: Use custom error; relying on `skip_name()` to trigger error might be too brittle?
                         * Would have to store it in `ErrorSafeJsonReader` then though
                         */
                        return Err(json_reader
                            // Call `skip_name()` to trigger a `FewerElementsThanExpected` error (since `has_next()` was `false`)
                            .skip_name()
                            .expect_err("should have failed with `FewerElementsThanExpected`")
                            .into());
                    }
                }

                if json_reader.has_next()? {
                    json_reader.skip_name()?;
                    is_moving_down = true;
                    path_index += 1;
                } else {
                    json_reader.end_object()?;
                    // Move up
                    is_moving_down = false;
                }
            }
        }

        // Check if the desired value was found
        if is_moving_down && path_index >= path.len() {
            has_match = true;

            read_value(json_reader, &mut f)?;

            match path[path_index - 1] {
                PartialPath::JsonPath(_) | PartialPath::OptionalObjectMember(_) => {}
                // For trailing `AllArrayItems` and `AllObjectMembers` process all their remaining values here;
                // alternatively could also not do anything special here but just let main loop reach the
                // `read_value` call above again, but that might be less efficient
                PartialPath::AllArrayItems { .. } => {
                    // Read all array items
                    while json_reader.has_next()? {
                        read_value(json_reader, &mut f)?;
                    }
                    // Don't call `end_array` yet; main loop will do that at the beginning again
                }
                PartialPath::AllObjectMembers { .. } => {
                    // Read all object members
                    while json_reader.has_next()? {
                        json_reader.skip_name()?;
                        read_value(json_reader, &mut f)?;
                    }
                    // Don't call `end_object` yet; main loop will do that at the beginning again
                }
            }

            // Move up
            is_moving_down = false;
        }

        // Handle moving up towards top level
        if !is_moving_down {
            if path_index == 0 {
                // Finished seeking
                break;
            }
            path_index -= 1;
        }
    }

    if require_at_least_one_match && !has_match {
        // TODO: Should this rather use the notation used by `multi_json_path!` macro?
        let formatted_path = original_path
            .iter()
            .map(|p| match p {
                MultiJsonPathPiece::ArrayItem(index) => format!("[{index}]"),
                MultiJsonPathPiece::AllArrayItems { allow_empty } => {
                    // Note: The `[+]` syntax is not part of the official JSONPath specification
                    if *allow_empty { "[*]" } else { "[+]" }.to_owned()
                }
                MultiJsonPathPiece::ObjectMember(name) => format!(".{name}"),
                // Note: This `.name?` syntax is not part of the official JSONPath specification
                MultiJsonPathPiece::OptionalObjectMember(name) => format!(".{name}?"),
                MultiJsonPathPiece::AllObjectMembers { allow_empty } => {
                    // Note: The `.+` syntax is not part of the official JSONPath specification
                    if *allow_empty { ".*" } else { ".+" }.to_owned()
                }
            })
            .collect::<String>();

        // Report the original location where seeking started because there is no single
        // location where the path had no match, instead the path as a whole had no match
        let original_location = original_location.unwrap();
        if json_reader.error.is_none() {
            // Use dummy error because cannot preserve original error, and because error
            // is specific to `read_seeked_multi` and the provided path, and it would be
            // confusing to repeat it for unrelated subsequent reader calls
            json_reader.error = Some(create_dummy_error(&original_location));
        }
        // TODO Proper error type?
        Err(
            format!("no matching value found for path '{formatted_path}' at {original_location}")
                .into(),
        )
    } else {
        Ok(())
    }
}

mod error_safe_reader {
    use super::*;

    type IoError = std::io::Error;

    pub(super) fn clone_original_io_error(error: &IoError) -> IoError {
        // Report as `Other` kind (and with custom message) to avoid caller indefinitely retrying
        // because it considers the original error kind as safe to retry
        #[expect(
            clippy::to_string_in_format_args,
            reason = "make it explicit that this uses `to_string()`"
        )]
        IoError::other(format!(
            "previous error '{}': {}",
            error.kind(),
            error.to_string()
        ))
    }

    /// Creates a [`ReaderError`] for situations where the original error cannot be preserved
    pub(super) fn create_dummy_error(location: &JsonReaderPosition) -> ReaderError {
        ReaderError::SyntaxError(JsonSyntaxError {
            // This error kind does not suit that well, but the other kinds suit even worse
            // Mainly have to return 'any' error; users should not rely on this safeguard in the first place
            kind: SyntaxErrorKind::IncompleteDocument,
            location: location.clone(),
        })
    }

    /// Creates a dummy [`ReaderError`] with unknown position
    fn create_unknown_pos_error() -> ReaderError {
        create_dummy_error(&JsonReaderPosition::unknown_position())
    }

    /// If previously an error had occurred, returns that error. Otherwise uses the delegate
    /// reader and in case of an error stores it to prevent subsequent usage.
    /* This is a macro instead of a function for methods returning `&str`, and to support error conversion */
    macro_rules! use_delegate {
        ($self:ident, |$json_reader:ident| $reading_action:expr, |$original_error:ident| $original_error_converter:expr, |$stored_error:ident| $stored_error_converter:expr) => {
            if let Some(error) = &$self.error {
                let $stored_error = error.rough_clone();
                Err($stored_error_converter)
            } else {
                let $json_reader = &mut $self.delegate;
                let result = $reading_action;
                if let Err($original_error) = &result {
                    $self.error = Some($original_error_converter);
                }
                result
            }
        };
        ($self:ident, |$json_reader:ident| $reading_action:expr) => {
            use_delegate!(
                $self,
                |$json_reader| $reading_action,
                |original_error| {
                    match original_error {
                        // Note: List all error types instead of using a 'catch-all' to explicitly decide for each the
                        // correct handling, especially when future error types are being added
                        e @ ReaderError::SyntaxError(_) => e.rough_clone(),
                        e @ ReaderError::MaxNestingDepthExceeded { .. } => e.rough_clone(),
                        e @ ReaderError::UnsupportedNumberValue { .. } => e.rough_clone(),
                        ReaderError::IoError { error, location } => ReaderError::IoError {
                            error: clone_original_io_error(error),
                            location: location.clone(),
                        },
                        // For these repeating the error might be confusing, e.g. when a subsequent call performs a completely unrelated action,
                        // therefore use a dummy error
                        // Technically `JsonReader` allows retrying for these errors, but that would be error-prone when they occurred during a
                        // `seek_to` or similar where the reader position is uncertain afterwards; therefore don't allow retrying
                        ReaderError::UnexpectedValueType { location, .. } => create_dummy_error(location),
                        ReaderError::UnexpectedStructure { location, .. } => create_dummy_error(location),
                    }
                },
                |stored_error| stored_error
            )
        };
    }

    /// [`JsonReader`] implementation which in case of errors keeps returning the error and does
    /// not use the underlying JSON reader anymore
    ///
    /// This is mainly to protect against user-provided closures or functions which accidentally
    /// discard and not propagate reader errors, which could lead to subsequent panics. How exactly
    /// this reader repeats errors or what information it preserves is unspecified; users should
    /// always propagate reader errors and not (intentionally) rely on this safeguard here.
    #[derive(Debug)]
    pub(super) struct ErrorSafeJsonReader<J: JsonReader> {
        pub(super) delegate: J,
        pub(super) error: Option<ReaderError>,
    }

    impl<J: JsonReader> JsonReader for ErrorSafeJsonReader<J> {
        fn peek(&mut self) -> Result<ValueType, ReaderError> {
            use_delegate!(self, |r| r.peek())
        }

        fn begin_object(&mut self) -> Result<(), ReaderError> {
            use_delegate!(self, |r| r.begin_object())
        }

        fn end_object(&mut self) -> Result<(), ReaderError> {
            use_delegate!(self, |r| r.end_object())
        }

        fn begin_array(&mut self) -> Result<(), ReaderError> {
            use_delegate!(self, |r| r.begin_array())
        }

        fn end_array(&mut self) -> Result<(), ReaderError> {
            use_delegate!(self, |r| r.end_array())
        }

        fn has_next(&mut self) -> Result<bool, ReaderError> {
            use_delegate!(self, |r| r.has_next())
        }

        fn next_name(&mut self) -> Result<&str, ReaderError> {
            use_delegate!(self, |r| r.next_name())
        }

        fn next_name_owned(&mut self) -> Result<String, ReaderError> {
            use_delegate!(self, |r| r.next_name_owned())
        }

        fn next_str(&mut self) -> Result<&str, ReaderError> {
            use_delegate!(self, |r| r.next_str())
        }

        fn next_string(&mut self) -> Result<String, ReaderError> {
            use_delegate!(self, |r| r.next_string())
        }

        #[expect(
            refining_impl_trait,
            reason = "this `JsonReader` impl is for an internal struct, so should not cause issues?"
        )]
        fn next_string_reader(
            &mut self,
        ) -> Result<ErrorSafeStringValueReader<'_, impl Read>, ReaderError> {
            let string_reader_delegate = use_delegate!(self, |r| r.next_string_reader())?;
            Ok(ErrorSafeStringValueReader {
                delegate: string_reader_delegate,
                // Store errors in the `error` field of this `ErrorSafeStringValueReader`, so they are
                // available to the original JsonReader afterwards
                error: &mut self.error,
            })
        }

        fn next_number_as_str(&mut self) -> Result<&str, ReaderError> {
            use_delegate!(self, |r| r.next_number_as_str())
        }

        fn next_number_as_string(&mut self) -> Result<String, ReaderError> {
            use_delegate!(self, |r| r.next_number_as_string())
        }

        fn next_number<T: FromStr>(&mut self) -> Result<Result<T, T::Err>, ReaderError> {
            use_delegate!(self, |r| r.next_number())
        }

        fn next_bool(&mut self) -> Result<bool, ReaderError> {
            use_delegate!(self, |r| r.next_bool())
        }

        fn next_null(&mut self) -> Result<(), ReaderError> {
            use_delegate!(self, |r| r.next_null())
        }

        #[cfg(feature = "serde")]
        fn deserialize_next<'de, D: serde_core::de::Deserialize<'de>>(
            &mut self,
        ) -> Result<D, crate::serde::DeserializerError> {
            use crate::serde::DeserializerError;

            use_delegate!(
                self,
                |r| r.deserialize_next(),
                |original_error| {
                    match original_error {
                        DeserializerError::ReaderError(e) => e.rough_clone(),
                        // Cannot easily preserve information for these errors
                        DeserializerError::Custom(_) => create_unknown_pos_error(),
                        DeserializerError::MaxNestingDepthExceeded(_) => create_unknown_pos_error(),
                        DeserializerError::InvalidNumber(_) => create_unknown_pos_error(),
                    }
                },
                |stored_error| DeserializerError::ReaderError(stored_error)
            )
        }

        fn skip_name(&mut self) -> Result<(), ReaderError> {
            use_delegate!(self, |r| r.skip_name())
        }

        fn skip_value(&mut self) -> Result<(), ReaderError> {
            use_delegate!(self, |r| r.skip_value())
        }

        fn skip_to_top_level(&mut self) -> Result<(), ReaderError> {
            use_delegate!(self, |r| r.skip_to_top_level())
        }

        fn transfer_to<W: JsonWriter>(&mut self, json_writer: &mut W) -> Result<(), TransferError> {
            use_delegate!(
                self,
                |r| r.transfer_to(json_writer),
                |original_error| match original_error {
                    TransferError::ReaderError(e) => e.rough_clone(),
                    // Cannot easily preserve this, and reporting it as reader IO error might be confusing
                    TransferError::WriterError(_) => create_unknown_pos_error(),
                },
                |stored_error| TransferError::ReaderError(stored_error)
            )
        }

        fn current_position(&self, include_path: bool) -> JsonReaderPosition {
            // Permit calling this even if error occurred before
            self.delegate.current_position(include_path)
        }

        fn seek_to(&mut self, rel_json_path: &JsonPath) -> Result<(), ReaderError> {
            use_delegate!(self, |r| r.seek_to(rel_json_path))
        }

        fn seek_back(&mut self, rel_json_path: &JsonPath) -> Result<(), ReaderError> {
            use_delegate!(self, |r| r.seek_back(rel_json_path))
        }

        fn consume_trailing_whitespace(self) -> Result<(), ReaderError> {
            // Special code instead of `use_delegate!(...)` because this method consumes `self`
            if let Some(error) = self.error {
                return Err(error);
            }
            self.delegate.consume_trailing_whitespace()
        }
    }

    // Note: Repeating errors here might be a bit redundant if JsonStreamReader is used since it does
    // this itself as well; but that is not guaranteed for all JsonReader implementations
    pub(super) struct ErrorSafeStringValueReader<'a, D: Read> {
        delegate: D,
        pub(super) error: &'a mut Option<ReaderError>,
    }
    impl<D: Read> ErrorSafeStringValueReader<'_, D> {
        fn use_delegate<T>(
            &mut self,
            f: impl FnOnce(&mut D) -> std::io::Result<T>,
        ) -> std::io::Result<T> {
            if let Some(error) = &self.error {
                return Err(match error.rough_clone() {
                    // Ignore `location`; assuming that IO error was set here by ErrorSafeStringValueReader
                    // and therefore no real location exists (or original location would already be included
                    // in error message)
                    ReaderError::IoError { error, .. } => error,
                    // Error should have only been set by ErrorSafeStringValueReader, and therefore be IoError;
                    // any previous other error type should have prevented creation of ErrorSafeStringValueReader
                    e => unreachable!("unexpected error type: {e:?}"),
                });
            }

            let result = f(&mut self.delegate);
            if let Err(error) = &result {
                *self.error = Some(ReaderError::IoError {
                    error: clone_original_io_error(error),
                    location: JsonReaderPosition::unknown_position(),
                });
            }
            result
        }
    }
    impl<D: Read> Read for ErrorSafeStringValueReader<'_, D> {
        fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
            self.use_delegate(|d| d.read(buf))
        }
    }
}

/// JSON reader variant which is easier to use than [`JsonReader`]
///
/// This JSON reader variant ensures correct usage at compile-time making it easier and less
/// error-prone to use than [`JsonReader`], which validates correct usage at runtime and panics
/// on incorrect usage. However, this comes at the cost of `SimpleJsonReader` being less flexible
/// to use, and it not offering all features of [`JsonReader`].
///
/// When an error is returned by one of the methods of the reader, the error should be propagated
/// (for example by using Rust's `?` operator), processing should be aborted and the reader should
/// not be used any further.
///
/// # Examples
/// ```
/// # use struson::reader::simple::*;
/// // In this example JSON data comes from a string;
/// // normally it would come from a file or a network connection
/// let json_reader = SimpleJsonReader::new(r#"["a", "short", "example"]"#.as_bytes());
/// let mut words = Vec::<String>::new();
/// json_reader.read_array_items(|item_reader| {
///     let word = item_reader.read_string()?;
///     words.push(word);
///     Ok(())
/// })?;
/// assert_eq!(words, vec!["a", "short", "example"]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug)]
pub struct SimpleJsonReader<J: JsonReader> {
    json_reader: ErrorSafeJsonReader<J>,
    /// Whether [`seek_to`] has been used
    has_seeked: bool,
}
impl<J: JsonReader> SimpleJsonReader<J> {
    /// Creates a new `SimpleJsonReader` from the given [`JsonReader`]
    ///
    /// The `JsonReader` acts as delegate which performs the actual JSON reading. It should
    /// be positioned at the start of the top-level JSON value and should not have consumed
    /// any data yet, otherwise the behavior of the created `SimpleJsonReader` is unspecified
    /// and it may panic.
    ///
    /// # Examples
    /// ```
    /// # use struson::reader::*;
    /// # use struson::reader::simple::*;
    /// let json = r"
    ///     [
    ///         1,
    ///         2,
    ///     ]
    ///     ".as_bytes();
    ///
    /// # #[expect(unused_variables)]
    /// let json_reader = SimpleJsonReader::from_json_reader(
    ///     JsonStreamReader::new_custom(
    ///         json,
    ///         ReaderSettings {
    ///             allow_trailing_comma: true,
    ///             // For all other settings use the default
    ///             ..Default::default()
    ///         },
    ///     )
    /// );
    /// ```
    pub fn from_json_reader(json_reader: J) -> Self {
        SimpleJsonReader {
            json_reader: ErrorSafeJsonReader {
                delegate: json_reader,
                error: None,
            },
            has_seeked: false,
        }
    }

    /// Seeks to the specified location in the JSON document
    ///
    /// Seeks to the specified relative JSON path in the JSON document by skipping all previous
    /// values. Once this method returns successfully the reader will be positioned before the
    /// last element specified by the path.
    ///
    /// For example for the JSON path `json_path!["foo", 2]` it will start consuming a JSON object,
    /// skipping members until it finds one with name "foo". Then it starts consuming the member
    /// value, expecting that it is a JSON array, until right before the array item with (starting
    /// at 0) index 2. If multiple members in a JSON object have the same name (for example
    /// `{"a": 1, "a": 2}`) this method will seek to the first occurrence.
    ///
    /// Seeking to a specific location can be useful when parts of the processed JSON document
    /// are not relevant for the application processing it.
    ///
    /// If the structure of the JSON data does not match the path, for example when the JSON data
    /// contains an array but the path expects an object, an error is returned.
    ///
    /// The seeking behavior of this method is equivalent to [`read_seeked`](Self::read_seeked), but
    /// `seek_to` allows consuming the value afterwards without having to use a closure or separate
    /// function (as required by `read_seeked`), however it is only available for `SimpleJsonReader`
    /// and not generally for the `ValueReader` trait.
    ///
    /// # Examples
    /// ```
    /// # use struson::reader::simple::*;
    /// # use struson::reader::json_path::*;
    /// let mut json_reader = SimpleJsonReader::new(
    ///     r#"{"bar": true, "foo": ["a", "b", "c"]}"#.as_bytes()
    /// );
    /// json_reader.seek_to(&json_path!["foo", 2])?;
    ///
    /// // Can now consume the value to which the call seeked to
    /// let value = json_reader.read_string()?;
    /// assert_eq!(value, "c");
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    /*
     * Only provided for top-level reader but not for nested arrays or objects, because they implicitly
     * close the open arrays and objects, which would panic because `seek_to` opens additional arrays
     * and objects. Maybe most of the `ValueReader` implementations here could track that and let the
     * caller close the arrays and objects, but for `ArrayReader` it would cause problems because seeking
     * would break its `has_next` method, which expects to always be inside the original array.
     */
    pub fn seek_to(&mut self, path: &JsonPath) -> Result<(), ReaderError> {
        self.has_seeked = true;
        self.json_reader.seek_to(path)
    }

    fn finish(mut self) -> Result<(), ReaderError> {
        // TODO: Or should for `has_seeked` simply stop processing without any skipping and without
        //       call to `consume_trailing_whitespace`?
        if self.has_seeked {
            self.json_reader.skip_to_top_level()?;
        }
        self.json_reader.consume_trailing_whitespace()
    }
}
impl<R: Read> SimpleJsonReader<JsonStreamReader<R>> {
    /// Creates a new JSON reader
    ///
    /// The given reader must provide the JSON data UTF-8 encoded, without leading byte order mark (BOM).
    /// Internally this creates a [`JsonStreamReader`] which acts as delegate; see its documentation for
    /// more information about the parsing behavior and security considerations.
    pub fn new(reader: R) -> Self {
        SimpleJsonReader::from_json_reader(JsonStreamReader::new(reader))
    }
}

// These methods all call `json_reader.consume_trailing_whitespace()` because `SimpleJsonReader`
// is intended to be used for top-level value
impl<J: JsonReader> ValueReader<J> for SimpleJsonReader<J> {
    fn peek_value(&mut self) -> Result<ValueType, ReaderError> {
        self.json_reader.peek()
    }

    fn read_null(mut self) -> Result<(), ReaderError> {
        self.json_reader.next_null()?;
        self.finish()
    }

    fn read_bool(mut self) -> Result<bool, ReaderError> {
        let result = self.json_reader.next_bool()?;
        self.finish()?;
        Ok(result)
    }

    fn read_str<T>(
        mut self,
        f: impl FnOnce(&str) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        let result = f(self.json_reader.next_str()?)?;
        self.finish()?;
        Ok(result)
    }

    fn read_string(mut self) -> Result<String, ReaderError> {
        let result = self.json_reader.next_string()?;
        self.finish()?;
        Ok(result)
    }

    fn read_string_with_reader<T>(
        mut self,
        f: impl FnOnce(StringValueReader<'_>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        let result = read_string_with_reader(&mut self.json_reader, f)?;
        self.finish()?;
        Ok(result)
    }

    fn read_number<T: FromStr>(mut self) -> Result<Result<T, T::Err>, ReaderError> {
        let result = self.json_reader.next_number()?;
        self.finish()?;
        Ok(result)
    }

    fn read_number_as_string(mut self) -> Result<String, ReaderError> {
        let result = self.json_reader.next_number_as_string()?;
        self.finish()?;
        Ok(result)
    }

    #[cfg(feature = "serde")]
    fn read_deserialize<'de, D: serde_core::de::Deserialize<'de>>(
        mut self,
    ) -> Result<D, crate::serde::DeserializerError> {
        let result = self.json_reader.deserialize_next()?;
        self.finish()?;
        Ok(result)
    }

    fn skip_value(mut self) -> Result<(), ReaderError> {
        self.json_reader.skip_value()?;
        self.finish()
    }

    fn read_array<T>(
        mut self,
        f: impl FnOnce(&mut ArrayReader<'_, J>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        let result = read_array(&mut self.json_reader, f)?;
        self.finish()?;
        Ok(result)
    }

    fn read_object_borrowed_names(
        mut self,
        f: impl FnMut(MemberReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>> {
        read_object_borrowed_names(&mut self.json_reader, f)?;
        self.finish()?;
        Ok(())
    }

    fn read_object_owned_names(
        mut self,
        f: impl FnMut(String, SingleValueReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>> {
        read_object_owned_names(&mut self.json_reader, f)?;
        self.finish()?;
        Ok(())
    }

    fn read_seeked<T>(
        mut self,
        path: &JsonPath,
        f: impl FnOnce(SingleValueReader<'_, J>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        let result = read_seeked(&mut self.json_reader, path, f)?;
        self.finish()?;
        Ok(result)
    }

    fn read_seeked_multi(
        mut self,
        path: &MultiJsonPath,
        at_least_one_match: bool,
        f: impl FnMut(SingleValueReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>> {
        read_seeked_multi(&mut self.json_reader, path, at_least_one_match, f)?;
        self.finish()?;
        Ok(())
    }
}

/// Reader for an arbitrary amount of JSON array items
///
/// This struct is used by [`ValueReader::read_array`].
#[derive(Debug)]
pub struct ArrayReader<'a, J: JsonReader> {
    json_reader: &'a mut ErrorSafeJsonReader<J>,
}
impl<J: JsonReader> ArrayReader<'_, J> {
    /// Checks if there is a next item in the JSON array, without consuming it
    ///
    /// Returns `true` if there is a next item, `false` otherwise. This method can be
    /// useful as condition of a `while` loop when processing a JSON array of unknown size.
    ///
    /// # Examples
    /// ```
    /// # use struson::reader::simple::*;
    /// let json_reader = SimpleJsonReader::new("[1, 2]".as_bytes());
    /// json_reader.read_array(|array_reader| {
    ///     while array_reader.has_next()? {
    ///         let value = array_reader.read_number_as_string()?;
    ///         println!("{value}");
    ///     }
    /// #   assert!(!array_reader.has_next()?);
    ///     Ok(())
    /// })?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn has_next(&mut self) -> Result<bool, ReaderError> {
        self.json_reader.has_next()
    }
}

// Implement for `&mut ArrayReader` to allow reading multiple values instead of just one
impl<J: JsonReader> ValueReader<J> for &mut ArrayReader<'_, J> {
    fn peek_value(&mut self) -> Result<ValueType, ReaderError> {
        self.json_reader.peek()
    }

    fn read_null(self) -> Result<(), ReaderError> {
        self.json_reader.next_null()
    }

    fn read_bool(self) -> Result<bool, ReaderError> {
        self.json_reader.next_bool()
    }

    fn read_str<T>(
        self,
        f: impl FnOnce(&str) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        f(self.json_reader.next_str()?)
    }

    fn read_string(self) -> Result<String, ReaderError> {
        self.json_reader.next_string()
    }

    fn read_string_with_reader<T>(
        self,
        f: impl FnOnce(StringValueReader<'_>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        read_string_with_reader(self.json_reader, f)
    }

    fn read_number<T: FromStr>(self) -> Result<Result<T, T::Err>, ReaderError> {
        self.json_reader.next_number()
    }

    fn read_number_as_string(self) -> Result<String, ReaderError> {
        self.json_reader.next_number_as_string()
    }

    #[cfg(feature = "serde")]
    fn read_deserialize<'de, D: serde_core::de::Deserialize<'de>>(
        self,
    ) -> Result<D, crate::serde::DeserializerError> {
        self.json_reader.deserialize_next()
    }

    fn skip_value(self) -> Result<(), ReaderError> {
        self.json_reader.skip_value()
    }

    fn read_array<T>(
        self,
        f: impl FnOnce(&mut ArrayReader<'_, J>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        read_array(self.json_reader, f)
    }

    fn read_object_borrowed_names(
        self,
        f: impl FnMut(MemberReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>> {
        read_object_borrowed_names(self.json_reader, f)
    }

    fn read_object_owned_names(
        self,
        f: impl FnMut(String, SingleValueReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>> {
        read_object_owned_names(self.json_reader, f)
    }

    fn read_seeked<T>(
        self,
        path: &JsonPath,
        f: impl FnOnce(SingleValueReader<'_, J>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        read_seeked(self.json_reader, path, f)
    }

    fn read_seeked_multi(
        self,
        path: &MultiJsonPath,
        at_least_one_match: bool,
        f: impl FnMut(SingleValueReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>> {
        read_seeked_multi(self.json_reader, path, at_least_one_match, f)
    }
}

/// Reader for a single JSON value
///
/// This struct is used by
/// - [`ValueReader::read_array_items`]
/// - [`ValueReader::read_object_owned_names`]
/// - [`ValueReader::read_seeked_multi`]
#[derive(Debug)]
pub struct SingleValueReader<'a, J: JsonReader> {
    json_reader: &'a mut ErrorSafeJsonReader<J>,
    consumed_value: &'a mut bool,
}
impl<J: JsonReader> ValueReader<J> for SingleValueReader<'_, J> {
    fn peek_value(&mut self) -> Result<ValueType, ReaderError> {
        self.json_reader.peek()
    }

    fn read_null(self) -> Result<(), ReaderError> {
        *self.consumed_value = true;
        self.json_reader.next_null()
    }

    fn read_bool(self) -> Result<bool, ReaderError> {
        *self.consumed_value = true;
        self.json_reader.next_bool()
    }

    fn read_str<T>(
        self,
        f: impl FnOnce(&str) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        *self.consumed_value = true;
        f(self.json_reader.next_str()?)
    }

    fn read_string(self) -> Result<String, ReaderError> {
        *self.consumed_value = true;
        self.json_reader.next_string()
    }

    fn read_string_with_reader<T>(
        self,
        f: impl FnOnce(StringValueReader<'_>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        *self.consumed_value = true;
        read_string_with_reader(self.json_reader, f)
    }

    fn read_number<T: FromStr>(self) -> Result<Result<T, T::Err>, ReaderError> {
        *self.consumed_value = true;
        self.json_reader.next_number()
    }

    fn read_number_as_string(self) -> Result<String, ReaderError> {
        *self.consumed_value = true;
        self.json_reader.next_number_as_string()
    }

    #[cfg(feature = "serde")]
    fn read_deserialize<'de, D: serde_core::de::Deserialize<'de>>(
        self,
    ) -> Result<D, crate::serde::DeserializerError> {
        *self.consumed_value = true;
        self.json_reader.deserialize_next()
    }

    fn skip_value(self) -> Result<(), ReaderError> {
        *self.consumed_value = true;
        self.json_reader.skip_value()
    }

    fn read_array<T>(
        self,
        f: impl FnOnce(&mut ArrayReader<'_, J>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        *self.consumed_value = true;
        read_array(self.json_reader, f)
    }

    fn read_object_borrowed_names(
        self,
        f: impl FnMut(MemberReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>> {
        *self.consumed_value = true;
        read_object_borrowed_names(self.json_reader, f)
    }

    fn read_object_owned_names(
        self,
        f: impl FnMut(String, SingleValueReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>> {
        *self.consumed_value = true;
        read_object_owned_names(self.json_reader, f)
    }

    fn read_seeked<T>(
        self,
        path: &JsonPath,
        f: impl FnOnce(SingleValueReader<'_, J>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        *self.consumed_value = true;
        read_seeked(self.json_reader, path, f)
    }

    fn read_seeked_multi(
        self,
        path: &MultiJsonPath,
        at_least_one_match: bool,
        f: impl FnMut(SingleValueReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>> {
        *self.consumed_value = true;
        read_seeked_multi(self.json_reader, path, at_least_one_match, f)
    }
}

/// Reader for a JSON object member
///
/// The member name can be read using [`read_name`](Self::read_name), and afterwards
/// the member value can be read using one of the [`ValueReader`] methods.
///
/// This struct is used by [`ValueReader::read_object_borrowed_names`].
#[derive(Debug)]
pub struct MemberReader<'a, J: JsonReader> {
    json_reader: &'a mut ErrorSafeJsonReader<J>,
    consumed_name: &'a mut bool,
    consumed_value: &'a mut bool,
}
impl<J: JsonReader> MemberReader<'_, J> {
    /// Reads the member name
    ///
    /// Panics if called more than once.
    pub fn read_name(&mut self) -> Result<&str, ReaderError> {
        /*
         * Ideally this would not panic but instead keep returning the same reference on
         * subsequent calls
         * In theory this should be safe to implement by storing the reference after the first
         * read in the struct, and due to borrowing rules the member value cannot be consumed
         * while the name from `read_name` is still referenced. Also when the member value is
         * read, `self` is consumed so the name cannot be accessed afterwards anymore.
         *
         * However, the Rust compiler does not seem to permit this, see also https://stackoverflow.com/q/32300132.
         * So for now panic, which should be fine assuming that normally users don't call
         * `read_name` multiple times.
         * But also the method name `read_name` suggests that it advances the reader, so it
         * might be counterintuitive that subsequent calls return a previously read name.
         */
        if *self.consumed_name {
            panic!("name has already been consumed");
        }
        *self.consumed_name = true;
        self.json_reader.next_name()
    }

    fn check_skip_name(&mut self) -> Result<(), ReaderError> {
        if !*self.consumed_name {
            *self.consumed_name = true;
            self.json_reader.skip_name()?;
        }
        Ok(())
    }
}

/// The `ValueReader` methods read the member value
impl<J: JsonReader> ValueReader<J> for MemberReader<'_, J> {
    /// Peeks at the type of the member value, without consuming it
    ///
    /// If [`read_name`](MemberReader::read_name) has not been called yet, the
    /// member name will be skipped implicitly and calling `read_name` will not
    /// be possible anymore afterwards.
    fn peek_value(&mut self) -> Result<ValueType, ReaderError> {
        self.check_skip_name()?;
        self.json_reader.peek()
    }

    fn read_null(mut self) -> Result<(), ReaderError> {
        self.check_skip_name()?;
        *self.consumed_value = true;
        self.json_reader.next_null()
    }

    fn read_bool(mut self) -> Result<bool, ReaderError> {
        self.check_skip_name()?;
        *self.consumed_value = true;
        self.json_reader.next_bool()
    }

    fn read_str<T>(
        mut self,
        f: impl FnOnce(&str) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        self.check_skip_name()?;
        *self.consumed_value = true;
        f(self.json_reader.next_str()?)
    }

    fn read_string(mut self) -> Result<String, ReaderError> {
        self.check_skip_name()?;
        *self.consumed_value = true;
        self.json_reader.next_string()
    }

    fn read_string_with_reader<T>(
        mut self,
        f: impl FnOnce(StringValueReader<'_>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        self.check_skip_name()?;
        *self.consumed_value = true;
        read_string_with_reader(self.json_reader, f)
    }

    fn read_number<T: FromStr>(mut self) -> Result<Result<T, T::Err>, ReaderError> {
        self.check_skip_name()?;
        *self.consumed_value = true;
        self.json_reader.next_number()
    }

    fn read_number_as_string(mut self) -> Result<String, ReaderError> {
        self.check_skip_name()?;
        *self.consumed_value = true;
        self.json_reader.next_number_as_string()
    }

    #[cfg(feature = "serde")]
    fn read_deserialize<'de, D: serde_core::de::Deserialize<'de>>(
        mut self,
    ) -> Result<D, crate::serde::DeserializerError> {
        self.check_skip_name()?;
        *self.consumed_value = true;
        self.json_reader.deserialize_next()
    }

    fn skip_value(mut self) -> Result<(), ReaderError> {
        self.check_skip_name()?;
        *self.consumed_value = true;
        self.json_reader.skip_value()
    }

    fn read_array<T>(
        mut self,
        f: impl FnOnce(&mut ArrayReader<'_, J>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        self.check_skip_name()?;
        *self.consumed_value = true;
        read_array(self.json_reader, f)
    }

    fn read_object_borrowed_names(
        mut self,
        f: impl FnMut(MemberReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>> {
        self.check_skip_name()?;
        *self.consumed_value = true;
        read_object_borrowed_names(self.json_reader, f)
    }

    fn read_object_owned_names(
        mut self,
        f: impl FnMut(String, SingleValueReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>> {
        self.check_skip_name()?;
        *self.consumed_value = true;
        read_object_owned_names(self.json_reader, f)
    }

    fn read_seeked<T>(
        mut self,
        path: &JsonPath,
        f: impl FnOnce(SingleValueReader<'_, J>) -> Result<T, Box<dyn Error>>,
    ) -> Result<T, Box<dyn Error>> {
        self.check_skip_name()?;
        *self.consumed_value = true;
        read_seeked(self.json_reader, path, f)
    }

    fn read_seeked_multi(
        mut self,
        path: &MultiJsonPath,
        at_least_one_match: bool,
        f: impl FnMut(SingleValueReader<'_, J>) -> Result<(), Box<dyn Error>>,
    ) -> Result<(), Box<dyn Error>> {
        self.check_skip_name()?;
        *self.consumed_value = true;
        read_seeked_multi(self.json_reader, path, at_least_one_match, f)
    }
}

/// Reader for a JSON string value
///
/// This struct is used by [`ValueReader::read_string_with_reader`].
///
/// # Errors
/// A [`std::io::Error`] is returned when the underlying reader encounters an error, or
/// when malformed JSON data is encountered.
///
/// # Error behavior
/// The error behavior of this string reader differs from the guarantees made by [`Read`]:
/// - if an error is returned there are no guarantees about if or how many data has been
///   consumed from the underlying data source and been stored in the provided `buf`
/// - if an error occurs, processing should be aborted, regardless of the kind of the error;
///   trying to use this string reader or the original JSON reader afterwards will lead
///   to unspecified behavior
/* TODO: Should implement `Debug`, but seems to not be easily possible when using `dyn` below */
pub struct StringValueReader<'a> {
    // TODO: Ideally would avoid `dyn` here, but using generic type parameter seems to not be
    //   easily possible when `JsonReader::next_string_reader()` does not use associated type
    //   and `FnOnce` cannot use `impl Trait` for arguments
    delegate: &'a mut dyn Read,
}
impl Read for StringValueReader<'_> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        self.delegate.read(buf)
    }
}