diesel 2.4.0

A safe, extensible ORM and Query Builder for PostgreSQL, SQLite, and MySQL
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
//! Support for JSON and JSONB values under SQLite.

use crate::deserialize::{self, FromSql};
use crate::serialize::{self, IsNull, Output, ToSql};
use crate::sql_types;
use crate::sqlite::{Sqlite, SqliteValue};
use alloc::boxed::Box;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

#[cfg(all(feature = "__sqlite-shared", feature = "serde_json"))]
impl FromSql<sql_types::Json, Sqlite> for serde_json::Value {
    fn from_sql(mut value: SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
        serde_json::from_str(value.read_text()).map_err(|_| "Invalid Json".into())
    }
}

#[cfg(all(feature = "__sqlite-shared", feature = "serde_json"))]
impl ToSql<sql_types::Json, Sqlite> for serde_json::Value {
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
        out.set_value(serde_json::to_string(self)?);
        Ok(IsNull::No)
    }
}

#[cfg(all(feature = "__sqlite-shared", feature = "serde_json"))]
impl FromSql<sql_types::Jsonb, Sqlite> for serde_json::Value {
    fn from_sql(mut value: SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
        use self::jsonb::*;

        let bytes = value.read_blob();

        if bytes.is_empty() {
            return Err("Empty blob cannot be decoded as JSONB".into());
        }

        // Read the JSONB value from the byte stream
        let (jsonb, size) = read_jsonb_value(bytes)?;
        if size == bytes.len() {
            Ok(jsonb)
        } else {
            Err("Payload contained more bytes than the encoded JSONB".into())
        }
    }
}

#[cfg(all(feature = "__sqlite-shared", feature = "serde_json"))]
impl ToSql<sql_types::Jsonb, Sqlite> for serde_json::Value {
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
        use self::jsonb::*;

        // Create a buffer to hold the binary JSONB encoding
        let mut buffer = Vec::new();

        // Write the JSON value into the buffer in JSONB format
        write_jsonb_value(self, &mut buffer)?;

        // Set the serialized binary data to the output
        out.set_value(buffer);

        Ok(IsNull::No)
    }
}

#[cfg(all(feature = "__sqlite-shared", feature = "serde_json"))]
mod jsonb {
    extern crate serde_json;

    use core::error::Error;

    use super::*;

    pub(super) const JSONB_NULL: u8 = 0x00;
    pub(super) const JSONB_TRUE: u8 = 0x01;
    pub(super) const JSONB_FALSE: u8 = 0x02;
    pub(super) const JSONB_INT: u8 = 0x03;
    pub(super) const JSONB_INT5: u8 = 0x04;
    pub(super) const JSONB_FLOAT: u8 = 0x05;
    pub(super) const JSONB_FLOAT5: u8 = 0x06;
    pub(super) const JSONB_TEXT: u8 = 0x07;
    pub(super) const JSONB_TEXTJ: u8 = 0x08;
    pub(super) const JSONB_TEXT5: u8 = 0x09;
    pub(super) const JSONB_TEXTRAW: u8 = 0x0A;
    pub(super) const JSONB_ARRAY: u8 = 0x0B;
    pub(super) const JSONB_OBJECT: u8 = 0x0C;

    #[derive(Debug)]
    struct JsonbHeader {
        element_type: u8,
        payload_size: usize,
        header_size: usize,
        total_size: usize,
    }

    // Helper function to read a JSONB value from the byte stream
    #[allow(unsafe_code)]
    pub(super) fn read_jsonb_value(
        bytes: &[u8],
    ) -> deserialize::Result<(serde_json::Value, usize)> {
        if bytes.is_empty() {
            return Err("Empty JSONB data".into());
        }
        let (global_header, mut global_value) = read_header_and_value(bytes)?;

        if global_value.is_array() || global_value.is_object() {
            // we need to use pointers here, as the borrow checker does not understand
            // that we only modify the last value in this stack. Given that we cannot
            // invalidate any pointer that's up in the stack
            let mut stack = vec![(
                &mut global_value as *mut serde_json::Value,
                global_header.payload_size,
            )];
            let mut payload = &bytes[global_header.header_size..];
            let mut total_read = 0;
            // we must use a loop based decoding approach here instead of the much simpler recursive implementation
            // as we otherwise run into stack overflows for deeply nested objects/arrays

            while total_read < global_header.payload_size {
                let Some((value, _size)) = stack.last().copied() else {
                    break;
                };
                let value = unsafe {
                    // SAFETY: The pointer was initialized before
                    // We we cannot invalidate the underlying object
                    &mut *value
                };

                if let serde_json::Value::Array(array) = value {
                    let (header, value) = read_header_and_value(payload)?;

                    array.push(value);
                    let last_ref = array.last_mut().expect("Pushed above");
                    let payload_size = if last_ref.is_object() || last_ref.is_array() {
                        stack.push((last_ref as *mut _, total_read + header.total_size));
                        header.header_size
                    } else {
                        header.total_size
                    };
                    total_read += payload_size;
                    if payload.len() > payload_size {
                        payload = &payload[payload_size..];
                    } else {
                        for (_, v) in stack {
                            if v != total_read {
                                return Err("Invalid size of payload declared".into());
                            }
                        }
                        break;
                    }
                } else if let serde_json::Value::Object(object) = value {
                    //       while total_read < payload_size {
                    let (key_header, key) = read_header_and_value(payload)?;
                    total_read += key_header.total_size;
                    let serde_json::Value::String(key) = key else {
                        return Err("Expected a string as object key".into());
                    };
                    if payload.len() > key_header.total_size {
                        payload = &payload[key_header.total_size..];
                    } else {
                        return Err("No value found for object".into());
                    }
                    let (value_header, value) = read_header_and_value(payload)?;
                    object.insert(key.clone(), value);
                    let last_ref = object.get_mut(&key).expect("We inserted it above");
                    let payload_size = if last_ref.is_object() || last_ref.is_array() {
                        stack.push((last_ref as *mut _, total_read + value_header.total_size));
                        value_header.header_size
                    } else {
                        value_header.total_size
                    };
                    total_read += payload_size;
                    if payload.len() > payload_size {
                        payload = &payload[payload_size..];
                    } else {
                        for (_, v) in stack {
                            if v != total_read {
                                return Err("Invalid size of payload declared".into());
                            }
                        }
                        break;
                    }
                } else {
                    unreachable!()
                }

                while let Some(v) = stack.last().map(|(_, v)| *v) {
                    if v > total_read {
                        break;
                    } else if v == total_read {
                        stack.pop();
                    } else {
                        return Err("Invalid size of payload declared".into());
                    }
                }
            }
        }
        Ok((global_value, global_header.total_size))
    }

    // This function decodes the jsonb header
    // and the value for non-composite values. For composite values like array and object
    // we only decode the "value header" and leave decoding
    // the actual child values to future calls
    fn read_header_and_value(
        bytes: &[u8],
    ) -> deserialize::Result<(JsonbHeader, serde_json::Value)> {
        let header = read_jsonb_value_header(bytes)?;
        let payload_bytes = &bytes[header.header_size..header.total_size];
        let value = match header.element_type {
            // sqlite writes a constant as a bare one byte header and refuses any other spelling
            JSONB_NULL | JSONB_TRUE | JSONB_FALSE if header.total_size != 1 => {
                Err("Invalid JSONB data: a constant must be a single byte".into())
            }
            JSONB_NULL => Ok(serde_json::Value::Null),
            JSONB_TRUE => Ok(serde_json::Value::Bool(true)),
            JSONB_FALSE => Ok(serde_json::Value::Bool(false)),
            JSONB_INT => read_jsonb_int(payload_bytes, header.payload_size),
            JSONB_INT5 => Err("INT5 is not supported".into()),
            JSONB_FLOAT => read_jsonb_float(payload_bytes, header.payload_size),
            JSONB_FLOAT5 => Err("FLOAT5 is not supported".into()),
            JSONB_TEXT => read_jsonb_text(payload_bytes, header.payload_size),
            JSONB_TEXTJ => read_jsonb_textj(payload_bytes, header.payload_size),
            JSONB_TEXTRAW => read_jsonb_text(payload_bytes, header.payload_size),
            JSONB_TEXT5 => Err("TEXT5 is not supported".into()),
            JSONB_ARRAY => Ok(serde_json::Value::Array(alloc::vec::Vec::new())),
            JSONB_OBJECT => Ok(serde_json::Value::Object(serde_json::Map::new())),
            _ => Err(alloc::format!(
                "Unsupported or reserved JSONB type: {}",
                header.element_type
            )
            .into()),
        }?;
        Ok((header, value))
    }

