jsonx-lib 0.5.2

Strict RFC 8259 JSON: a borrowing parser, a pull reader, and a deterministic writer, with budgets checked before allocation and no recursion. No dependencies, no_std.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
//! Typed output: [`ToJson`], and the writers it writes through.
//!
//! A `ToJson` value writes itself straight into the output buffer, member by
//! member, without building a [`Value`] first. The writers are typed so that
//! the output is well-formed by construction: an [`ObjectWriter`] only takes
//! members, an [`ArrayWriter`] only takes items, and a [`ValueWriter`] is
//! consumed by the one value it writes, returning the [`Written`] proof that
//! `to_json` must return.

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

use crate::{Array, Format, Number, Object, RawValue, Value, number, write};

/// Takes the output buffer when it holds enough to pass on.
pub(crate) trait Flush {
    /// Passes `buf` on and empties it. A failure is kept by the
    /// implementation and reported when the write completes.
    fn flush(&mut self, buf: &mut String);
}

/// Output kept in a `String`: nothing to pass on.
pub(crate) struct Keep;

impl Flush for Keep {
    #[inline]
    fn flush(&mut self, _buf: &mut String) {}
}

/// Output to an [`io::Write`](std::io::Write), in chunks. The first error is
/// kept; after it, chunks are dropped.
#[cfg(feature = "std")]
pub(crate) struct IoSink<'a> {
    pub(crate) writer: &'a mut dyn std::io::Write,
    pub(crate) error: Option<std::io::Error>,
}

#[cfg(feature = "std")]
impl Flush for IoSink<'_> {
    fn flush(&mut self, buf: &mut String) {
        if self.error.is_none() {
            if let Err(e) = self.writer.write_all(buf.as_bytes()) {
                self.error = Some(e);
            }
        }
        buf.clear();
    }
}

/// Where a writer writes: the buffer, what to do when it fills, the layout,
/// and the indentation level of what is written next.
pub(crate) struct Out<'w> {
    pub(crate) buf: &'w mut String,
    pub(crate) sink: &'w mut dyn Flush,
    /// Hand the buffer to `sink` once it holds this many bytes.
    pub(crate) flush_at: usize,
    pub(crate) format: Format,
    pub(crate) depth: usize,
}

impl Out<'_> {
    /// The same output, at indentation level `depth`.
    #[inline]
    pub(crate) fn at(&mut self, depth: usize) -> Out<'_> {
        Out {
            buf: &mut *self.buf,
            sink: &mut *self.sink,
            flush_at: self.flush_at,
            format: self.format,
            depth,
        }
    }

    /// Ends a value: passes the buffer on if it is full enough.
    #[inline]
    fn done(&mut self) -> Written {
        if self.buf.len() >= self.flush_at {
            self.sink.flush(self.buf);
        }
        Written(())
    }
}

/// Proof that a [`ValueWriter`] wrote its value: what [`ToJson::to_json`]
/// returns.
///
/// Only a `ValueWriter` method makes one, and each consumes the writer, so
/// a `to_json` that compiles writes exactly one value: never none, never
/// two. There is nothing to do with a `Written` but return it.
#[must_use = "return it from `to_json`"]
#[derive(Debug)]
pub struct Written(pub(crate) ());

/// Writes one JSON value; see [`ToJson`].
///
/// Each method writes a value and consumes the writer. Strings are escaped
/// as [`write_string`](crate::write_string) describes; numbers are written
/// in their shortest exact form; containers are built in a closure that
/// gets an [`ObjectWriter`] or [`ArrayWriter`]. The layout (compact or
/// pretty) is the caller's choice and is applied for you.
///
/// # Examples
///
/// ```
/// use jsonx_lib::{ToJson, ValueWriter, Written};
///
/// struct Celsius(f64);
///
/// impl ToJson for Celsius {
///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
///         out.f64(self.0)
///     }
/// }
///
/// assert_eq!(jsonx_lib::to_string(&Celsius(21.5)), "21.5");
/// assert_eq!(jsonx_lib::to_string(&Celsius(f64::NAN)), "null");
/// ```
pub struct ValueWriter<'w> {
    out: Out<'w>,
}

impl<'w> ValueWriter<'w> {
    #[inline]
    pub(crate) const fn new(out: Out<'w>) -> Self {
        Self { out }
    }

    /// Writes `null`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use jsonx_lib::{ToJson, ValueWriter, Written};
    /// struct Nothing;
    /// impl ToJson for Nothing {
    ///     fn to_json(&self, out: ValueWriter<'_>) -> Written { out.null() }
    /// }
    /// assert_eq!(jsonx_lib::to_string(&Nothing), "null");
    /// ```
    #[inline]
    pub fn null(mut self) -> Written {
        self.out.buf.push_str("null");
        self.out.done()
    }

