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
//! The writer: deterministic compact and pretty output with complete escaping.

use alloc::{borrow::Cow, string::String};
use core::{fmt, slice};

use crate::{
    Format, ToJson, Value,
    encode::{Keep, Out, ValueWriter, Written},
    scan,
    tree::Member,
};

/// Spaces for indentation, copied in slices.
const SPACES: &str = "                                                                ";

/// Appends `text` to `out` as a JSON string literal, quotes included.
///
/// The escaping is complete and the same every time:
///
/// - `"` and `\` are escaped as `\"` and `\\`;
/// - the control characters U+0000 to U+001F are escaped, with the short forms
///   `\b \f \n \r \t` where JSON has one and `\u00XX` (lowercase hex)
///   otherwise;
/// - U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR are escaped as
///   `\u2028` and `\u2029`: JSON allows them raw, but JavaScript before ES2019
///   ends a line at them, so raw ones break JSON embedded in a script;
/// - everything else, including `/`, DEL, and all other non-ASCII text, is
///   copied unchanged as UTF-8.
///
/// This escaping is not HTML-safe: `<`, `>`, and `&` are copied, so
/// `</script>` inside a string stays `</script>` in the output. For JSON
/// placed inside HTML, write with [`Format::html_safe`] (and
/// [`Format::ascii`] for a channel that is not 8-bit clean).
///
/// So no string can end the literal early or put a raw line break in it,
/// whatever it contains: the output is always exactly one JSON string token
/// that decodes back to `text`. A string with nothing to escape (most keys
/// and values) is tested a word at a time and copied whole; otherwise the
/// unescaped stretches are copied in bulk, found eight bytes at a time.
///
/// # Examples
///
/// ```
/// let mut out = String::from("{\"msg\":");
/// jsonx_lib::write_string(&mut out, "say \"hi\"\n\u{2028}");
/// out.push('}');
/// assert_eq!(out, r#"{"msg":"say \"hi\"\n\u2028"}"#);
/// ```
#[inline(always)]
pub fn write_string(out: &mut String, text: &str) {
    let plain = scan::plain_prefix_for_write(text.as_bytes());
    if plain == text.len() {
        out.reserve(text.len() + 2);
        out.push('"');
        out.push_str(text);
        out.push('"');
    } else {
        write_string_escaped(out, text, plain);
    }
}

/// [`write_string`] for a string with at least one byte that may need
/// escaping, none of them in `text[..plain]`.
#[inline(never)]
fn write_string_escaped(out: &mut String, text: &str, plain: usize) {
    out.reserve(text.len() + 2);
    out.push('"');
    let bytes = text.as_bytes();
    let mut from = 0;
    let mut at = plain;
    loop {
        at = scan::find_write_special(bytes, at);
        let Some(&b) = bytes.get(at) else { break };
        let escape: &str = match b {
            b'"' => "\\\"",
            b'\\' => "\\\\",
            b'\n' => "\\n",
            b'\r' => "\\r",
            b'\t' => "\\t",
            0x08 => "\\b",
            0x0C => "\\f",
            0xE2 => match bytes.get(at + 1..at + 3) {
                Some([0x80, 0xA8]) => "\\u2028",
                Some([0x80, 0xA9]) => "\\u2029",
                // Another character starting with 0xE2: copy it as is.
                _ => {
                    at += 1;
                    continue;
                }
            },
            control => {
                out.push_str(text.get(from..at).unwrap_or(""));
                out.push_str("\\u00");
                out.push(char::from(scan::hex_digit(control >> 4)));
                out.push(char::from(scan::hex_digit(control)));
                at += 1;
                from = at;
                continue;
            }
        };
        out.push_str(text.get(from..at).unwrap_or(""));
        out.push_str(escape);
        // The line separators are three bytes; every other escaped byte, one.
        at += if b == 0xE2 { 3 } else { 1 };
        from = at;
    }
    out.push_str(text.get(from..).unwrap_or(""));
    out.push('"');
}

/// Appends `text` as a JSON string literal escaped as `format` asks:
/// [`write_string`] unless the format is [`html_safe`](Format::html_safe)
/// or [`ascii`](Format::ascii).
#[inline(always)]
pub(crate) fn write_str(out: &mut String, text: &str, format: Format) {
    if format.strict() {
        write_string_strict(out, text, format);
    } else {
        write_string(out, text);
    }
}