    fn read_jsonb_value_header(bytes: &[u8]) -> deserialize::Result<JsonbHeader> {
        let first_byte = bytes[0];
        let element_type = first_byte & 0x0F;
        let size_hint = (first_byte & 0xF0) >> 4;
        let (payload_size, header_size): (usize, usize) = match size_hint {
            0x00..=0x0B => (size_hint as usize, 1), // Payload size is directly in the upper nibble
            0x0C => {
                if bytes.len() < 2 {
                    return Err("Invalid JSONB data: insufficient bytes for payload size".into());
                }
                (bytes[1] as usize, 2) // 1 additional byte for payload size
            }
            0x0D => {
                if bytes.len() < 3 {
                    return Err("Invalid JSONB data: insufficient bytes for payload size".into());
                }
                (u16::from_be_bytes([bytes[1], bytes[2]]) as usize, 3) // 2 additional bytes
            }
            0x0E => {
                if bytes.len() < 5 {
                    return Err("Invalid JSONB data: insufficient bytes for payload size".into());
                }
                (
                    u32::from_be_bytes([bytes[1], bytes[2], bytes[3], bytes[4]]) as usize,
                    5,
                ) // 4 additional bytes
            }
            0x0F => {
                if bytes.len() < 9 {
                    return Err("Invalid JSONB data: insufficient bytes for payload size".into());
                }
                (
                    usize::try_from(u64::from_be_bytes([
                        bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
                        bytes[8],
                    ]))
                    .map_err(Box::new)?,
                    9,
                ) // 8 additional bytes
            }
            _ => return Err("Invalid payload size hint".into()),
        };
        let total_size = header_size
            .checked_add(payload_size)
            .ok_or("The provided payload size overflows usize")?;
        if bytes.len() < total_size {
            return Err(alloc::format!(
                "Invalid JSONB data: insufficient bytes for value of type {}, expected {} bytes, got {}",
                element_type,
                total_size,
                bytes.len()
            )
            .into());
        }

        Ok(JsonbHeader {
            element_type,
            payload_size,
            header_size,
            total_size,
        })
    }

    // Read a JSON integer in canonical format (INT)
    pub(super) fn read_jsonb_int(
        bytes: &[u8],
        payload_size: usize,
    ) -> deserialize::Result<serde_json::Value> {
        // Ensure the bytes are at least as large as the payload size
        if bytes.len() < payload_size {
            return Err(alloc::format!(
                "Expected payload of size {}, but got {}",
                payload_size,
                bytes.len()
            )
            .into());
        }

        // Read only the number of bytes specified by the payload size
        let int_str = core::str::from_utf8(bytes).map_err(|_| "Invalid ASCII in JSONB integer")?;
        // An INT payload is integer text, so fractional and exponent forms are malformed.
        // `serde_json` reports a non-finite `1e999` as neither float nor integer.
        if int_str.contains(['.', 'e', 'E']) {
            return Err("Failed to parse JSONB integer".into());
        }
        // `-0` is the one integer text `serde_json` turns into a float, and a signed
        // zero only carries meaning for floats, so decode the integer it denotes.
        if int_str == "-0" {
            return Ok(serde_json::Value::Number(serde_json::Number::from(0)));
        }
        let int_value = serde_json::from_str(int_str)
            .map_err(|_| "Failed to parse JSONB")
            .and_then(|v: serde_json::Value| {
                // Without `arbitrary_precision` an integer wider than 64 bits parses as
                // a lossy float, which must not pass as the stored value.
                (v.is_number() && !v.is_f64())
                    .then_some(v)
                    .ok_or("Failed to parse JSONB integer")
            })?;

        Ok(int_value)
    }

    // Read a JSON float in canonical format (FLOAT)
    pub(super) fn read_jsonb_float(
        bytes: &[u8],
        payload_size: usize,
    ) -> deserialize::Result<serde_json::Value> {
        if bytes.len() < payload_size {
            return Err(alloc::format!(
                "Expected payload of size {}, but got {}",
                payload_size,
                bytes.len()
            )
            .into());
        }

        let float_str = core::str::from_utf8(bytes).map_err(|_| "Invalid UTF-8 in JSONB float")?;
        let float_value = serde_json::from_str(float_str)
            .map_err(|_| "Failed to parse JSONB")
            .and_then(|v: serde_json::Value| {
                v.is_f64()
                    .then_some(v)
                    .ok_or("Failed to parse JSONB number")
            })?;

        Ok(float_value)
    }

    // Read a JSON string
    pub(super) fn read_jsonb_text(
        bytes: &[u8],
        payload_size: usize,
    ) -> deserialize::Result<serde_json::Value> {
        if bytes.len() < payload_size {
            return Err(alloc::format!(
                "Expected payload of size {}, but got {}",
                payload_size,
                bytes.len()
            )
            .into());
        }

        let text = core::str::from_utf8(bytes).map_err(|_| "Invalid UTF-8 in JSONB string")?;
        Ok(serde_json::Value::String(text.to_string()))
    }

    pub(super) fn read_jsonb_textj(
        bytes: &[u8],
        payload_size: usize,
    ) -> deserialize::Result<serde_json::Value> {
        if bytes.len() < payload_size {
            return Err(alloc::format!(
                "Expected payload of size {}, but got {}",
                payload_size,
                bytes.len()
            )
            .into());
        }

        let text = core::str::from_utf8(bytes).map_err(|_| "Invalid UTF-8 in JSONB string")?;

        // Unescape JSON escape sequences (e.g., "\n", "\u0020")
        let unescaped_text = serde_json::from_str(&alloc::format!("\"{text}\""))
            .map_err(|_| "Failed to parse JSON-escaped text in TEXTJ")?;

        Ok(unescaped_text)
    }

    enum JsonValuePtr<'a> {
        Value(&'a serde_json::Value),
        Array {
            values: &'a [serde_json::Value],
            serialized_buffer: Vec<u8>,
        },
        Object {
            object: &'a serde_json::value::Map<String, serde_json::Value>,
            keys: Box<dyn Iterator<Item = &'a String> + 'a>,
            serialized_buffer: Vec<u8>,
        },
    }

    pub(super) fn write_jsonb_header(
        buffer: &mut Vec<u8>,
        element_type: u8,
        payload_size: usize,
    ) -> serialize::Result {
        // Check if payload size exceeds the maximum allowed size
        if payload_size > 2_147_483_647 {
            return Err("Payload size exceeds the maximum allowed size of 2GB".into());
        }

        if payload_size <= 0x0B {
            // Small payloads, 0 additional byte for size
            buffer.push(
                ((u8::try_from(payload_size).map_err(|e| e.to_string())?) << 4) | element_type,
            );
        } else if payload_size <= 0xFF {
            // Medium payloads, 1 additional byte for size
            buffer.extend_from_slice(&[
                (0x0C << 4) | element_type,
                u8::try_from(payload_size).map_err(|e| e.to_string())?,
            ]);
        } else if payload_size <= 0xFFFF {
            // Larger payloads, 2 additional bytes for size
            buffer.push((0x0D << 4) | element_type);
            buffer.extend_from_slice(
                &(u16::try_from(payload_size).map_err(|e| e.to_string())?).to_be_bytes(),
            );
        } else {
            // Very large payloads, 4 additional bytes for size (up to 2 GiB)
            buffer.push((0x0E << 4) | element_type);
            buffer.extend_from_slice(
                &(u32::try_from(payload_size).map_err(|e| e.to_string())?).to_be_bytes(),
            );
        };

        Ok(IsNull::No)
    }