    /// Writes `true` or `false`.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(jsonx_lib::to_string(&true), "true");
    /// ```
    #[inline]
    pub fn bool(mut self, value: bool) -> Written {
        if value {
            self.out.buf.push_str("true");
        } else {
            self.out.buf.push_str("false");
        }
        self.out.done()
    }

    /// Writes an integer in decimal.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(jsonx_lib::to_string(&-42i64), "-42");
    /// ```
    #[inline]
    pub fn i64(mut self, value: i64) -> Written {
        number::write_i64(self.out.buf, value);
        self.out.done()
    }

    /// Writes an integer in decimal.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(jsonx_lib::to_string(&u64::MAX), "18446744073709551615");
    /// ```
    #[inline]
    pub fn u64(mut self, value: u64) -> Written {
        number::write_u64(self.out.buf, value);
        self.out.done()
    }

    /// Writes a 128-bit integer in decimal, exactly (JSON numbers have no
    /// size limit; a reader that stores them as `f64` will round it).
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(jsonx_lib::to_string(&i128::MIN), "-170141183460469231731687303715884105728");
    /// ```
    #[inline]
    pub fn i128(mut self, value: i128) -> Written {
        number::write_i128(self.out.buf, value);
        self.out.done()
    }

    /// Writes a 128-bit unsigned integer in decimal, exactly.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(jsonx_lib::to_string(&u128::MAX), "340282366920938463463374607431768211455");
    /// ```
    #[inline]
    pub fn u128(mut self, value: u128) -> Written {
        number::write_u128(self.out.buf, value);
        self.out.done()
    }

    /// Writes the shortest text that reads back as the same `f64`, laid out
    /// as [`Number::from_f64`] describes (`1.0`, `0.1`, `1e100`). NaN and the
    /// infinities, which JSON cannot represent, are written as `null`, as
    /// [`Value::from`] makes them.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(jsonx_lib::to_string(&1.0f64), "1.0");
    /// assert_eq!(jsonx_lib::to_string(&f64::INFINITY), "null");
    /// ```
    #[inline]
    pub fn f64(mut self, value: f64) -> Written {
        if !number::write_f64_as(self.out.buf, value, self.out.format) {
            self.out.buf.push_str("null");
        }
        self.out.done()
    }

    /// Writes the shortest text that reads back as the same `f32` (so
    /// `0.1f32` is `0.1`); NaN and the infinities as `null`.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(jsonx_lib::to_string(&0.1f32), "0.1");
    /// ```
    #[inline]
    pub fn f32(mut self, value: f32) -> Written {
        if !number::write_f32_as(self.out.buf, value, self.out.format) {
            self.out.buf.push_str("null");
        }
        self.out.done()
    }

    /// Writes a [`Number`]'s exact text.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::Number;
    ///
    /// let price = Number::from_text("19.990").unwrap();
    /// assert_eq!(jsonx_lib::to_string(&price), "19.990");
    /// ```
    #[inline]
    pub fn number(mut self, value: &Number<'_>) -> Written {
        self.out.buf.push_str(value.as_str());
        self.out.done()
    }

    /// Writes a string, escaped as [`write_string`](crate::write_string)
    /// describes.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(jsonx_lib::to_string("say \"hi\""), r#""say \"hi\"""#);
    /// ```
    #[inline]
    pub fn str(mut self, value: &str) -> Written {
        write::write_str(self.out.buf, value, self.out.format);
        self.out.done()
    }

    /// Writes a [`RawValue`]'s text: already valid JSON, so it is not
    /// checked again. In the plain formats it is copied unchanged (in the
    /// pretty layout it keeps its own spacing), U+2028 and U+2029 inside
    /// its strings included: valid JSON, though
    /// [`write_string`](crate::write_string) escapes them in the strings it
    /// writes, for JavaScript before ES2019. For text that will be pasted
    /// into JavaScript source, use [`Format::html_safe`](crate::Format::html_safe),
    /// which escapes them in raw text too. Under
    /// [`Format::html_safe`](crate::Format::html_safe) or
    /// [`Format::ascii`](crate::Format::ascii), the characters that format
    /// escapes are escaped inside the raw text's strings too (the only place
    /// valid JSON can hold them), so `</script>` in a forwarded value is
    /// written `\u003c/script\u003e` and the format's promise holds; the
    /// value it stands for is unchanged.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{RawValue, ToJson, ValueWriter, Written};
    ///
    /// struct Forward<'a> { id: u64, result: RawValue<'a> }
    ///
    /// impl ToJson for Forward<'_> {
    ///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
    ///         out.object(|o| {
    ///             o.member("id", &self.id);
    ///             o.member_with("result", |w| w.raw(&self.result));
    ///         })
    ///     }
    /// }
    ///
    /// let result = RawValue::new(r#"{"items": [1, 2]}"#)?;
    /// let reply = Forward { id: 3, result };
    /// assert_eq!(jsonx_lib::to_string(&reply), r#"{"id":3,"result":{"items": [1, 2]}}"#);
    /// # Ok::<(), jsonx_lib::Error>(())
    /// ```
    #[inline]
    pub fn raw(mut self, value: &RawValue<'_>) -> Written {
        crate::write::write_raw(self.out.buf, value.as_str(), self.out.format);
        self.out.done()
    }