/// [`write_string`] with the stricter escaping of `format`: `<`, `>`, and
/// `&` when HTML-safe, every non-ASCII character when ASCII-only.
fn write_string_strict(out: &mut String, text: &str, format: Format) {
    out.reserve(text.len() + 2);
    out.push('"');
    let mut from = 0;
    for (at, ch) in text.char_indices() {
        let escape = match ch {
            '"' | '\\' | '\u{0}'..='\u{1f}' | '\u{2028}' | '\u{2029}' => true,
            '<' | '>' | '&' => format.html,
            _ => format.ascii && !ch.is_ascii(),
        };
        if !escape {
            continue;
        }
        out.push_str(text.get(from..at).unwrap_or(""));
        from = at + ch.len_utf8();
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            '\u{8}' => out.push_str("\\b"),
            '\u{c}' => out.push_str("\\f"),
            _ => {
                let mut units = [0u16; 2];
                for unit in ch.encode_utf16(&mut units) {
                    push_unicode_escape(out, *unit);
                }
            }
        }
    }
    out.push_str(text.get(from..).unwrap_or(""));
    out.push('"');
}

/// Appends `raw`, the text of one valid JSON value, in `format`: unchanged
/// in the plain formats (U+2028 and U+2029 included, which
/// [`write_string`] escapes); under a strict one, with every character that
/// format escapes in strings (`<`, `>`, `&` when HTML-safe, anything
/// non-ASCII when ASCII-only, U+2028 and U+2029 in both) written as its
/// `\uXXXX` escape. In valid JSON those characters can only occur inside
/// strings, where the escape stands for the same character, so the value is
/// unchanged and the format's byte guarantees hold for raw text too.
pub(crate) fn write_raw(out: &mut String, raw: &str, format: Format) {
    if !format.strict() {
        out.push_str(raw);
        return;
    }
    out.reserve(raw.len());
    let mut from = 0;
    for (at, ch) in raw.char_indices() {
        let escape = match ch {
            '\u{2028}' | '\u{2029}' => true,
            '<' | '>' | '&' => format.html,
            _ => format.ascii && !ch.is_ascii(),
        };
        if !escape {
            continue;
        }
        out.push_str(raw.get(from..at).unwrap_or(""));
        from = at + ch.len_utf8();
        let mut units = [0u16; 2];
        for unit in ch.encode_utf16(&mut units) {
            push_unicode_escape(out, *unit);
        }
    }
    out.push_str(raw.get(from..).unwrap_or(""));
}

/// Appends the six-character escape for one UTF-16 code unit, lowercase.
fn push_unicode_escape(out: &mut String, unit: u16) {
    out.push('\\');
    out.push('u');
    for shift in [12, 8, 4, 0] {
        // The nibble is below 16.
        out.push(char::from(scan::hex_digit((unit >> shift) as u8)));
    }
}

/// The first allocation of [`to_string`] and its siblings: enough for a
/// small document, so a short reply is allocated once instead of growing
/// from eight bytes through four reallocations. A larger document grows by
/// doubling from here; to allocate once for that too, size a buffer and
/// use [`write_value_with`].
const START: usize = 128;

/// Serializes `value` as compact JSON: no whitespace at all.
///
/// `value` is anything that implements [`ToJson`]: a [`Value`], or your own
/// type, written straight to text without building a `Value`. The output is
/// deterministic: members in their order, numbers as their exact text,
/// strings escaped as [`write_string`] describes. For a `Value`, parsing the
/// output gives back an equal value.
///
/// # Examples
///
/// ```
/// use jsonx_lib::{json, to_string};
///
/// let v = json!({ "id": 1, "tags": ["a", "b"], "none": null });
/// assert_eq!(to_string(&v), r#"{"id":1,"tags":["a","b"],"none":null}"#);
/// assert_eq!(to_string(&[Some(1), None]), "[1,null]");
/// ```
#[must_use]
pub fn to_string<T: ToJson + ?Sized>(value: &T) -> String {
    let mut out = String::with_capacity(START);
    write_value(&mut out, value);
    out
}