    fn place_composite_value(
        buffer: &mut Vec<u8>,
        stack: &mut [JsonValuePtr<'_>],
        serialized_buffer: Vec<u8>,
        tpe: u8,
    ) -> serialize::Result {
        let buffer = match stack.last_mut() {
            None | Some(JsonValuePtr::Value(_)) => buffer,
            Some(
                JsonValuePtr::Array {
                    serialized_buffer, ..
                }
                | JsonValuePtr::Object {
                    serialized_buffer, ..
                },
            ) => serialized_buffer,
        };
        write_jsonb_header(buffer, tpe, serialized_buffer.len())?;
        buffer.extend(serialized_buffer);

        Ok(IsNull::No)
    }

    // Helper function to write a JSON value into a JSONB binary format
    pub(super) fn write_jsonb_value(
        value: &serde_json::Value,
        buffer: &mut Vec<u8>,
    ) -> serialize::Result {
        let mut stack = vec![JsonValuePtr::Value(value)];

        while let Some(value) = stack.pop() {
            let add_to_stack = match value {
                JsonValuePtr::Value(value) => write_plain_jsonb_value(buffer, value)?,
                JsonValuePtr::Array {
                    values,
                    mut serialized_buffer,
                } => {
                    if let Some((el, tail)) = values.split_first() {
                        let ret = write_plain_jsonb_value(&mut serialized_buffer, el)?;
                        stack.push(JsonValuePtr::Array {
                            values: tail,
                            serialized_buffer,
                        });
                        ret
                    } else {
                        place_composite_value(buffer, &mut stack, serialized_buffer, JSONB_ARRAY)?;
                        None
                    }
                }
                JsonValuePtr::Object {
                    object,
                    mut keys,
                    mut serialized_buffer,
                } => {
                    if let Some(next_key) = keys.next() {
                        let value = object
                            .get(next_key)
                            .ok_or_else(|| format!("Missing value for object key: `{next_key}`"))?;
                        write_jsonb_string(next_key, &mut serialized_buffer)?;
                        let ret = write_plain_jsonb_value(&mut serialized_buffer, value)?;
                        stack.push(JsonValuePtr::Object {
                            object,
                            keys,
                            serialized_buffer,
                        });
                        ret
                    } else {
                        place_composite_value(buffer, &mut stack, serialized_buffer, JSONB_OBJECT)?;
                        None
                    }
                }
            };
            if let Some(next) = add_to_stack {
                stack.push(next);
            }
        }

        Ok(IsNull::No)
    }

    fn write_plain_jsonb_value<'a>(
        buffer: &mut Vec<u8>,
        value: &'a serde_json::Value,
    ) -> Result<Option<JsonValuePtr<'a>>, Box<dyn Error + Send + Sync>> {
        let ret = if value.is_null() {
            write_jsonb_null(buffer)?;
            None
        } else if value.is_boolean() {
            write_jsonb_bool(value.as_bool().ok_or("Failed to read JSONB value")?, buffer)?;
            None
        } else if value.is_number() {
            write_jsonb_number(value, buffer)?;
            None
        } else if value.is_string() {
            write_jsonb_string(value.as_str().ok_or("Failed to read JSONB value")?, buffer)?;
            None
        } else if value.is_array() {
            let array = value.as_array().ok_or("Failed to read JSONB value")?;
            Some(JsonValuePtr::Array {
                values: array,
                serialized_buffer: Vec::new(),
            })
        } else if value.is_object() {
            let object = value.as_object().ok_or("Failed to read JSONB value")?;
            Some(JsonValuePtr::Object {
                object,
                keys: Box::new(object.keys()),
                serialized_buffer: Vec::new(),
            })
        } else {
            return Err("Unsupported JSONB value type".into());
        };
        Ok(ret)
    }

    // Write a JSON null
    pub(super) fn write_jsonb_null(buffer: &mut Vec<u8>) -> serialize::Result {
        write_jsonb_header(buffer, JSONB_NULL, 0x0)?;
        Ok(IsNull::No)
    }

    // Write a JSON boolean
    pub(super) fn write_jsonb_bool(b: bool, buffer: &mut Vec<u8>) -> serialize::Result {
        // Use the constants for true and false
        write_jsonb_header(buffer, if b { JSONB_TRUE } else { JSONB_FALSE }, 0x0)?;
        Ok(IsNull::No)
    }

    // Write a JSON number (integers and floats)
    pub(super) fn write_jsonb_number(
        n: &serde_json::Value,
        buffer: &mut Vec<u8>,
    ) -> serialize::Result {
        let n = n.to_string();
        let tpe = if n
            .char_indices()
            .any(|(idx, c)| !(c.is_ascii_digit() || (idx == 0 && (c == '-' || c == '+'))))
        {
            JSONB_FLOAT
        } else {
            JSONB_INT
        };
        write_jsonb_header(buffer, tpe, n.len())?;

        // Write the ASCII text representation of the integer/float as the payload
        buffer.extend_from_slice(n.as_bytes());

        Ok(IsNull::No)
    }

    pub(super) fn write_jsonb_string(s: &str, buffer: &mut Vec<u8>) -> serialize::Result {
        // strings needing a json escape go down TEXTJ, matching what sqlite's own jsonb() writes
        // the scan is over bytes because json escapes nothing above 0x1F, unlike char::is_control
        if s.bytes().any(|b| b < 0x20 || b == b'"' || b == b'\\') {
            write_jsonb_textj(s, buffer)
        } else {
            write_jsonb_header(buffer, JSONB_TEXT, s.len())?;
            buffer.extend_from_slice(s.as_bytes());
            Ok(IsNull::No)
        }
    }

    pub(super) fn write_jsonb_textj(s: &str, buffer: &mut Vec<u8>) -> serialize::Result {
        // &s passes a sized &str, required by the serde_json 0.8.0 to_string bound
        let escaped_string =
            serde_json::to_string(&s).map_err(|_| "Failed to serialize string for TEXTJ")?;

        let escaped_string = &escaped_string[1..escaped_string.len() - 1];

        write_jsonb_header(buffer, JSONB_TEXTJ, escaped_string.len())?;

        buffer.extend_from_slice(escaped_string.as_bytes());

        Ok(IsNull::No)
    }
}

#[cfg(test)]
#[cfg(all(feature = "__sqlite-shared", feature = "serde_json"))]
mod tests {
    use super::jsonb::*;
    use super::*;
    #[cfg(not(miri))] // ffi call
    use crate::ExpressionMethods;
    #[cfg(not(miri))] // ffi call
    use crate::dsl::json_valid_with_flags;
    #[cfg(not(miri))] // ffi call
    use crate::query_dsl::RunQueryDsl;
    #[cfg(not(miri))] // ffi call
    use crate::sqlite::JsonValidFlag;
    #[cfg(not(miri))] // ffi call
    use crate::test_helpers::connection;
    use crate::test_helpers::format_error;
    #[cfg(not(miri))] // ffi call
    use crate::{IntoSql, dsl::sql};
    use serde_json::{Value, json};
    use sql_types::{Json, Jsonb};

    // Helper function to create the correct JsonbHeader based on the payload size
    pub(super) fn create_jsonb_header(
        element_type: u8,
        payload_size: usize,
    ) -> Result<Vec<u8>, Box<dyn core::error::Error + Send + Sync>> {
        let mut buffer = Vec::new();
        jsonb::write_jsonb_header(&mut buffer, element_type, payload_size)?;
        Ok(buffer)
    }

    fn value_with_payload(element_type: u8, payload: &[u8]) -> Vec<u8> {
        let mut buffer = create_jsonb_header(element_type, payload.len()).unwrap();
        buffer.extend_from_slice(payload);
        buffer
    }