    /// Writes a [`Value`] (iterative at any depth), in the writer's layout.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{json, ToJson, Value, ValueWriter, Written};
    ///
    /// struct Extra<'a>(Value<'a>);
    /// impl ToJson for Extra<'_> {
    ///     fn to_json(&self, out: ValueWriter<'_>) -> Written { out.value(&self.0) }
    /// }
    /// assert_eq!(jsonx_lib::to_string(&Extra(json!([1, {}]))), "[1,{}]");
    /// ```
    pub fn value(mut self, value: &Value<'_>) -> Written {
        let Out {
            buf,
            sink,
            flush_at,
            format,
            depth,
        } = &mut self.out;
        let Ok(()) = write::emit::<Infallible>(buf, value, *format, *depth, *flush_at, &mut |b| {
            sink.flush(b);
            Ok(())
        });
        self.out.done()
    }

    /// Writes `value` through its own [`ToJson`]: `out.json(&x)` is
    /// `x.to_json(out)`.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{ToJson, ValueWriter, Written};
    ///
    /// struct Id(u32);
    /// impl ToJson for Id {
    ///     fn to_json(&self, out: ValueWriter<'_>) -> Written { out.json(&self.0) }
    /// }
    /// assert_eq!(jsonx_lib::to_string(&Id(7)), "7");
    /// ```
    #[inline]
    pub fn json<T: ToJson + ?Sized>(self, value: &T) -> Written {
        value.to_json(self)
    }

    /// Writes an object whose members `members` adds, in the order it adds
    /// them.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{ToJson, ValueWriter, Written};
    ///
    /// struct Position { line: u32, character: u32 }
    ///
    /// impl ToJson for Position {
    ///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
    ///         out.object(|o| {
    ///             o.member("line", &self.line);
    ///             o.member("character", &self.character);
    ///         })
    ///     }
    /// }
    ///
    /// let p = Position { line: 3, character: 14 };
    /// assert_eq!(jsonx_lib::to_string(&p), r#"{"line":3,"character":14}"#);
    /// assert_eq!(jsonx_lib::to_string_pretty(&p), "{\n  \"line\": 3,\n  \"character\": 14\n}");
    /// ```
    #[inline]
    pub fn object(mut self, members: impl FnOnce(&mut ObjectWriter<'_>)) -> Written {
        self.out.buf.push('{');
        let depth = self.out.depth;
        let mut object = ObjectWriter {
            out: self.out.at(depth + 1),
            first: true,
        };
        members(&mut object);
        let empty = object.first;
        self.close(empty, '}')
    }

    /// Writes an array whose items `items` adds.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{ToJson, ValueWriter, Written};
    ///
    /// struct Range { start: u32, end: u32 }
    ///
    /// impl ToJson for Range {
    ///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
    ///         out.array(|a| {
    ///             a.item(&self.start);
    ///             a.item(&self.end);
    ///         })
    ///     }
    /// }
    ///
    /// assert_eq!(jsonx_lib::to_string(&Range { start: 1, end: 4 }), "[1,4]");
    /// ```
    #[inline]
    pub fn array(mut self, items: impl FnOnce(&mut ArrayWriter<'_>)) -> Written {
        self.out.buf.push('[');
        let depth = self.out.depth;
        let mut array = ArrayWriter {
            out: self.out.at(depth + 1),
            first: true,
        };
        items(&mut array);
        let empty = array.first;
        self.close(empty, ']')
    }

    /// Closes a container: on its own line in the pretty layout, unless it
    /// is empty (`[]`, `{}`).
    #[inline]
    fn close(mut self, empty: bool, bracket: char) -> Written {
        if !empty && self.out.format.pretty {
            write::newline(self.out.buf, self.out.depth, self.out.format);
        }
        self.out.buf.push(bracket);
        self.out.done()
    }
}

/// Adds the members of an object being written; see
/// [`ValueWriter::object`].
///
/// Members are written in the order they are added. Nothing stops a key
/// from being added twice; the reader of the output decides what that means
/// (this crate's parser refuses it by default), so add each key once.
pub struct ObjectWriter<'w> {
    out: Out<'w>,
    first: bool,
}

impl ObjectWriter<'_> {
    /// Writes the separator, the indentation, and the key with its colon.
    /// Always inlined, down to the string's scan: keys are mostly literals,
    /// and inlined into the caller the scan of a literal is done by the
    /// compiler.
    #[inline(always)]
    fn key(&mut self, key: &str) {
        if !self.first {
            self.out.buf.push(',');
        }
        self.first = false;
        if self.out.format.pretty {
            write::newline(self.out.buf, self.out.depth, self.out.format);
        }
        write::write_str(self.out.buf, key, self.out.format);
        // Two calls, not one with a length chosen at run time: each is a
        // constant store.
        if self.out.format.pretty {
            self.out.buf.push_str(": ");
        } else {
            self.out.buf.push(':');
        }
    }