/// Serializes `value` as indented JSON: two spaces per level, one member or
/// item per line, `": "` after keys, empty containers as `[]` and `{}`, and
/// no trailing newline.
///
/// Every line is indented by its depth, so the output grows with the square
/// of the nesting depth: about `2 * depth * depth` bytes of indentation for
/// a value nested `depth` levels, where compact output takes `2 * depth`
/// bytes. Under the default [`Limits`](crate::Limits) a parsed value is at
/// most 128 levels deep, which keeps this small; a value parsed with
/// [`Limits::unlimited`](crate::Limits::unlimited) or built in code can be
/// nested deeply enough that its pretty text does not fit in memory. Write
/// such a value compactly.
///
/// # Examples
///
/// ```
/// use jsonx_lib::{json, to_string_pretty};
///
/// let v = json!({ "a": [1, 2], "b": {} });
/// assert_eq!(to_string_pretty(&v), "{\n  \"a\": [\n    1,\n    2\n  ],\n  \"b\": {}\n}");
/// ```
#[must_use]
pub fn to_string_pretty<T: ToJson + ?Sized>(value: &T) -> String {
    let mut out = String::with_capacity(START);
    write_value_pretty(&mut out, value);
    out
}

/// Appends `value` to `out` as compact JSON (see [`to_string`]). Reusing one
/// buffer across documents avoids an allocation per document.
///
/// # Examples
///
/// ```
/// use jsonx_lib::{json, write_value};
///
/// let mut out = String::from("Content: ");
/// write_value(&mut out, &json!([true]));
/// assert_eq!(out, "Content: [true]");
/// ```
pub fn write_value<T: ToJson + ?Sized>(out: &mut String, value: &T) {
    write_value_with(out, value, Format::compact());
}

/// Appends `value` to `out` as indented JSON (see [`to_string_pretty`]).
///
/// # Examples
///
/// ```
/// use jsonx_lib::{json, write_value_pretty};
///
/// let mut out = String::new();
/// write_value_pretty(&mut out, &json!([1]));
/// assert_eq!(out, "[\n  1\n]");
/// ```
pub fn write_value_pretty<T: ToJson + ?Sized>(out: &mut String, value: &T) {
    write_value_with(out, value, Format::pretty());
}

/// Serializes `value` in the given [`Format`].
///
/// # Examples
///
/// ```
/// use jsonx_lib::{json, to_string_with, Format};
///
/// let v = json!({ "tag": "<b>" });
/// assert_eq!(to_string_with(&v, Format::compact().html_safe(true)), r#"{"tag":"\u003cb\u003e"}"#);
/// ```
#[must_use]
pub fn to_string_with<T: ToJson + ?Sized>(value: &T, format: Format) -> String {
    let mut out = String::with_capacity(START);
    write_value_with(&mut out, value, format);
    out
}

/// Appends `value` to `out` in the given [`Format`].
///
/// # Examples
///
/// ```
/// use jsonx_lib::{write_value_with, Format};
///
/// let mut out = String::from("data: ");
/// write_value_with(&mut out, &[1, 2], Format::pretty().with_indent(0));
/// assert_eq!(out, "data: [\n1,\n2\n]");
/// ```
pub fn write_value_with<T: ToJson + ?Sized>(out: &mut String, value: &T, format: Format) {
    let mut keep = Keep;
    let writer = ValueWriter::new(Out {
        buf: out,
        sink: &mut keep,
        flush_at: usize::MAX,
        format,
        depth: 0,
    });
    let Written(()) = value.to_json(writer);
}

/// Writes `value` as compact JSON to an [`io::Write`](std::io::Write),
/// through a buffer of a few kilobytes, so a large document is never held as
/// one string.
///
/// # Errors
///
/// Any error from `writer`.
///
/// # Examples
///
/// ```
/// let mut out = Vec::new();
/// jsonx_lib::to_writer(&mut out, &jsonx_lib::json!({ "ok": true }))?;
/// assert_eq!(out, br#"{"ok":true}"#);
/// # Ok::<(), std::io::Error>(())
/// ```
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn to_writer<W: std::io::Write, T: ToJson + ?Sized>(
    writer: W,
    value: &T,
) -> std::io::Result<()> {
    to_writer_with(writer, value, Format::compact())
}

/// Writes `value` as indented JSON (see [`to_string_pretty`]) to an
/// [`io::Write`](std::io::Write), through a buffer of a few kilobytes.
///
/// # Errors
///
/// Any error from `writer`.
///
/// # Examples
///
/// ```
/// let mut out = Vec::new();
/// jsonx_lib::to_writer_pretty(&mut out, &jsonx_lib::json!([null]))?;
/// assert_eq!(out, b"[\n  null\n]");
/// # Ok::<(), std::io::Error>(())
/// ```
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn to_writer_pretty<W: std::io::Write, T: ToJson + ?Sized>(
    writer: W,
    value: &T,
) -> std::io::Result<()> {
    to_writer_with(writer, value, Format::pretty())
}