    #[diesel_test_helper::test]
    fn regression_a_constant_reads_its_declared_size() {
        let blobs: Vec<Vec<u8>> = vec![
            value_with_payload(JSONB_NULL, &[0x0D, 0x00, 0xF3]), // null, inline size 3
            value_with_payload(JSONB_TRUE, b"1.5"),              // true, inline size 3
            value_with_payload(JSONB_NULL, &[0x00]),             // null, inline size 1
            value_with_payload(JSONB_TRUE, &[0x08]),             // true, inline size 1
            value_with_payload(JSONB_FALSE, &[0xFF]),            // false, inline size 1
            // not buildable with create_jsonb_header because the writer takes the narrowest size spelling
            vec![0xC0, 0x00], // null, two byte header
            vec![0xC1, 0x00], // true, two byte header
            {
                // array holding a two byte true
                let mut blob = create_jsonb_header(JSONB_ARRAY, 2).unwrap();
                blob.extend_from_slice(&[0xC1, 0x00]);
                blob
            },
            {
                // object holding one
                let mut blob = create_jsonb_header(JSONB_OBJECT, 4).unwrap();
                blob.extend(create_jsonb_header(JSONB_TEXT, 1).unwrap());
                blob.extend_from_slice(b"a");
                blob.extend_from_slice(&[0xC1, 0x00]);
                blob
            },
        ];
        for blob in blobs {
            let blob: &[u8] = &blob;
            assert!(
                read_jsonb_value(blob).is_err(),
                "{blob:02X?} decoded to {:?}",
                read_jsonb_value(blob).map(|value| value.0)
            );
        }

        // the one byte spelling sqlite writes
        let mut blob = create_jsonb_header(JSONB_ARRAY, 2).unwrap();
        blob.extend(create_jsonb_header(JSONB_TRUE, 0).unwrap());
        blob.extend(create_jsonb_header(JSONB_NULL, 0).unwrap());
        assert_eq!(read_jsonb_value(&blob).unwrap().0, json!([true, null]));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn regression_json_text_float_survives_a_round_trip() {
        let conn = &mut connection();
        for float in [
            8.829872855928286e-308f64,
            -0.20221894534048165,
            1.7383394626966921e-307,
            6.178787134922198e305,
        ] {
            let value = json!(float);
            let text = diesel::select(sql::<sql_types::Text>("").bind::<sql_types::Json, _>(value))
                .get_result::<String>(conn)
                .unwrap();
            let back = diesel::select(sql::<sql_types::Json>("").bind::<sql_types::Text, _>(text))
                .get_result::<Value>(conn)
                .unwrap();
            assert_eq!(back.as_f64().map(f64::to_bits), Some(float.to_bits()));
        }
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn regression_float_keeps_its_bits_through_the_writers_text() {
        let conn = &mut connection();
        let mut assert_roundtrip = |value: Value| {
            let expected = value.as_f64().unwrap();
            let blob = diesel::select(sql::<sql_types::Binary>("").bind::<Jsonb, _>(value))
                .get_result::<Vec<u8>>(conn)
                .unwrap();
            let back = diesel::select(sql::<Jsonb>("").bind::<sql_types::Binary, _>(blob))
                .get_result::<Value>(conn)
                .unwrap();
            assert_eq!(
                back.as_f64().unwrap().to_bits(),
                expected.to_bits(),
                "{expected:?} came back as {back}"
            );
        };

        // parsed from text, so `serde_json` chose the double
        assert_roundtrip(serde_json::from_str("-922333372720360.5975808").unwrap());

        for f in [
            // these drift without `float_roundtrip`
            -0.20221894534048165,
            6.178787134922198e305,
            -5.276099561814224e214,
            3.587959730897931e-246,
            9.136353238902674e-45,
            2.4261860608815182e-160,
            -2.5496151068102186e-175,
            5.0513463356317975e-231,
            // exact already, must stay exact
            0.1,
            1e-7,
            5e-324,
            -0.0,
            f64::MAX,
            f64::MIN_POSITIVE,
        ] {
            assert_roundtrip(json!(f));
        }
    }

    #[diesel_test_helper::test]
    fn regression_every_float_text_reads_back_as_the_same_double() {
        let mut drifted = Vec::new();
        let mut check = |bits: u64| {
            let f = f64::from_bits(bits);
            if !f.is_finite() {
                return;
            }
            let mut buffer = Vec::new();
            write_jsonb_number(&json!(f), &mut buffer).unwrap();
            let back = read_jsonb_value(&buffer).unwrap().0;
            if back.as_f64().map(f64::to_bits) != Some(bits) {
                drifted.push(alloc::format!("{bits:#018x} {f:?} -> {back}"));
            }
        };

        for exponent in [0, 1, 2, 512, 1021, 1022, 1023, 1024, 1075, 2045, 2046] {
            for mantissa in [0, 1, 2, 1 << 26, 1 << 51, (1 << 52) - 2, (1 << 52) - 1] {
                for sign in [0, 1u64 << 63] {
                    check(sign | (exponent << 52) | mantissa);
                }
            }
        }

        // the doubles either side of the text-parsed witness
        let witness = serde_json::from_str::<Value>("-922333372720360.5975808")
            .unwrap()
            .as_f64()
            .unwrap()
            .to_bits();
        for bits in witness - 2..=witness + 2 {
            check(bits);
        }

        // deterministic bit-pattern sweep
        let mut state: u64 = 0x2545F4914F6CDD1D;
        for _ in 0..256 {
            state ^= state << 13;
            state ^= state >> 7;
            state ^= state << 17;
            check(state);
        }

        assert!(
            drifted.is_empty(),
            "{} floats drifted, first {:?}",
            drifted.len(),
            &drifted[..drifted.len().min(4)]
        );
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn every_jsonb_number_is_written_valid_and_round_trips() {
        let conn = &mut connection();

        // `1 << 63` is `i64::MAX + 1`, `1e21` is where Rust switches to exponent
        // notation, and an exponent without a fraction digit carries no `.`.
        let numbers = [
            (json!(0), JSONB_INT),
            (json!(1), JSONB_INT),
            (json!(-1), JSONB_INT),
            (json!(-5), JSONB_INT),
            (json!(u64::from(u32::MAX)), JSONB_INT),
            (json!(i64::MAX), JSONB_INT),
            (json!(i64::MIN), JSONB_INT),
            (json!(1u64 << 63), JSONB_INT),
            (json!(u64::MAX), JSONB_INT),
            (json!(3.0), JSONB_FLOAT),
            (json!(-0.0), JSONB_FLOAT),
            (json!(-2.5), JSONB_FLOAT),
            (json!(1e-7), JSONB_FLOAT),
            (json!(1e300), JSONB_FLOAT),
            (json!(-1e300), JSONB_FLOAT),
            (json!(1.5e300), JSONB_FLOAT),
            (json!(1e21), JSONB_FLOAT),
            (json!(f64::MIN), JSONB_FLOAT),
            (json!(f64::MAX), JSONB_FLOAT),
            (json!(f64::MIN_POSITIVE), JSONB_FLOAT),
        ];

        for (value, element_type) in numbers {
            let blob = diesel::select(sql::<sql_types::Binary>("").bind::<Jsonb, _>(value.clone()))
                .get_result::<Vec<u8>>(conn)
                .unwrap();
            assert_eq!(
                blob[0] & 0x0F,
                element_type,
                "{value} was written with the wrong element type: {blob:02X?}"
            );
            let valid = diesel::select(json_valid_with_flags::<sql_types::Binary, _, _>(
                blob.as_slice(),
                JsonValidFlag::JsonbStrict,
            ))
            .get_result::<bool>(conn)
            .unwrap();
            assert!(
                valid,
                "sqlite rejects the blob written for {value}: {blob:02X?}"
            );
            let back = diesel::select(sql::<Jsonb>("").bind::<sql_types::Binary, _>(blob.clone()))
                .get_result::<Value>(conn)
                .unwrap_or_else(|error| panic!("{value} does not read back: {error}"));
            assert_eq!(back, value, "{blob:02X?}");
        }
    }

    // asserts a written blob is well formed to sqlite and reads back unchanged
    #[cfg(not(miri))] // ffi call
    fn jsonb_survives(conn: &mut crate::SqliteConnection, value: &Value) -> Result<(), String> {
        let blob = diesel::select(sql::<sql_types::Binary>("").bind::<Jsonb, _>(value.clone()))
            .get_result::<Vec<u8>>(conn)
            .unwrap();
        let valid = diesel::select(json_valid_with_flags::<sql_types::Binary, _, _>(
            blob.as_slice(),
            JsonValidFlag::JsonbStrict,
        ))
        .get_result::<bool>(conn)
        .unwrap();
        let back = diesel::select(sql::<Jsonb>("").bind::<sql_types::Binary, _>(blob.clone()))
            .get_result::<Value>(conn);
        if !valid {
            return Err(alloc::format!(
                "sqlite calls {value} malformed as {blob:02X?}"
            ));
        }
        match back {
            Ok(ref back) if back == value => Ok(()),
            Ok(back) => Err(alloc::format!("{value} came back as {back}")),
            Err(e) => Err(alloc::format!("{value} came back as {e}")),
        }
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn regression_string_needing_an_escape_is_written_valid() {
        let conn = &mut connection();
        for value in [
            json!(r#"a"b"#),
            json!(r#"a\b"#),
            json!(r#"a"\b"#),
            json!(r#"""#),
            json!(r#"\"#),
            json!(r#"\""#),
            json!(r#""leading"#),
            json!(r#"trailing""#),
            json!(r#"aaaaaaaaaaaa"bbbbbbbbbbbb"#),
            json!({ "k": r#"a"b"# }),
            json!([r#"a\b"#]),
            json!({ r#"a"b"#: 1 }),
            json!(""),
            json!("abc"),
            json!("a\nb"),
            json!("a\"\nb"),
        ] {
            jsonb_survives(conn, &value).unwrap_or_else(|e| panic!("{e}"));
        }
    }

    #[diesel_test_helper::test]
    fn test_write_jsonb_number_classifies_by_integer_text() {
        // `serde_json` writes an exponent with a sign, so the payload is `1e+300`.
        for (value, element_type, payload) in [
            (json!(0), JSONB_INT, "0"),
            (json!(-1), JSONB_INT, "-1"),
            (json!(i64::MIN), JSONB_INT, "-9223372036854775808"),
            (json!(u64::MAX), JSONB_INT, "18446744073709551615"),
            (json!(3.0), JSONB_FLOAT, "3.0"),
            (json!(-0.0), JSONB_FLOAT, "-0.0"),
            (json!(-2.5), JSONB_FLOAT, "-2.5"),
            (json!(1e-7), JSONB_FLOAT, "1e-7"),
            (json!(1e300), JSONB_FLOAT, "1e+300"),
        ] {
            let mut buffer = Vec::new();
            write_jsonb_value(&value, &mut buffer).unwrap();

            let mut expected = create_jsonb_header(element_type, payload.len()).unwrap();
            expected.extend_from_slice(payload.as_bytes());

            assert_eq!(buffer, expected, "{value} was not written as {payload}");
        }
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn numbers_nested_in_containers_are_written_valid() {
        let conn = &mut connection();
        let value = json!({"a": -1, "b": [-2, -2.5, 1e-7, 0, u64::MAX], "c": {"d": i64::MIN}});

        let blob = diesel::select(sql::<sql_types::Binary>("").bind::<Jsonb, _>(value.clone()))
            .get_result::<Vec<u8>>(conn)
            .unwrap();
        let valid = diesel::select(
            sql::<sql_types::Integer>("json_valid(")
                .bind::<sql_types::Binary, _>(blob.clone())
                .sql(", 8)"),
        )
        .get_result::<i32>(conn)
        .unwrap();
        assert_eq!(valid, 1, "sqlite rejects {blob:02X?}");

        let back = diesel::select(sql::<Jsonb>("").bind::<sql_types::Binary, _>(blob))
            .get_result::<Value>(conn)
            .unwrap();
        assert_eq!(back, value);
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_int_decodes_the_whole_representable_range() {
        for (payload, expected) in [
            (&b"0"[..], json!(0)),
            (&b"1"[..], json!(1)),
            (&b"-1"[..], json!(-1)),
            // sqlite stores `-0` as an integer and `json_extract` reads it as one
            (&b"-0"[..], json!(0)),
            (&b"9223372036854775807"[..], json!(i64::MAX)),
            (&b"-9223372036854775808"[..], json!(i64::MIN)),
            (&b"9223372036854775808"[..], json!(1u64 << 63)),
            (&b"18446744073709551615"[..], json!(u64::MAX)),
        ] {
            let mut data = create_jsonb_header(JSONB_INT, payload.len()).unwrap();
            data.extend_from_slice(payload);

            let payload = core::str::from_utf8(payload).unwrap();
            assert_eq!(
                read_jsonb_value(&data).unwrap().0,
                expected,
                "{payload} did not decode to itself"
            );
        }
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn regression_every_char_is_written_valid() {
        let conn = &mut connection();
        let mut offenders = Vec::new();
        // batch scalars into arrays so one query covers a chunk, then walk only a failing chunk
        let mut chunk = Vec::new();
        for code in 0u32..=0x10FFFF {
            if let Some(c) = char::from_u32(code) {
                let mut s = alloc::string::String::from('a');
                s.push(c);
                s.push('b');
                chunk.push(json!(s));
            }
            if chunk.len() < 2048 && code != 0x10FFFF {
                continue;
            }
            let batch = Value::Array(core::mem::take(&mut chunk));
            if let Err(batch_error) = jsonb_survives(conn, &batch) {
                let Value::Array(values) = batch else {
                    unreachable!()
                };
                let known = offenders.len();
                for value in values {
                    if let Err(e) = jsonb_survives(conn, &value) {
                        offenders.push(e);
                    }
                }
                // a batch no element explains means the array header itself is wrong, so keep it
                if offenders.len() == known {
                    offenders.push(batch_error);
                }
            }
        }
        assert!(
            offenders.is_empty(),
            "{} values broke the blob, first {:?}",
            offenders.len(),
            &offenders[..offenders.len().min(4)]
        );
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_int_rejects_non_integer_payloads() {
        for payload in [
            // fractions and exponents belong to a FLOAT element
            &b"1.5"[..],
            &b"1e5"[..],
            // parses as neither an integer nor a finite float
            &b"1e999"[..],
            // not canonical JSON integer text
            &b"+1"[..],
            &b"01"[..],
            &b""[..],
            &b"-"[..],
            &b"0x10"[..],
            &b"nan"[..],
            &b"Infinity"[..],
        ] {
            let mut data = create_jsonb_header(JSONB_INT, payload.len()).unwrap();
            data.extend_from_slice(payload);

            let payload = core::str::from_utf8(payload).unwrap();
            assert!(
                read_jsonb_value(&data).is_err(),
                "{payload} is not a JSONB integer"
            );
        }
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_int_decodes_a_wide_integer_exactly_or_not_at_all() {
        // `Number::from_u128` succeeds only when `serde_json` can hold the value
        // exactly, which depends on its `arbitrary_precision` feature, and any
        // dependency can enable that.
        for payload in [
            &b"18446744073709551616"[..],
            &b"123456789012345678901234567890"[..],
        ] {
            let mut data = create_jsonb_header(JSONB_INT, payload.len()).unwrap();
            data.extend_from_slice(payload);

            let payload = core::str::from_utf8(payload).unwrap();
            let exact = serde_json::Number::from_u128(payload.parse().unwrap()).map(Value::Number);
            let decoded = read_jsonb_value(&data);

            match exact {
                Some(expected) => assert_eq!(
                    decoded.unwrap().0,
                    expected,
                    "{payload} is representable and must decode"
                ),
                None => assert!(decoded.is_err(), "{payload} would decode rounded"),
            }
        }
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_int_above_i64_max_inside_containers() {
        let mut element = create_jsonb_header(JSONB_INT, 20).unwrap();
        element.extend_from_slice(b"18446744073709551615");

        let mut array = create_jsonb_header(JSONB_ARRAY, element.len()).unwrap();
        array.extend_from_slice(&element);
        assert_eq!(read_jsonb_value(&array).unwrap().0, json!([u64::MAX]));

        let mut key = create_jsonb_header(JSONB_TEXT, 1).unwrap();
        key.extend_from_slice(b"a");
        let mut object = create_jsonb_header(JSONB_OBJECT, key.len() + element.len()).unwrap();
        object.extend_from_slice(&key);
        object.extend_from_slice(&element);
        assert_eq!(read_jsonb_value(&object).unwrap().0, json!({"a": u64::MAX}));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn regression_unsigned_above_i64_reads_a_blob_sqlite_wrote() {
        let conn = &mut connection();

        let back = diesel::select(sql::<Jsonb>("jsonb('18446744073709551615')"))
            .get_result::<Value>(conn)
            .unwrap();

        assert_eq!(back, json!(u64::MAX));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn regression_negative_zero_reads_a_blob_sqlite_wrote() {
        let conn = &mut connection();

        let back = diesel::select(sql::<Jsonb>("jsonb('-0')"))
            .get_result::<Value>(conn)
            .unwrap();

        assert_eq!(back, json!(0));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn json_to_sql() {
        let conn = &mut connection();
        let res = diesel::select(json!(true).into_sql::<Json>().eq(&sql("json('true')")))
            .get_result::<bool>(conn)
            .unwrap();
        assert!(res);
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_null() {
        let data = vec![JSONB_NULL];
        let result = read_jsonb_value(&data).unwrap().0;
        assert_eq!(result, Value::Null);
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_true() {
        let data = vec![JSONB_TRUE];
        let result = read_jsonb_value(&data).unwrap().0;
        assert_eq!(result, Value::Bool(true));
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_false() {
        let data = vec![JSONB_FALSE];
        let result = read_jsonb_value(&data).unwrap().0;
        assert_eq!(result, Value::Bool(false));
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_int() {
        // JSONB_INT with payload "1"
        let mut data = Vec::new();
        data.extend(create_jsonb_header(JSONB_INT, 0x01).unwrap());
        data.push(b'1'); // Add the payload (integer "1")

        let result = read_jsonb_value(&data).unwrap().0;
        assert_eq!(result, json!(1));
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_float() {
        // JSONB_FLOAT with payload "1.5"
        let mut data = Vec::new();
        data.extend(create_jsonb_header(JSONB_FLOAT, 0x03).unwrap());
        data.extend_from_slice(b"1.5"); // Add the payload (float "1.5")

        let result = read_jsonb_value(&data).unwrap().0;
        assert_eq!(result, json!(1.5));
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_text() {
        // JSONB_TEXT with payload "foo"
        let mut data = Vec::new();
        data.extend(create_jsonb_header(JSONB_TEXT, 0x03).unwrap());
        data.extend_from_slice(b"foo"); // Add the payload (text "foo")

        let result = read_jsonb_value(&data).unwrap().0;
        assert_eq!(result, json!("foo"));
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_textraw() {
        // JSONB_TEXTRAW with payload "foo"
        let mut data = Vec::new();
        data.extend(create_jsonb_header(JSONB_TEXTRAW, 0x03).unwrap());
        data.extend_from_slice(b"foo");

        let result = read_jsonb_value(&data).unwrap().0;
        assert_eq!(result, json!("foo"));
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_object_with_textraw_key() {
        // JSONB_OBJECT with a TEXTRAW key and value
        let mut data = Vec::new();
        data.extend(create_jsonb_header(JSONB_OBJECT, 0x06).unwrap());
        data.extend(create_jsonb_header(JSONB_TEXTRAW, 0x01).unwrap());
        data.extend_from_slice(b"a");
        data.extend(create_jsonb_header(JSONB_TEXTRAW, 0x03).unwrap());
        data.extend_from_slice(b"bar");

        let result = read_jsonb_value(&data).unwrap().0;
        assert_eq!(result, json!({"a": "bar"}));
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_array() {
        // JSONB_ARRAY with two elements: 1 and true
        let mut data = Vec::new();
        data.extend(create_jsonb_header(JSONB_ARRAY, 0x03).unwrap()); // Array header

        // Element 1: integer "1"
        data.extend(create_jsonb_header(JSONB_INT, 0x01).unwrap());
        data.push(b'1');

        // Element 2: true
        data.extend(create_jsonb_header(JSONB_TRUE, 0x00).unwrap());

        let result = read_jsonb_value(&data).unwrap().0;
        assert_eq!(result, json!([1, true]));
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_object() {
        // JSONB_OBJECT with one key-value pair: "key": 42
        let mut data = Vec::new();
        data.extend(create_jsonb_header(JSONB_OBJECT, 0x07).unwrap()); // Object header

        // Key: "key"
        data.extend(create_jsonb_header(JSONB_TEXT, 0x03).unwrap());
        data.extend_from_slice(b"key"); // Add the key payload

        // Value: 42 (integer)
        data.extend(create_jsonb_header(JSONB_INT, 0x02).unwrap());
        data.extend_from_slice(b"42"); // Add the integer payload

        let result = read_jsonb_value(&data).unwrap().0;
        assert_eq!(result, json!({"key": 42}));
    }

    #[diesel_test_helper::test]
    fn test_read_jsonb_nested_object() {
        let mut data = Vec::new();

        data.extend(create_jsonb_header(JSONB_OBJECT, 42).unwrap());

        data.extend(create_jsonb_header(JSONB_TEXT, 9).unwrap());
        data.extend_from_slice(b"outer_key");

        data.extend(create_jsonb_header(JSONB_OBJECT, 13).unwrap());

        data.extend(create_jsonb_header(JSONB_TEXT, 9).unwrap());
        data.extend_from_slice(b"inner_key");

        data.extend(create_jsonb_header(JSONB_INT, 2).unwrap());
        data.extend_from_slice(b"42");

        data.extend(create_jsonb_header(JSONB_TEXT, 14).unwrap());
        data.extend_from_slice(b"additional_key");

        data.extend(create_jsonb_header(JSONB_TRUE, 0).unwrap());

        let result = read_jsonb_value(&data).unwrap().0;
        assert_eq!(
            result,
            json!({
                "additional_key": true,
                "outer_key": {
                    "inner_key": 42
                },
            })
        );
    }

    #[diesel_test_helper::test]
    fn test_write_jsonb_null() {
        let value = serde_json::Value::Null;
        let mut buffer = Vec::new();
        write_jsonb_value(&value, &mut buffer).unwrap();
        assert_eq!(buffer, vec![JSONB_NULL]);
    }

    #[diesel_test_helper::test]
    fn test_write_jsonb_true() {
        let value = serde_json::Value::Bool(true);
        let mut buffer = Vec::new();
        write_jsonb_value(&value, &mut buffer).unwrap();
        assert_eq!(buffer, vec![JSONB_TRUE]);
    }

    #[diesel_test_helper::test]
    fn test_write_jsonb_false() {
        let value = serde_json::Value::Bool(false);
        let mut buffer = Vec::new();
        write_jsonb_value(&value, &mut buffer).unwrap();
        assert_eq!(buffer, vec![JSONB_FALSE]);
    }

    #[diesel_test_helper::test]
    fn test_write_jsonb_int() {
        let value = serde_json::Value::Number(serde_json::Number::from(1));
        let mut buffer = Vec::new();
        write_jsonb_value(&value, &mut buffer).unwrap();

        let mut expected_buffer = Vec::new();
        expected_buffer.extend(create_jsonb_header(JSONB_INT, 0x01).unwrap());
        expected_buffer.push(b'1'); // Payload: integer "1"

        assert_eq!(buffer, expected_buffer);
    }

    #[diesel_test_helper::test]
    fn test_write_jsonb_float() {
        let value = serde_json::Value::Number(serde_json::Number::from_f64(1.5).unwrap());
        let mut buffer = Vec::new();
        write_jsonb_value(&value, &mut buffer).unwrap();

        let mut expected_buffer = Vec::new();
        expected_buffer.extend(create_jsonb_header(JSONB_FLOAT, 0x03).unwrap());
        expected_buffer.extend_from_slice(b"1.5"); // Payload: float "1.5"

        assert_eq!(buffer, expected_buffer);
    }

    #[diesel_test_helper::test]
    fn test_write_jsonb_text() {
        let mut buffer = Vec::new();
        let input_string = "hello";
        write_jsonb_string(input_string, &mut buffer).unwrap();

        let mut expected_buffer = Vec::new();
        expected_buffer.extend(create_jsonb_header(JSONB_TEXT, 0x05).unwrap());
        expected_buffer.extend_from_slice(b"hello");

        assert_eq!(buffer, expected_buffer);
    }

    #[diesel_test_helper::test]
    fn test_write_jsonb_textj() {
        let mut buffer = Vec::new();
        let input_string = "hello\nworld"; // Contains a newline, requires escaping
        write_jsonb_string(input_string, &mut buffer).unwrap();

        let mut expected_buffer = Vec::new();
        expected_buffer.extend(create_jsonb_header(JSONB_TEXTJ, 12).unwrap());
        expected_buffer.extend_from_slice(b"hello\\nworld");

        assert_eq!(buffer, expected_buffer);
    }

    #[diesel_test_helper::test]
    fn test_write_jsonb_textj_quote_and_backslash() {
        let mut buffer = Vec::new();
        let input_string = r#"a"b\c"#;
        write_jsonb_string(input_string, &mut buffer).unwrap();

        let mut expected_buffer = Vec::new();
        expected_buffer.extend(create_jsonb_header(JSONB_TEXTJ, 7).unwrap());
        expected_buffer.extend_from_slice(br#"a\"b\\c"#);

        assert_eq!(buffer, expected_buffer);
    }

    #[diesel_test_helper::test]
    fn test_write_jsonb_text_keeps_high_control_free_text() {
        // U+007F is a control character json never escapes, so it stays plain TEXT
        let mut buffer = Vec::new();
        write_jsonb_string("a\u{7f}b", &mut buffer).unwrap();

        let mut expected_buffer = Vec::new();
        expected_buffer.extend(create_jsonb_header(JSONB_TEXT, 3).unwrap());
        expected_buffer.extend_from_slice("a\u{7f}b".as_bytes());

        assert_eq!(buffer, expected_buffer);
    }

    #[diesel_test_helper::test]
    fn test_write_jsonb_array() {
        let value = json!([1, true]);
        let mut buffer = Vec::new();
        write_jsonb_value(&value, &mut buffer).unwrap();

        let mut expected_buffer = Vec::new();
        expected_buffer.extend(create_jsonb_header(JSONB_ARRAY, 0x03).unwrap()); // Array header
        expected_buffer.extend(create_jsonb_header(JSONB_INT, 0x01).unwrap()); // Integer header
        expected_buffer.push(b'1'); // Integer payload "1"
        expected_buffer.extend(create_jsonb_header(JSONB_TRUE, 0x00).unwrap()); // Boolean header for "true"

        assert_eq!(buffer, expected_buffer);
    }

    #[diesel_test_helper::test]
    fn test_write_jsonb_object() {
        let value = json!({"key": 42});
        let mut buffer = Vec::new();
        write_jsonb_value(&value, &mut buffer).unwrap();

        let mut expected = Vec::new();
        expected.extend(create_jsonb_header(JSONB_OBJECT, 7).unwrap());
        expected.extend(create_jsonb_header(JSONB_TEXT, 3).unwrap());
        expected.extend_from_slice(b"key");
        expected.extend(create_jsonb_header(JSONB_INT, 2).unwrap());
        expected.extend_from_slice(b"42");

        assert_eq!(buffer, expected,);
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_to_sql_bool() {
        let conn = &mut connection();
        let res = diesel::select(json!(true).into_sql::<Jsonb>().eq(&sql("jsonb('true')")))
            .get_result::<bool>(conn)
            .unwrap();
        assert!(res);
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_to_sql_null() {
        let conn = &mut connection();
        let res = diesel::select(json!(null).into_sql::<Jsonb>().eq(&sql("jsonb('null')")))
            .get_result::<bool>(conn)
            .unwrap();
        assert!(res);
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_to_sql_integer() {
        let conn = &mut connection();
        let res = diesel::select(json!(42).into_sql::<Jsonb>().eq(&sql("jsonb('42')")))
            .get_result::<bool>(conn)
            .unwrap();
        assert!(res);
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_to_sql_float() {
        let conn = &mut connection();
        let res = diesel::select(json!(42.23).into_sql::<Jsonb>().eq(&sql("jsonb('42.23')")))
            .get_result::<bool>(conn)
            .unwrap();
        assert!(res);
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_to_sql_text() {
        let conn = &mut connection();

        // Test for TEXT (simple string)
        let res = diesel::select(
            json!("hello")
                .into_sql::<Jsonb>()
                .eq(&sql(r#"jsonb('"hello"')"#)),
        )
        .get_result::<bool>(conn)
        .unwrap();

        assert!(res);
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_to_sql_textj() {
        let conn = &mut connection();

        // Test for TEXTJ (JSON-escaped string, e.g., containing \n or \uXXXX)
        let res = diesel::select(
            json!("hello\nworld")
                .into_sql::<Jsonb>()
                .eq(&sql(r#"jsonb('"hello\nworld"')"#)), // The string is JSON-escaped
        )
        .get_result::<bool>(conn)
        .unwrap();

        assert!(res);
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_to_sql_textj_quote_and_backslash() {
        let conn = &mut connection();

        // a quote and a backslash are the other two characters sqlite writes as TEXTJ
        let res = diesel::select(
            json!(r#"a"b\c"#)
                .into_sql::<Jsonb>()
                .eq(&sql(r#"jsonb('"a\"b\\c"')"#)),
        )
        .get_result::<bool>(conn)
        .unwrap();

        assert!(res);
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_to_sql_array() {
        let conn = &mut connection();
        let res = diesel::select(
            json!([1, true, "foo"])
                .into_sql::<Jsonb>()
                .eq(&sql(r#"jsonb('[1, true, "foo"]')"#)),
        )
        .get_result::<bool>(conn)
        .unwrap();
        assert!(res);
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_to_sql_object() {
        let conn = &mut connection();
        let res = diesel::select(
            json!({"key": "value"})
                .into_sql::<Jsonb>()
                .eq(&sql(r#"jsonb('{"key": "value"}')"#)),
        )
        .get_result::<bool>(conn)
        .unwrap();
        assert!(res);
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_to_sql_object_in_object() {
        let conn = &mut connection();
        let json_value = json!({
            "outer_key": {
                "additional_key": true,
                "inner_key": {
                    "nested_key": 42
                },
            }
        });

        let res = diesel::select(json_value.into_sql::<Jsonb>().eq(&sql(
            r#"jsonb('{"outer_key": {"additional_key": true, "inner_key": {"nested_key": 42}}}')"#,
        )))
        .get_result::<bool>(conn)
        .unwrap();
        assert!(res);
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_to_sql_array_in_object() {
        let conn = &mut connection();
        let json_value = json!({
            "is_valid": false,
            "key": [1, 2, 3],
        });
        let res = diesel::select(
            json_value
                .into_sql::<Jsonb>()
                .eq(&sql(r#"jsonb('{"is_valid": false, "key": [1, 2, 3]}')"#)),
        )
        .get_result::<bool>(conn)
        .unwrap();
        assert!(res);
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_to_sql_object_in_array() {
        let conn = &mut connection();
        let json_value = json!([
            {
                "nested_key": "nested_value"
            },
            {
                "int_value": 99
            }
        ]);
        let res = diesel::select(json_value.into_sql::<Jsonb>().eq(&sql(
            r#"jsonb('[{"nested_key": "nested_value"}, {"int_value": 99}]')"#,
        )))
        .get_result::<bool>(conn)
        .unwrap();
        assert!(res);
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_from_sql_null() {
        let conn = &mut connection();
        let res = diesel::select(sql::<Jsonb>("jsonb('null')"))
            .get_result::<serde_json::Value>(conn)
            .unwrap();
        assert_eq!(res, serde_json::json!(null));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_from_sql_true() {
        let conn = &mut connection();
        let res = diesel::select(sql::<Jsonb>("jsonb('true')"))
            .get_result::<serde_json::Value>(conn)
            .unwrap();
        assert_eq!(res, serde_json::json!(true));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_from_sql_false() {
        let conn = &mut connection();
        let res = diesel::select(sql::<Jsonb>("jsonb('false')"))
            .get_result::<serde_json::Value>(conn)
            .unwrap();
        assert_eq!(res, serde_json::json!(false));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_from_sql_int() {
        let conn = &mut connection();
        let res = diesel::select(sql::<Jsonb>("jsonb('42')"))
            .get_result::<serde_json::Value>(conn)
            .unwrap();
        assert_eq!(res, serde_json::json!(42));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_from_sql_float() {
        let conn = &mut connection();
        let res = diesel::select(sql::<Jsonb>("jsonb('42.23')"))
            .get_result::<serde_json::Value>(conn)
            .unwrap();
        assert_eq!(res, serde_json::json!(42.23));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_from_sql_object() {
        let conn = &mut connection();
        let res = diesel::select(sql::<Jsonb>(r#"jsonb('{"key": "value"}')"#))
            .get_result::<serde_json::Value>(conn)
            .unwrap();
        assert_eq!(res, serde_json::json!({"key": "value"}));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_from_sql_array() {
        let conn = &mut connection();
        let res = diesel::select(sql::<Jsonb>("jsonb('[1, 2, 3]')"))
            .get_result::<serde_json::Value>(conn)
            .unwrap();
        assert_eq!(res, serde_json::json!([1, 2, 3]));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_from_sql_nested_objects() {
        let conn = &mut connection();
        let res = diesel::select(sql::<Jsonb>(r#"jsonb('{"outer": {"inner": 42}}')"#))
            .get_result::<serde_json::Value>(conn)
            .unwrap();
        assert_eq!(res, serde_json::json!({"outer": {"inner": 42}}));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_from_sql_nested_arrays() {
        let conn = &mut connection();
        let res = diesel::select(sql::<Jsonb>("jsonb('[[1, 2], [3, 4]]')"))
            .get_result::<serde_json::Value>(conn)
            .unwrap();
        assert_eq!(res, serde_json::json!([[1, 2], [3, 4]]));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_from_sql_nested_arrays_in_objects() {
        let conn = &mut connection();
        let res = diesel::select(sql::<Jsonb>(r#"jsonb('{"array": [1, 2, 3]}')"#))
            .get_result::<serde_json::Value>(conn)
            .unwrap();
        assert_eq!(res, serde_json::json!({"array": [1, 2, 3]}));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_from_sql_nested_objects_in_arrays() {
        let conn = &mut connection();
        let res = diesel::select(sql::<Jsonb>(
            r#"jsonb('[{"key1": "value1"}, {"key2": "value2"}]')"#,
        ))
        .get_result::<serde_json::Value>(conn)
        .unwrap();
        assert_eq!(
            res,
            serde_json::json!([{"key1": "value1"}, {"key2": "value2"}])
        );
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_from_sql_text() {
        let conn = &mut connection();
        let res = diesel::select(sql::<Jsonb>(r#"jsonb('"hello"')"#))
            .get_result::<serde_json::Value>(conn)
            .unwrap();
        assert_eq!(res, serde_json::json!("hello"));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn jsonb_from_sql_textj() {
        let conn = &mut connection();
        let res = diesel::select(sql::<Jsonb>(r#"jsonb('"hello\nworld"')"#))
            .get_result::<serde_json::Value>(conn)
            .unwrap();
        assert_eq!(res, serde_json::json!("hello\nworld"));
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn bad_json_from_sql() {
        let conn = &mut connection();
        let res = diesel::select(json!(true).into_sql::<Json>().eq(&sql("json('boom')")))
            .get_result::<bool>(conn);
        assert_eq!(format_error(&res.unwrap_err()), "malformed JSON");
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn bad_jsonb_from_sql() {
        let conn = &mut connection();
        let res = diesel::select(json!(true).into_sql::<Jsonb>().eq(&sql("jsonb('boom')")))
            .get_result::<bool>(conn);
        assert_eq!(format_error(&res.unwrap_err()), "malformed JSON");
    }

    #[diesel_test_helper::test]
    fn no_json_from_sql() {
        let uuid: Result<serde_json::Value, _> = FromSql::<Json, Sqlite>::from_nullable_sql(None);
        assert_eq!(
            format_error(&*uuid.unwrap_err()),
            "Unexpected null for non-null column"
        );
    }

    #[diesel_test_helper::test]
    fn no_jsonb_from_sql() {
        let uuid: Result<serde_json::Value, _> = FromSql::<Jsonb, Sqlite>::from_nullable_sql(None);
        assert_eq!(
            format_error(&*uuid.unwrap_err()),
            "Unexpected null for non-null column"
        );
    }

    #[cfg(all(
        not(miri),
        not(all(target_family = "wasm", target_os = "unknown")),
        unix
    ))]
    const RECURSION_DEPTH: usize = 2000;

    #[cfg(all(
        not(miri),
        any(windows, all(target_family = "wasm", target_os = "unknown"))
    ))]
    const RECURSION_DEPTH: usize = 1000;

    #[cfg(any(
        miri,
        all(
            not(unix),
            not(windows),
            not(all(target_family = "wasm", target_os = "unknown"))
        )
    ))]
    const RECURSION_DEPTH: usize = 10;

    #[diesel_test_helper::test]
    fn guard_against_stackoverflow_array() {
        let mut value = serde_json::Value::Number(42.into());
        for i in 0..RECURSION_DEPTH {
            value = serde_json::Value::Array(vec![value, serde_json::Value::Number(i.into())]);
        }
        // We compare the encoded buffer for both values here
        // as serde_json otherwise runs into stackoverflows itself
        let mut expected_buffer = Vec::new();
        write_jsonb_value(&value, &mut expected_buffer).unwrap();
        let res = read_jsonb_value(&expected_buffer).unwrap().0;

        let mut buffer = Vec::new();
        write_jsonb_value(&res, &mut buffer).unwrap();
        assert_eq!(expected_buffer, buffer);
    }

    #[diesel_test_helper::test]
    fn guard_against_stackoverflow_object() {
        let mut value = serde_json::Value::Number(42.into());
        for i in 0..RECURSION_DEPTH {
            let mut map = serde_json::Map::new();
            map.insert(format!("key_{i}"), value);
            value = serde_json::Value::Object(map);
        }

        // We compare the encoded buffer for both values here
        // as serde_json otherwise runs into stackoverflows itself
        let mut expected_buffer = Vec::new();
        write_jsonb_value(&value, &mut expected_buffer).unwrap();

        let res = read_jsonb_value(&expected_buffer).unwrap().0;

        let mut buffer = Vec::new();
        write_jsonb_value(&res, &mut buffer).unwrap();
        assert_eq!(expected_buffer, buffer);
    }

    #[diesel_test_helper::test]
    fn guard_against_stackoverflow_mixed() {
        // otherwise miri is too slow
        #[cfg(not(miri))]
        const SIZE: usize = 2000;
        #[cfg(miri)]
        const SIZE: usize = 20;

        let mut value = serde_json::Value::Number(42.into());
        for i in 0_usize..SIZE {
            if i.is_multiple_of(2) {
                let mut map = serde_json::Map::new();
                map.insert(format!("key_{i}"), value);
                value = serde_json::Value::Object(map);
            } else {
                value = serde_json::Value::Array(vec![value]);
            }
        }
        // We compare the encoded buffer for both values here
        // as serde_json otherwise runs into stackoverflows itself
        let mut expected_buffer = Vec::new();
        write_jsonb_value(&value, &mut expected_buffer).unwrap();
        let res = read_jsonb_value(&expected_buffer).unwrap().0;
        let mut buffer = Vec::new();
        write_jsonb_value(&res, &mut buffer).unwrap();
        assert_eq!(expected_buffer, buffer);
    }

    #[diesel_test_helper::test]
    #[cfg(not(miri))] // ffi call
    fn dangling_bytes_result_in_error() {
        let mut value = Vec::<u8>::new();
        value.extend(create_jsonb_header(JSONB_INT, 1).unwrap());
        value.push(b'1');
        value.push(42);
        assert_eq!(value.len(), 3);
        let conn = &mut connection();
        let res = diesel::select(
            crate::dsl::sql::<sql_types::Jsonb>("jsonb(?)").bind::<sql_types::Binary, _>(value),
        )
        .get_result::<serde_json::Value>(conn);
        assert!(res.is_err(), "{:?}", res.unwrap());
    }

    #[diesel_test_helper::test]
    fn object_key_without_value_results_in_error() {
        let mut value = Vec::new();
        value.extend(create_jsonb_header(JSONB_OBJECT, 2).unwrap());
        value.extend(create_jsonb_header(JSONB_TEXT, 1).unwrap());
        value.push(b'a');
        let res = read_jsonb_value(&value);
        assert!(res.is_err(), "{:?}", res.unwrap());
    }

    #[diesel_test_helper::test]
    fn check_invalid_size_header() {
        // 9-byte JSONB blob: first byte 0xFB (size_hint nibble = 0x0F),
        // bytes 1..9 = 0xFF -> encoded payload length = u64::MAX.
        let res = read_jsonb_value(&[0xFB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
        assert!(res.is_err());
    }

    #[diesel_test_helper::test]
    fn nested_container_cannot_cross_parent_boundary() {
        let res = read_jsonb_value(&[0x3B, 0x1B, 0x1B, JSONB_NULL]);
        assert!(res.is_err(), "{:?}", res.unwrap());
    }

    #[diesel_test_helper::test]
    fn check_signed_integer() {
        let mut buf = Vec::new();
        write_jsonb_value(&json!(-42), &mut buf).unwrap();
        let mut expected = create_jsonb_header(JSONB_INT, 3).unwrap();
        expected.extend(b"-42");
        assert_eq!(buf, expected);
    }
}