    /// Adds the member `key: value`. An `Option` that is `None` is written as
    /// `null`; to leave the member out instead, use
    /// [`optional`](Self::optional).
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{ToJson, ValueWriter, Written};
    ///
    /// struct Reply<'a> { id: u64, result: Option<&'a str> }
    ///
    /// impl ToJson for Reply<'_> {
    ///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
    ///         out.object(|o| {
    ///             o.member("jsonrpc", "2.0");
    ///             o.member("id", &self.id);
    ///             o.member("result", &self.result); // `null` when None
    ///         })
    ///     }
    /// }
    ///
    /// let reply = Reply { id: 1, result: None };
    /// assert_eq!(jsonx_lib::to_string(&reply), r#"{"jsonrpc":"2.0","id":1,"result":null}"#);
    /// ```
    #[inline]
    pub fn member<T: ToJson + ?Sized>(&mut self, key: &str, value: &T) {
        self.key(key);
        let depth = self.out.depth;
        let Written(()) = value.to_json(ValueWriter::new(self.out.at(depth)));
    }

    /// Adds the member `key: value`, unless `value` is absent
    /// ([`ToJson::is_absent`]: an `Option` that is `None`). This is
    /// TypeScript's optional `key?: T`, as LSP and MCP use it.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{ToJson, ValueWriter, Written};
    ///
    /// struct Item { label: String, detail: Option<String> }
    ///
    /// impl ToJson for Item {
    ///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
    ///         out.object(|o| {
    ///             o.member("label", &self.label);
    ///             o.optional("detail", &self.detail);
    ///         })
    ///     }
    /// }
    ///
    /// let bare = Item { label: "x".into(), detail: None };
    /// assert_eq!(jsonx_lib::to_string(&bare), r#"{"label":"x"}"#);
    /// let full = Item { label: "x".into(), detail: Some("fn x()".into()) };
    /// assert_eq!(jsonx_lib::to_string(&full), r#"{"label":"x","detail":"fn x()"}"#);
    /// ```
    #[inline]
    pub fn optional<T: ToJson + ?Sized>(&mut self, key: &str, value: &T) {
        if !value.is_absent() {
            self.member(key, value);
        }
    }

    /// Adds the member `key`, its value written by `value`.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{ToJson, ValueWriter, Written};
    ///
    /// struct Hover { markdown: String }
    ///
    /// impl ToJson for Hover {
    ///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
    ///         out.object(|o| {
    ///             o.member_with("contents", |w| {
    ///                 w.object(|c| {
    ///                     c.member("kind", "markdown");
    ///                     c.member("value", &self.markdown);
    ///                 })
    ///             });
    ///         })
    ///     }
    /// }
    ///
    /// let h = Hover { markdown: "**x**".into() };
    /// assert_eq!(jsonx_lib::to_string(&h), r#"{"contents":{"kind":"markdown","value":"**x**"}}"#);
    /// ```
    #[inline]
    pub fn member_with(&mut self, key: &str, value: impl FnOnce(ValueWriter<'_>) -> Written) {
        self.key(key);
        let depth = self.out.depth;
        let Written(()) = value(ValueWriter::new(self.out.at(depth)));
    }
}

/// Adds the items of an array being written; see [`ValueWriter::array`].
pub struct ArrayWriter<'w> {
    out: Out<'w>,
    first: bool,
}