/// How much output the streaming writers collect before passing it on.
pub(crate) const CHUNK: usize = 8 * 1024;

/// Writes `value` in the given [`Format`] to an [`io::Write`](std::io::Write),
/// through a buffer of a few kilobytes.
///
/// # Errors
///
/// Any error from `writer`.
///
/// # Examples
///
/// ```
/// use jsonx_lib::{json, to_writer_with, Format};
///
/// let mut out = Vec::new();
/// to_writer_with(&mut out, &json!(["caf\u{e9}"]), Format::compact().ascii(true))?;
/// assert_eq!(out, b"[\"caf\\u00e9\"]");
/// # Ok::<(), std::io::Error>(())
/// ```
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn to_writer_with<W: std::io::Write, T: ToJson + ?Sized>(
    mut writer: W,
    value: &T,
    format: Format,
) -> std::io::Result<()> {
    let mut buf = String::with_capacity(CHUNK + 256);
    let mut sink = crate::encode::IoSink {
        writer: &mut writer,
        error: None,
    };
    let out = Out {
        buf: &mut buf,
        sink: &mut sink,
        flush_at: CHUNK,
        format,
        depth: 0,
    };
    let Written(()) = value.to_json(ValueWriter::new(out));
    crate::encode::Flush::flush(&mut sink, &mut buf);
    sink.error.map_or(Ok(()), Err)
}

/// Appends a newline and the indentation for `depth` levels.
pub(crate) fn newline(out: &mut String, depth: usize, format: Format) {
    out.push('\n');
    let mut width = depth.saturating_mul(usize::from(format.indent));
    while width > 0 {
        let chunk = width.min(SPACES.len());
        out.push_str(SPACES.get(..chunk).unwrap_or(""));
        width -= chunk;
    }
}

/// The children of an open container still to be written.
enum Open<'r, 'a> {
    Array(slice::Iter<'r, Value<'a>>),
    Object(slice::Iter<'r, Member<'a>>),
}

/// Writes `value` into `out`, handing `out` to `flush` whenever it holds at
/// least `flush_at` bytes (the flush is expected to empty it). In the pretty
/// layout, the value starts at indentation level `base`.
///
/// Iterative: an explicit stack of open containers replaces recursion, so any
/// depth is safe.
pub(crate) fn emit<E>(
    out: &mut String,
    value: &Value<'_>,
    format: Format,
    base: usize,
    flush_at: usize,
    flush: &mut dyn FnMut(&mut String) -> Result<(), E>,
) -> Result<(), E> {
    // One copy of the loop per layout, so the common one (compact, plainly
    // escaped) tests neither per value.
    match (format.pretty, format.strict()) {
        (false, false) => emit_as::<E, false, false>(out, value, format, base, flush_at, flush),
        (false, true) => emit_as::<E, false, true>(out, value, format, base, flush_at, flush),
        (true, false) => emit_as::<E, true, false>(out, value, format, base, flush_at, flush),
        (true, true) => emit_as::<E, true, true>(out, value, format, base, flush_at, flush),
    }
}

/// [`emit`] for one layout: `PRETTY` is `format.pretty`, `STRICT` is
/// `format.strict()`.
fn emit_as<E, const PRETTY: bool, const STRICT: bool>(
    out: &mut String,
    value: &Value<'_>,
    format: Format,
    base: usize,
    flush_at: usize,
    flush: &mut dyn FnMut(&mut String) -> Result<(), E>,
) -> Result<(), E> {
    let write_text = |out: &mut String, text: &str| {
        if STRICT {
            write_string_strict(out, text, format);
        } else {
            write_string(out, text);
        }
    };
    let mut stack: alloc::vec::Vec<(Open<'_, '_>, bool)> = alloc::vec::Vec::new();
    let mut next = Some(value);
    loop {
        if let Some(value) = next.take() {
            match value {
                Value::Null => out.push_str("null"),
                Value::Bool(true) => out.push_str("true"),
                Value::Bool(false) => out.push_str("false"),
                Value::Number(n) => out.push_str(n.as_str()),
                Value::String(s) => write_text(out, s),
                Value::Array(a) if a.is_empty() => out.push_str("[]"),
                Value::Object(o) if o.is_empty() => out.push_str("{}"),
                Value::Array(a) => {
                    out.push('[');
                    stack.push((Open::Array(a.iter()), true));
                }
                Value::Object(o) => {
                    out.push('{');
                    stack.push((Open::Object(o.as_slice().iter()), true));
                }
            }
            if out.len() >= flush_at {
                flush(out)?;
            }
        }
        let depth = stack.len();
        let Some((open, first)) = stack.last_mut() else {
            return Ok(());
        };
        let (child, key): (Option<&Value<'_>>, Option<&Cow<'_, str>>) = match open {
            Open::Array(it) => (it.next(), None),
            Open::Object(it) => match it.next() {
                Some((k, v)) => (Some(v), Some(k)),
                None => (None, None),
            },
        };
        let Some(child) = child else {
            let close = if matches!(open, Open::Array(_)) {
                ']'
            } else {
                '}'
            };
            let _closed = stack.pop();
            if PRETTY {
                newline(out, base + depth - 1, format);
            }
            out.push(close);
            continue;
        };
        if !*first {
            out.push(',');
        }
        *first = false;
        if PRETTY {
            newline(out, base + depth, format);
        }
        if let Some(key) = key {
            write_text(out, key);
            out.push_str(if PRETTY { ": " } else { ":" });
        }
        next = Some(child);
    }
}