impl ArrayWriter<'_> {
    /// Writes the separator and the indentation, and returns the writer for
    /// the item.
    #[inline]
    fn next(&mut self) -> ValueWriter<'_> {
        if !self.first {
            self.out.buf.push(',');
        }
        self.first = false;
        let depth = self.out.depth;
        if self.out.format.pretty {
            write::newline(self.out.buf, depth, self.out.format);
        }
        ValueWriter::new(self.out.at(depth))
    }

    /// Adds `value` as the next item.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{ToJson, ValueWriter, Written};
    ///
    /// struct Pair(&'static str, i32);
    /// impl ToJson for Pair {
    ///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
    ///         out.array(|a| {
    ///             a.item(self.0);
    ///             a.item(&self.1);
    ///         })
    ///     }
    /// }
    /// assert_eq!(jsonx_lib::to_string(&Pair("x", -1)), r#"["x",-1]"#);
    /// ```
    #[inline]
    pub fn item<T: ToJson + ?Sized>(&mut self, value: &T) {
        let Written(()) = value.to_json(self.next());
    }

    /// Adds the next item, written by `value`.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{ToJson, ValueWriter, Written};
    ///
    /// struct Squares(u32);
    /// impl ToJson for Squares {
    ///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
    ///         out.array(|a| {
    ///             for n in 1..=self.0 {
    ///                 a.item_with(|w| w.u64(u64::from(n * n)));
    ///             }
    ///         })
    ///     }
    /// }
    /// assert_eq!(jsonx_lib::to_string(&Squares(3)), "[1,4,9]");
    /// ```
    #[inline]
    pub fn item_with(&mut self, value: impl FnOnce(ValueWriter<'_>) -> Written) {
        let Written(()) = value(self.next());
    }

    /// Adds every item of `items`, in order.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{ToJson, ValueWriter, Written};
    ///
    /// struct Evens(u32);
    /// impl ToJson for Evens {
    ///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
    ///         out.array(|a| a.items((0..self.0).map(|n| n * 2)))
    ///     }
    /// }
    /// assert_eq!(jsonx_lib::to_string(&Evens(3)), "[0,2,4]");
    /// ```
    pub fn items<I>(&mut self, items: I)
    where
        I: IntoIterator,
        I::Item: ToJson,
    {
        for item in items {
            self.item(&item);
        }
    }
}

/// A type that writes itself as JSON, straight into the output.
///
/// This is the explicit, hand-written half of jsonx-lib's typed API (the
/// other half is [`FromJson`](crate::FromJson)). There is no derive and no
/// reflection: an implementation says, in code, which members a type has and
/// what their keys are, and the compiler checks that it writes exactly one
/// well-formed value. Nothing is built in between: no [`Value`], no
/// intermediate strings.
///
/// # Implementing it
///
/// Write the value with the [`ValueWriter`] you are given and return what
/// its method returns. A struct is an object, a member per field; a newtype
/// usually writes its inner value; an enum writes whichever shape each
/// variant has.
///
/// ```
/// use jsonx_lib::{ToJson, ValueWriter, Written};
///
/// struct Position { line: u32, character: u32 }
/// struct Range { start: Position, end: Position }
/// enum Id { Number(i64), Text(String) }
/// struct Diagnostic { range: Range, message: String, code: Option<Id>, tags: Vec<u8> }
///
/// impl ToJson for Position {
///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
///         out.object(|o| {
///             o.member("line", &self.line);
///             o.member("character", &self.character);
///         })
///     }
/// }
///
/// impl ToJson for Range {
///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
///         out.object(|o| {
///             o.member("start", &self.start);
///             o.member("end", &self.end);
///         })
///     }
/// }
///
/// impl ToJson for Id {
///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
///         match self {
///             Id::Number(n) => out.i64(*n),
///             Id::Text(s) => out.str(s),
///         }
///     }
/// }
///
/// impl ToJson for Diagnostic {
///     fn to_json(&self, out: ValueWriter<'_>) -> Written {
///         out.object(|o| {
///             o.member("range", &self.range);
///             o.member("message", &self.message);
///             o.optional("code", &self.code); // left out when None
///             if !self.tags.is_empty() {
///                 o.member("tags", &self.tags);
///             }
///         })
///     }
/// }
///
/// let d = Diagnostic {
///     range: Range { start: Position { line: 1, character: 0 }, end: Position { line: 1, character: 5 } },
///     message: "unused \"x\"".into(),
///     code: Some(Id::Text("W01".into())),
///     tags: vec![1],
/// };
/// assert_eq!(
///     jsonx_lib::to_string(&d),
///     r#"{"range":{"start":{"line":1,"character":0},"end":{"line":1,"character":5}},"message":"unused \"x\"","code":"W01","tags":[1]}"#
/// );
/// ```
///
/// The rules the writers enforce:
///
/// - **Exactly one value.** Each [`ValueWriter`] method consumes the writer
///   and returns the [`Written`] that `to_json` must return.
/// - **Members only in objects, items only in arrays.** The closure of
///   [`object`](ValueWriter::object) gets an [`ObjectWriter`], which only
///   adds members; [`array`](ValueWriter::array)'s gets an [`ArrayWriter`].
/// - **Escaping is not your job.** Keys and strings are escaped by the
///   writer; no string can break out of its quotes.
/// - **Layout is not your job.** The same impl writes compact
///   ([`to_string`](crate::to_string)) and pretty
///   ([`to_string_pretty`](crate::to_string_pretty)) output.
///
/// Writing never fails from the implementation's point of view: an I/O
/// error of the underlying writer is kept and returned by the call that
/// started the write ([`to_writer`](crate::to_writer),
/// [`Writer::value`](crate::Writer::value)). A type whose values nest as
/// deep as their data (a tree of its own nodes) recurses once per level;
/// [`Value`] does not, and is the type to use for untrusted depth.
///
/// `ToJson` is object-safe: `&dyn ToJson` and `Box<dyn ToJson>` work, for
/// a list of differently typed values.
///
/// # Provided implementations
///
/// `bool`; every integer type (exact decimal); `f32` and `f64` (shortest
/// round-trip text, NaN and the infinities as `null`); `str`, `String`,
/// `Cow<str>`, `Box<str>`, `char`; `()` (`null`); `Option<T>` (`None` is
/// `null`, or left out by [`ObjectWriter::optional`]); `&T`, `&mut T`,
/// `Box<T>`, `Rc<T>`, `Arc<T>`; `[T]`, `[T; N]`, `Vec<T>`, `VecDeque<T>`,
/// `BTreeSet<T>`, and with `std` `HashSet<T, S>` (arrays); tuples of up to
/// eight (arrays); `BTreeMap<K, V>` and with `std` `HashMap<K, V, S>`
/// (objects, keys through [`ToJsonKey`]; a `HashMap` writes in its own
/// order, so use a `BTreeMap` for deterministic output); and this crate's
/// [`Value`], [`Number`], [`Array`], [`Object`], [`RawValue`], and
/// [`Nullable`](crate::Nullable).
pub trait ToJson {
    /// Writes `self` as one JSON value.
    fn to_json(&self, out: ValueWriter<'_>) -> Written;

    /// Whether [`ObjectWriter::optional`] leaves this member out. `false`,
    /// except for `Option<T>`, where it is `is_none()`, and the pointer types,
    /// which ask what they point to.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::ToJson;
    ///
    /// assert!(None::<u8>.is_absent());
    /// assert!(!Some(0u8).is_absent());
    /// assert!(!0u8.is_absent());
    /// ```
    #[inline]
    fn is_absent(&self) -> bool {
        false
    }
}

/// A type usable as a JSON object key, for [`ToJson`] on maps.
///
/// # Examples
///
/// ```
/// use std::borrow::Cow;
/// use std::collections::BTreeMap;
/// use jsonx_lib::ToJsonKey;
///
/// #[derive(PartialEq, Eq, PartialOrd, Ord)]
/// struct Uri(String);
///
/// impl ToJsonKey for Uri {
///     fn to_json_key(&self) -> Cow<'_, str> {
///         Cow::Borrowed(&self.0)
///     }
/// }
///
/// let mut changes = BTreeMap::new();
/// changes.insert(Uri("file:///a.rs".into()), vec![1, 2]);
/// assert_eq!(jsonx_lib::to_string(&changes), r#"{"file:///a.rs":[1,2]}"#);
/// ```
pub trait ToJsonKey {
    /// The key's text (escaped by the writer).
    fn to_json_key(&self) -> Cow<'_, str>;
}

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

impl ToJson for bool {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.bool(*self)
    }
}

macro_rules! to_json_integer {
    ($($t:ty => $method:ident as $via:ty),* $(,)?) => {$(
        impl ToJson for $t {
            #[inline]
            fn to_json(&self, out: ValueWriter<'_>) -> Written {
                out.$method(<$via>::from(*self))
            }
        }
    )*};
}

to_json_integer!(
    i8 => i64 as i64, i16 => i64 as i64, i32 => i64 as i64, i64 => i64 as i64,
    u8 => u64 as u64, u16 => u64 as u64, u32 => u64 as u64, u64 => u64 as u64,
    i128 => i128 as i128, u128 => u128 as u128,
);

impl ToJson for isize {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        // 64 bits or fewer on every target Rust supports today, so the
        // 128-bit path is compiled out; it stays for a wider target.
        match i64::try_from(*self) {
            Ok(n) => out.i64(n),
            Err(_) => out.i128(*self as i128),
        }
    }
}

impl ToJson for usize {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        match u64::try_from(*self) {
            Ok(n) => out.u64(n),
            Err(_) => out.u128(*self as u128),
        }
    }
}

impl ToJson for f64 {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.f64(*self)
    }
}