impl fmt::Display for Value<'_> {
    /// Writes the value as compact JSON, or as indented JSON with the
    /// alternate flag (`{:#}`).
    ///
    /// # Examples
    ///
    /// ```
    /// let v = jsonx_lib::json!({ "a": [1] });
    /// assert_eq!(format!("{v}"), r#"{"a":[1]}"#);
    /// assert_eq!(format!("{v:#}"), "{\n  \"a\": [\n    1\n  ]\n}");
    /// ```
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        const CHUNK: usize = 4 * 1024;
        let mut buf = String::new();
        emit(
            &mut buf,
            self,
            if f.alternate() {
                Format::pretty()
            } else {
                Format::compact()
            },
            0,
            CHUNK,
            &mut |chunk: &mut String| {
                let result = f.write_str(chunk);
                chunk.clear();
                result
            },
        )?;
        f.write_str(&buf)
    }
}

impl fmt::Debug for Value<'_> {
    /// Writes the value as compact JSON.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

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

    use super::*;

    fn quoted(text: &str) -> String {
        let mut out = String::new();
        write_string(&mut out, text);
        out
    }

    #[test]
    fn test_write_string_escapes_exactly() {
        assert_eq!(quoted(""), r#""""#);
        assert_eq!(quoted("plain/text"), r#""plain/text""#);
        assert_eq!(quoted("\"\\"), r#""\"\\""#);
        assert_eq!(quoted("\u{8}\u{c}\n\r\t"), r#""\b\f\n\r\t""#);
        assert_eq!(
            quoted("\u{0}\u{1}\u{1f}\u{7f}"),
            "\"\\u0000\\u0001\\u001f\u{7f}\""
        );
        assert_eq!(quoted("a\u{2028}b\u{2029}c"), r#""a\u2028b\u2029c""#);
        // Other characters starting with 0xE2 pass through.
        assert_eq!(
            quoted("\u{20ac}\u{2027}\u{202a}"),
            "\"\u{20ac}\u{2027}\u{202a}\""
        );
        assert_eq!(
            quoted("long run of text then \" quote"),
            r#""long run of text then \" quote""#
        );
    }

    #[test]
    fn test_pretty_layout_nested_and_empty() {
        let v = crate::parse(r#"{"a":[],"b":{},"c":[{"d":null}]}"#).unwrap_or_default();
        assert_eq!(
            to_string_pretty(&v),
            "{\n  \"a\": [],\n  \"b\": {},\n  \"c\": [\n    {\n      \"d\": null\n    }\n  ]\n}"
        );
        assert_eq!(to_string_pretty(&Value::from(1)), "1");
    }

    #[test]
    fn test_display_flushes_in_chunks_identically() {
        let mut big = String::from("[");
        for i in 0..5000 {
            big.push_str(&alloc::format!("{i},"));
        }
        big.push_str("0]");
        let v = crate::parse(&big).unwrap_or_default();
        assert_eq!(v.to_string(), big);
    }

    #[test]
    fn test_deep_indent_beyond_spaces_buffer() {
        let depth = 40;
        let text = "[".repeat(depth) + &"]".repeat(depth);
        let v = crate::parse(&text).unwrap_or_default();
        let pretty = to_string_pretty(&v);
        assert!(pretty.contains(&alloc::format!("\n{}[]", " ".repeat((depth - 1) * 2))));
    }
}