impl ToJson for f32 {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.f32(*self)
    }
}

impl ToJson for str {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.str(self)
    }
}

impl ToJson for String {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.str(self)
    }
}

impl ToJson for Cow<'_, str> {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.str(self)
    }
}

impl ToJson for char {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        let mut utf8 = [0u8; 4];
        out.str(self.encode_utf8(&mut utf8))
    }
}

impl ToJson for () {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.null()
    }
}

impl<T: ToJson> ToJson for Option<T> {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        match self {
            Some(value) => value.to_json(out),
            None => out.null(),
        }
    }

    #[inline]
    fn is_absent(&self) -> bool {
        self.is_none()
    }
}

macro_rules! to_json_pointer {
    ($($t:ty),*) => {$(
        impl<T: ToJson + ?Sized> ToJson for $t {
            #[inline]
            fn to_json(&self, out: ValueWriter<'_>) -> Written {
                (**self).to_json(out)
            }

            #[inline]
            fn is_absent(&self) -> bool {
                (**self).is_absent()
            }
        }
    )*};
}

to_json_pointer!(&T, &mut T, Box<T>, Rc<T>);
#[cfg(target_has_atomic = "ptr")]
to_json_pointer!(Arc<T>);

impl<T: ToJson> ToJson for [T] {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.array(|a| a.items(self))
    }
}

impl<T: ToJson, const N: usize> ToJson for [T; N] {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        self.as_slice().to_json(out)
    }
}

impl<T: ToJson> ToJson for Vec<T> {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        self.as_slice().to_json(out)
    }
}

impl<T: ToJson> ToJson for VecDeque<T> {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.array(|a| a.items(self))
    }
}

impl<T: ToJson> ToJson for BTreeSet<T> {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.array(|a| a.items(self))
    }
}

#[cfg(feature = "std")]
impl<T: ToJson, S> ToJson for std::collections::HashSet<T, S> {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.array(|a| a.items(self))
    }
}

macro_rules! to_json_tuple {
    ($(($($name:ident),+))*) => {$(
        impl<$($name: ToJson),+> ToJson for ($($name,)+) {
            #[allow(non_snake_case)]
            fn to_json(&self, out: ValueWriter<'_>) -> Written {
                let ($($name,)+) = self;
                out.array(|a| {
                    $(a.item($name);)+
                })
            }
        }
    )*};
}

to_json_tuple! {
    (A)
    (A, B)
    (A, B, C)
    (A, B, C, D)
    (A, B, C, D, E)
    (A, B, C, D, E, F)
    (A, B, C, D, E, F, G)
    (A, B, C, D, E, F, G, H)
}

impl<K: ToJsonKey, V: ToJson> ToJson for BTreeMap<K, V> {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.object(|o| {
            for (key, value) in self {
                o.member(&key.to_json_key(), value);
            }
        })
    }
}

#[cfg(feature = "std")]
impl<K: ToJsonKey, V: ToJson, S> ToJson for std::collections::HashMap<K, V, S> {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.object(|o| {
            for (key, value) in self {
                o.member(&key.to_json_key(), value);
            }
        })
    }
}

impl ToJson for Value<'_> {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.value(self)
    }
}

impl ToJson for Number<'_> {
    #[inline]
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.number(self)
    }
}

impl ToJson for Array<'_> {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.array(|a| a.items(self.iter()))
    }
}

impl ToJson for Object<'_> {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.object(|o| {
            for (key, value) in self {
                o.member(key, value);
            }
        })
    }
}

impl ToJsonKey for str {
    #[inline]
    fn to_json_key(&self) -> Cow<'_, str> {
        Cow::Borrowed(self)
    }
}

impl ToJsonKey for String {
    #[inline]
    fn to_json_key(&self) -> Cow<'_, str> {
        Cow::Borrowed(self)
    }
}

impl ToJsonKey for Cow<'_, str> {
    #[inline]
    fn to_json_key(&self) -> Cow<'_, str> {
        Cow::Borrowed(self)
    }
}

impl ToJsonKey for Box<str> {
    #[inline]
    fn to_json_key(&self) -> Cow<'_, str> {
        Cow::Borrowed(self)
    }
}

impl ToJsonKey for char {
    #[inline]
    fn to_json_key(&self) -> Cow<'_, str> {
        let mut utf8 = [0u8; 4];
        Cow::Owned(String::from(&*self.encode_utf8(&mut utf8)))
    }
}

impl<T: ToJsonKey + ?Sized> ToJsonKey for &T {
    #[inline]
    fn to_json_key(&self) -> Cow<'_, str> {
        (**self).to_json_key()
    }
}

#[cfg(test)]
mod tests {
    use alloc::{string::ToString, vec};

    use super::*;
    use crate::{json, to_string, to_string_pretty};

    #[test]
    fn test_provided_impls_write_their_shapes() {
        assert_eq!(
            to_string(&(1u8, "two", 3.5f64, None::<u8>, ())),
            r#"[1,"two",3.5,null,null]"#
        );
        assert_eq!(to_string(&[[1, 2], [3, 4]]), "[[1,2],[3,4]]");
        assert_eq!(to_string(&Vec::<u8>::new()), "[]");
        assert_eq!(to_string(&'q'), r#""q""#);
        assert_eq!(to_string(&Box::new(Some(-7i32))), "-7");
        assert_eq!(to_string(&Rc::new("rc")), r#""rc""#);
        assert_eq!(to_string(&VecDeque::from(vec![true])), "[true]");
        assert_eq!(to_string(&BTreeSet::from([2, 1])), "[1,2]");
        let map: BTreeMap<&str, Vec<u8>> = [("b", vec![]), ("a", vec![1])].into_iter().collect();
        assert_eq!(to_string(&map), r#"{"a":[1],"b":[]}"#);
        let chars: BTreeMap<char, u8> = [('x', 1)].into_iter().collect();
        assert_eq!(to_string(&chars), r#"{"x":1}"#);
        let cow: BTreeMap<Cow<'_, str>, Box<str>> =
            [(Cow::Borrowed("k"), Box::from("v"))].into_iter().collect();
        assert_eq!(to_string(&cow), r#"{"k":"v"}"#);
        let boxed: BTreeMap<Box<str>, String> =
            [(Box::from("k"), "v".to_string())].into_iter().collect();
        assert_eq!(to_string(&boxed), r#"{"k":"v"}"#);
        assert_eq!(
            to_string(&(isize::MIN, usize::MAX)),
            alloc::format!("[{},{}]", isize::MIN, usize::MAX)
        );
        assert_eq!(to_string(&(1, 2, 3, 4, 5, 6, 7, 8)), "[1,2,3,4,5,6,7,8]");
        assert_eq!(to_string(&f32::NAN), "null");
        #[cfg(target_has_atomic = "ptr")]
        assert_eq!(to_string(&Arc::new(1u8)), "1");
        let mut n = 5u8;
        assert_eq!(to_string(&&mut n), "5");
    }

    #[test]
    fn test_crate_types_write_as_themselves() {
        let v = json!({ "a": [1, "x", null, {}], "b": { "c": 2.50 } });
        assert_eq!(to_string(&v), v.to_string());
        let arr = v["a"].as_array().cloned().unwrap_or_default();
        assert_eq!(to_string(&arr), r#"[1,"x",null,{}]"#);
        let obj = v.as_object().cloned().unwrap_or_default();
        assert_eq!(to_string(&obj), v.to_string());
        assert_eq!(to_string_pretty(&obj), to_string_pretty(&v));
        assert_eq!(to_string(&Number::from(3)), "3");
    }

    #[test]
    fn test_values_nested_in_typed_output_keep_the_pretty_layout() {
        struct Wrap<'a>(Value<'a>, Vec<u8>);
        impl ToJson for Wrap<'_> {
            fn to_json(&self, out: ValueWriter<'_>) -> Written {
                out.object(|o| {
                    o.member("v", &self.0);
                    o.member("list", &self.1);
                    o.member_with("empty", |w| w.object(|_| {}));
                    o.member_with("none", |w| w.array(|_| {}));
                })
            }
        }
        let w = Wrap(json!({ "x": [1, { "y": [] }] }), vec![7]);
        let expected = json!({
            "v": { "x": [1, { "y": [] }] },
            "list": [7],
            "empty": {},
            "none": [],
        });
        assert_eq!(to_string_pretty(&w), to_string_pretty(&expected));
        assert_eq!(to_string(&w), to_string(&expected));
    }

    #[test]
    fn test_dyn_to_json_and_absence() {
        let parts: [&dyn ToJson; 3] = [&1u8, &"s", &None::<u8>];
        assert_eq!(
            to_string(&parts.as_slice().iter().collect::<Vec<_>>()),
            r#"[1,"s",null]"#
        );
        let boxed: Box<Option<u8>> = Box::new(None);
        assert!(boxed.is_absent());
        assert!(!().is_absent());
    }

    #[cfg(feature = "std")]
    #[test]
    fn test_std_collections() {
        let map: std::collections::HashMap<String, u8> =
            [("k".to_string(), 1)].into_iter().collect();
        assert_eq!(to_string(&map), r#"{"k":1}"#);
        let set: std::collections::HashSet<u8> = [3].into_iter().collect();
        assert_eq!(to_string(&set), "[3]");
    }
}