nanojson 0.3.0

A #![no_std], allocation-free, zero-dependency JSON serializer and pull-parser.
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
use crate::error::{ParseError, ParseErrorKind};

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
enum Token {
    Invalid,
    Eof,
    OpenCurly,
    CloseCurly,
    OpenBracket,
    CloseBracket,
    Comma,
    Colon,
    True,
    False,
    Null,
    String,
    Number,
}

impl Token {
    fn name(self) -> &'static str {
        match self {
            Token::Invalid      => "invalid",
            Token::Eof          => "end of input",
            Token::OpenCurly    => "{",
            Token::CloseCurly   => "}",
            Token::OpenBracket  => "[",
            Token::CloseBracket => "]",
            Token::Comma        => ",",
            Token::Colon        => ":",
            Token::True         => "true",
            Token::False        => "false",
            Token::Null         => "null",
            Token::String       => "string",
            Token::Number       => "number",
        }
    }
}

/// Immediate-mode JSON parser. Borrows the source (`'src`).
///
/// A scratch buffer must be supplied to `string()` and `object_member()` calls
/// for string decoding. This keeps the lifetime relationship explicit: the
/// returned `&str` lives as long as the buffer you pass in.
///
/// # Example
/// ```
/// use nanojson::Parser;
/// let src = b"[1, 2, 3]";
/// let mut p = Parser::new(src);
/// p.array_begin().unwrap();
/// let mut sum = 0i64;
/// while p.array_item().unwrap() {
///     sum += p.number_str().unwrap().parse::<i64>().unwrap();
/// }
/// p.array_end().unwrap();
/// assert_eq!(sum, 6);
/// ```
pub struct Parser<'src> {
    src: &'src [u8],
    pos: usize,
    token_start: usize,
    /// Start of the most recently parsed object member key (the opening `"`).
    /// Used by [`Parser::unknown_field`] to point at the key, not the colon.
    key_start: usize,

    str_len: usize,

    token: Token,
    /// Source span of the last NUMBER token (points into `src`).
    number_start: usize,
    number_end: usize,
}

impl<'src> Parser<'src> {
    pub fn new(src: &'src [u8]) -> Self {
        Self {
            src,
            pos: 0,
            token_start: 0,
            key_start: 0,
            str_len: 0,
            token: Token::Invalid,
            number_start: 0,
            number_end: 0,
        }
    }

    /// Byte offset of the start of the most recently attempted token.
    /// Use this in your own diagnostics to compute line/column.
    pub fn error_offset(&self) -> usize {
        self.token_start
    }

    // ---- tokenizer ----

    fn skip_whitespace(&mut self) {
        while self.pos < self.src.len() {
            match self.src[self.pos] {
                b' ' | b'\t' | b'\n' | b'\r' => self.pos += 1,
                _ => break,
            }
        }
    }

    /// Tokenize the next token. Writes decoded string bytes into `str_buf` when
    /// the token is a string; for all other tokens `str_buf` is not used.
    /// Pass `&mut []` when you do not expect a string token.
    fn get_token(&mut self, str_buf: &mut [u8]) -> Result<(), ParseError> {
        self.skip_whitespace();
        self.token_start = self.pos;

        if self.pos >= self.src.len() {
            self.token = Token::Eof;
            return Ok(());
        }

        let ch = self.src[self.pos];

        // Single-char punctuation
        let punct = match ch {
            b'{' => Some(Token::OpenCurly),
            b'}' => Some(Token::CloseCurly),
            b'[' => Some(Token::OpenBracket),
            b']' => Some(Token::CloseBracket),
            b',' => Some(Token::Comma),
            b':' => Some(Token::Colon),
            _ => None,
        };
        if let Some(t) = punct {
            self.token = t;
            self.pos += 1;
            return Ok(());
        }

        // Keywords: true / false / null
        let keywords: [(&[u8], Token); 3] = [
            (b"true",  Token::True),
            (b"false", Token::False),
            (b"null",  Token::Null),
        ];
        for (keyword, tok) in keywords {
            if self.src[self.pos..].starts_with(keyword) {
                self.token = tok;
                self.pos += keyword.len();
                return Ok(());
            }
        }

        // Number: optional '-', digits, optional '.digits', optional 'e/E±digits'
        if ch == b'-' || ch.is_ascii_digit() {
            let start = self.pos;
            if ch == b'-' { self.pos += 1; }
            while self.pos < self.src.len() && self.src[self.pos].is_ascii_digit() {
                self.pos += 1;
            }
            if self.pos < self.src.len() && self.src[self.pos] == b'.' {
                self.pos += 1;
                while self.pos < self.src.len() && self.src[self.pos].is_ascii_digit() {
                    self.pos += 1;
                }
            }
            if self.pos < self.src.len() && matches!(self.src[self.pos], b'e' | b'E') {
                self.pos += 1;
                if self.pos < self.src.len() && matches!(self.src[self.pos], b'+' | b'-') {
                    self.pos += 1;
                }
                while self.pos < self.src.len() && self.src[self.pos].is_ascii_digit() {
                    self.pos += 1;
                }
            }
            self.number_start = start;
            self.number_end = self.pos;
            self.token = Token::Number;
            return Ok(());
        }

        // String
        if ch == b'"' {
            self.pos += 1;
            self.str_len = 0;

            loop {
                if self.pos >= self.src.len() {
                    self.token = Token::Invalid;
                    return Err(ParseError::at(
                        self.token_start,
                        ParseErrorKind::UnexpectedEof,
                    ));
                }
                match self.src[self.pos] {
                    b'"' => {
                        self.pos += 1;
                        self.token = Token::String;
                        return Ok(());
                    }
                    b'\\' => {
                        self.pos += 1;
                        if self.pos >= self.src.len() {
                            self.token = Token::Invalid;
                            return Err(ParseError::at(
                                self.pos,
                                ParseErrorKind::UnexpectedEof,
                            ));
                        }
                        let esc = self.src[self.pos];
                        self.pos += 1;
                        let decoded = match esc {
                            b'"'  => b'"',
                            b'\\' => b'\\',
                            b'/'  => b'/',
                            b'b'  => b'\x08',
                            b't'  => b'\t',
                            b'n'  => b'\n',
                            b'v'  => b'\x0B',
                            b'f'  => b'\x0C',
                            b'r'  => b'\r',
                            other => {
                                self.token = Token::Invalid;
                                return Err(ParseError::at(
                                    self.pos - 1,
                                    ParseErrorKind::InvalidEscape(other),
                                ));
                            }
                        };
                        if self.str_len >= str_buf.len() {
                            return Err(ParseError::at(
                                self.token_start,
                                ParseErrorKind::StringBufferOverflow,
                            ));
                        }
                        str_buf[self.str_len] = decoded;
                        self.str_len += 1;
                    }
                    _ => {
                        let b = self.src[self.pos];
                        self.pos += 1;
                        if self.str_len < str_buf.len() {
                            str_buf[self.str_len] = b;
                            self.str_len += 1;
                        } else if str_buf.len() > 0 {
                            return Err(ParseError::at(
                                self.token_start,
                                ParseErrorKind::StringBufferOverflow,
                            ));
                        }
                    }
                }
            }
        }

        self.token = Token::Invalid;
        Err(ParseError::at(
            self.token_start,
            ParseErrorKind::UnexpectedToken { expected: "value", got: "invalid character" },
        ))
    }

    fn expect_token(&mut self, expected: Token) -> Result<(), ParseError> {
        if self.token != expected {
            return Err(ParseError::at(
                self.token_start,
                ParseErrorKind::UnexpectedToken {
                    expected: expected.name(),
                    got: self.token.name(),
                },
            ));
        }
        Ok(())
    }

    /// Tokenize and expect a specific non-string token. Passes an empty buffer
    /// because no string decoding is needed for structural tokens.
    fn get_and_expect(&mut self, expected: Token) -> Result<(), ParseError> {
        self.get_token(&mut [])?;
        self.expect_token(expected)
    }

    /// After a successful String token, return the decoded bytes as a `&'b str`.
    /// The lifetime `'b` is tied to `str_buf`, not to `&self`, so this is safe
    /// with no raw pointer tricks.
    fn current_string<'b>(&self, str_buf: &'b [u8]) -> Result<&'b str, ParseError> {
        let bytes = &str_buf[..self.str_len];
        core::str::from_utf8(bytes).map_err(|_| {
            ParseError::at(self.token_start, ParseErrorKind::InvalidUtf8)
        })
    }

    // ---- public API ----

    /// Parse `{`.
    pub fn object_begin(&mut self) -> Result<(), ParseError> {
        self.get_and_expect(Token::OpenCurly)
    }

    /// Parse the next object member key, or return `None` when `}` is reached.
    ///
    /// The returned string is decoded into `str_buf` and is valid for `'b`
    /// (the lifetime of the buffer). Copy it or process it before the next call
    /// that takes a `str_buf`.
    pub fn object_member<'b>(&mut self, str_buf: &'b mut [u8]) -> Result<Option<&'b str>, ParseError> {
        let saved_pos = self.pos;
        self.get_token(str_buf)?;

        match self.token {
            Token::Comma => {
                // Subsequent member: expect key string
                self.get_token(str_buf)?;
                self.expect_token(Token::String)?;
                self.key_start = self.token_start;
                self.get_and_expect(Token::Colon)?;
                Ok(Some(self.current_string(str_buf)?))
            }
            Token::CloseCurly => {
                self.pos = saved_pos;
                Ok(None)
            }
            Token::String => {
                // First member — key was decoded into str_buf by get_token
                self.key_start = self.token_start;
                self.get_and_expect(Token::Colon)?;
                Ok(Some(self.current_string(str_buf)?))
            }
            _ => Err(ParseError::at(
                self.token_start,
                ParseErrorKind::UnexpectedToken {
                    expected: "string or }",
                    got: self.token.name(),
                },
            )),
        }
    }

    /// Parse `}`.
    pub fn object_end(&mut self) -> Result<(), ParseError> {
        self.get_and_expect(Token::CloseCurly)
    }

    /// Returns an `UnknownField` error at the current position.
    /// Call this inside the `_` arm of your `object_member` match.
    pub fn unknown_field(&self) -> ParseError {
        ParseError::at(self.key_start, ParseErrorKind::UnknownField { type_name: "", expected_fields: &[] })
    }

    /// Returns an `UnknownField` error enriched with the type name and its valid field names.
    /// Used by derive-generated code to produce more helpful diagnostics.
    pub fn unknown_field_in(&self, type_name: &'static str, expected_fields: &'static [&'static str]) -> ParseError {
        ParseError::at(self.key_start, ParseErrorKind::UnknownField { type_name, expected_fields })
    }

    /// Parse `[`.
    pub fn array_begin(&mut self) -> Result<(), ParseError> {
        self.get_and_expect(Token::OpenBracket)
    }

    /// Check whether there is another item in the array.
    /// Returns `true` if so (consuming a `,` separator if present),
    /// `false` when `]` is reached.
    ///
    /// Uses fast first-character inspection so no scratch buffer is needed.
    pub fn array_item(&mut self) -> Result<bool, ParseError> {
        match self.peek_token() {
            Token::Comma => {
                // Consume the comma
                self.skip_whitespace();
                self.token_start = self.pos;
                self.pos += 1;
                self.token = Token::Comma;
                Ok(true)
            }
            Token::CloseBracket => Ok(false),
            // First item, or EOF/invalid — let the item deserializer produce the error.
            _ => Ok(true),
        }
    }

    /// Parse `]`.
    pub fn array_end(&mut self) -> Result<(), ParseError> {
        self.get_and_expect(Token::CloseBracket)
    }

    /// Parse `null`.
    pub fn null(&mut self) -> Result<(), ParseError> {
        self.get_and_expect(Token::Null)
    }

    /// Parse `true` or `false`, returning the value.
    pub fn bool_val(&mut self) -> Result<bool, ParseError> {
        self.get_token(&mut [])?;
        match self.token {
            Token::True  => Ok(true),
            Token::False => Ok(false),
            _ => Err(ParseError::at(
                self.token_start,
                ParseErrorKind::UnexpectedToken { expected: "boolean", got: self.token.name() },
            )),
        }
    }

    /// Parse a JSON string, decoding escape sequences into `str_buf`.
    ///
    /// The returned `&'b str` is valid for the lifetime of `str_buf`. It is
    /// overwritten on the next call to `string()` or `object_member()` that
    /// uses the same buffer.
    pub fn string<'b>(&mut self, str_buf: &'b mut [u8]) -> Result<&'b str, ParseError> {
        self.get_token(str_buf)?;
        self.expect_token(Token::String)?;
        self.current_string(str_buf)
    }

    /// Parse a JSON number and return the raw source bytes as a `&'src str`.
    /// No numeric conversion is performed. Parse the value yourself with
    /// e.g. `s.parse::<f64>()` (requires std) or a dedicated crate.
    pub fn number_str(&mut self) -> Result<&'src str, ParseError> {
        self.get_and_expect(Token::Number)?;
        let bytes = &self.src[self.number_start..self.number_end];
        core::str::from_utf8(bytes).map_err(|_| {
            ParseError::at(self.token_start, ParseErrorKind::InvalidUtf8)
        })
    }

    // ---- lookahead ----

    /// Peek at the type of the next token without advancing the parser.
    /// Inspects only the first non-whitespace byte, so no scratch buffer is needed.
    fn peek_token(&self) -> Token {
        let mut i = self.pos;
        while i < self.src.len() && matches!(self.src[i], b' ' | b'\t' | b'\n' | b'\r') {
            i += 1;
        }
        if i >= self.src.len() { return Token::Eof; }
        match self.src[i] {
            b'{' => Token::OpenCurly,
            b'}' => Token::CloseCurly,
            b'[' => Token::OpenBracket,
            b']' => Token::CloseBracket,
            b',' => Token::Comma,
            b':' => Token::Colon,
            b'"' => Token::String,
            b't' => Token::True,
            b'f' => Token::False,
            b'n' => Token::Null,
            b'-' | b'0'..=b'9' => Token::Number,
            _ => Token::Invalid,
        }
    }

    pub fn is_null_ahead(&self) -> bool   { self.peek_token() == Token::Null }
    pub fn is_bool_ahead(&self) -> bool   { matches!(self.peek_token(), Token::True | Token::False) }
    pub fn is_number_ahead(&self) -> bool { self.peek_token() == Token::Number }
    pub fn is_string_ahead(&self) -> bool { self.peek_token() == Token::String }
    pub fn is_array_ahead(&self) -> bool  { self.peek_token() == Token::OpenBracket }
    pub fn is_object_ahead(&self) -> bool { self.peek_token() == Token::OpenCurly }
}

/// Trait for types that can deserialize themselves from JSON using a [`Parser`].
///
/// `'src` is the lifetime of the JSON source bytes. `'buf` is the lifetime of
/// the scratch buffer used for string decoding. Owned types implement
/// `for<'s, 'b> Deserialize<'s, 'b>`.
pub trait Deserialize<'src, 'buf>: Sized {
    fn deserialize(parser: &mut Parser<'src>, str_buf: &'buf mut [u8]) -> Result<Self, ParseError>;
}

impl<'src, 'buf> Deserialize<'src, 'buf> for bool {
    fn deserialize(parser: &mut Parser<'src>, _str_buf: &'buf mut [u8]) -> Result<Self, ParseError> {
        parser.bool_val()
    }
}

impl<'src, 'buf> Deserialize<'src, 'buf> for &'buf str {
    fn deserialize(parser: &mut Parser<'src>, str_buf: &'buf mut [u8]) -> Result<Self, ParseError> {
        parser.string(str_buf)
    }
}

#[cfg(feature = "alloc")]
impl<'src, 'buf> Deserialize<'src, 'buf> for alloc::string::String {
    fn deserialize(parser: &mut Parser<'src>, str_buf: &'buf mut [u8]) -> Result<Self, ParseError> {
        Ok(alloc::string::String::from(parser.string(str_buf)?))
    }
}

macro_rules! impl_float {
    ($($t:ty),*) => {$(
        impl<'src, 'buf> Deserialize<'src, 'buf> for $t {
            fn deserialize(parser: &mut Parser<'src>, _str_buf: &'buf mut [u8]) -> Result<Self, ParseError> {
                let s = parser.number_str()?;
                let offset = parser.error_offset();
                s.parse::<$t>().map_err(|_| ParseError::at(
                    offset,
                    ParseErrorKind::UnexpectedToken { expected: "number", got: "invalid float" },
                ))
            }
        }
    )*};
}
impl_float!(f32, f64);

macro_rules! impl_integer {
    ($($t:ty),*) => {$(
        impl<'src, 'buf> Deserialize<'src, 'buf> for $t {
            fn deserialize(parser: &mut Parser<'src>, _str_buf: &'buf mut [u8]) -> Result<Self, ParseError> {
                let s = parser.number_str()?;
                integer_from_str::<$t>(s, parser.token_start)
            }
        }
    )*};
}
impl_integer!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);

fn integer_from_str<T: IntParse>(s: &str, offset: usize) -> Result<T, ParseError> {
    T::from_str(s).ok_or_else(|| ParseError::at(
        offset,
        ParseErrorKind::UnexpectedToken { expected: "integer", got: "number out of range" },
    ))
}

trait IntParse: Sized {
    fn from_str(s: &str) -> Option<Self>;
}

macro_rules! impl_int_parse {
    (signed: $($t:ty),*) => {$(
        impl IntParse for $t {
            fn from_str(s: &str) -> Option<Self> {
                let bytes = s.as_bytes();
                if bytes.is_empty() { return None; }
                let (neg, digits) = if bytes[0] == b'-' { (true, &bytes[1..]) } else { (false, bytes) };
                if digits.is_empty() { return None; }
                // Accumulate as a negative value so that i8::MIN (-128) parses correctly.
                // 128 doesn't fit as positive i8 before negation, but -128 fits as-is.
                let mut v: $t = 0;
                for &b in digits {
                    if b < b'0' || b > b'9' { return None; }
                    v = v.checked_mul(10)?.checked_sub((b - b'0') as $t)?;
                }
                if neg { Some(v) } else { v.checked_neg() }
            }
        }
    )*};
    (unsigned: $($t:ty),*) => {$(
        impl IntParse for $t {
            fn from_str(s: &str) -> Option<Self> {
                let bytes = s.as_bytes();
                if bytes.is_empty() || bytes[0] == b'-' { return None; }
                let mut v: $t = 0;
                for &b in bytes {
                    if b < b'0' || b > b'9' { return None; }
                    v = v.checked_mul(10)?.checked_add((b - b'0') as $t)?;
                }
                Some(v)
            }
        }
    )*};
}
impl_int_parse!(signed:   i8, i16, i32, i64, i128, isize);
impl_int_parse!(unsigned: u8, u16, u32, u64, u128, usize);

impl<'src, 'buf, T: Deserialize<'src, 'buf>> Deserialize<'src, 'buf> for Option<T> {
    fn deserialize(parser: &mut Parser<'src>, str_buf: &'buf mut [u8]) -> Result<Self, ParseError> {
        if parser.is_null_ahead() {
            parser.null()?;
            Ok(None)
        } else {
            T::deserialize(parser, str_buf).map(Some)
        }
    }
}

impl<'src, 'buf, T, const N: usize> Deserialize<'src, 'buf> for [T; N]
where
    T: for<'x> Deserialize<'src, 'x>,
{
    fn deserialize(parser: &mut Parser<'src>, str_buf: &'buf mut [u8]) -> Result<Self, ParseError> {
        parser.array_begin()?;

        let mut arr: [Option<T>; N] = [(); N].map(|_| None);

        for i in 0..N {
            if !parser.array_item()? {
                return Err(ParseError::at(
                    parser.error_offset(),
                    ParseErrorKind::UnexpectedToken { expected: "array item", got: "]" },
                ));
            }
            // `&mut *str_buf` creates a fresh shorter-lived reborrow so NLL can
            // release it after the call (T: for<'x> ensures T doesn't capture it).
            arr[i] = Some(T::deserialize(parser, &mut *str_buf)?);
        }

        // Reject arrays with more items than N.
        if parser.array_item()? {
            return Err(ParseError::at(
                parser.error_offset(),
                ParseErrorKind::UnexpectedToken { expected: "]", got: "array item" },
            ));
        }
        parser.array_end()?;

        // Every element was set to `Some(...)` by the loop above — unwrap is
        // dead code (the loop invariant guarantees all slots are filled).
        // `array::try_from_fn` would be the ideal solution but is nightly-only.
        Ok(arr.map(|x| x.unwrap()))
    }
}

#[cfg(feature = "arrayvec")]
impl<'src, 'buf, T, const N: usize> Deserialize<'src, 'buf> for arrayvec::ArrayVec<T, N>
where
    T: for<'x> Deserialize<'src, 'x>,
{
    fn deserialize(parser: &mut Parser<'src>, str_buf: &'buf mut [u8]) -> Result<Self, ParseError> {
        let mut vec = arrayvec::ArrayVec::new();
        parser.array_begin()?;
        while parser.array_item()? {
            let v = T::deserialize(parser, &mut *str_buf)?;
            vec.try_push(v).map_err(|_| ParseError::at(
                parser.error_offset(),
                ParseErrorKind::StringBufferOverflow,
            ))?;
        }
        parser.array_end()?;
        Ok(vec)
    }
}

#[cfg(feature = "arrayvec")]
impl<'src, 'buf, const N: usize> Deserialize<'src, 'buf> for arrayvec::ArrayString<N> {
    fn deserialize(parser: &mut Parser<'src>, str_buf: &'buf mut [u8]) -> Result<Self, ParseError> {
        let s = parser.string(str_buf)?;
        arrayvec::ArrayString::try_from(s).map_err(|_| ParseError::at(
            parser.error_offset(),
            ParseErrorKind::StringBufferOverflow,
        ))
    }
}

#[cfg(feature = "alloc")]
impl<'src, 'buf, T> Deserialize<'src, 'buf> for alloc::vec::Vec<T>
where
    T: for<'x> Deserialize<'src, 'x>,
{
    fn deserialize(parser: &mut Parser<'src>, str_buf: &'buf mut [u8]) -> Result<Self, ParseError> {
        let mut vec = alloc::vec::Vec::new();
        parser.array_begin()?;
        while parser.array_item()? {
            vec.push(T::deserialize(parser, &mut *str_buf)?);
        }
        parser.array_end()?;
        Ok(vec)
    }
}

#[cfg(feature = "alloc")]
impl<'src, 'buf, T: Deserialize<'src, 'buf>> Deserialize<'src, 'buf> for alloc::boxed::Box<T> {
    fn deserialize(parser: &mut Parser<'src>, str_buf: &'buf mut [u8]) -> Result<Self, ParseError> {
        T::deserialize(parser, str_buf).map(alloc::boxed::Box::new)
    }
}

macro_rules! impl_deserialize_map {
    ($map_ty:ty, $new:expr) => {
        impl<'src, 'buf, V> Deserialize<'src, 'buf> for $map_ty
        where
            V: for<'x> Deserialize<'src, 'x>,
        {
            fn deserialize(parser: &mut Parser<'src>, str_buf: &'buf mut [u8]) -> Result<Self, ParseError> {
                let mut map = $new;
                parser.object_begin()?;
                // Explicit loop so NLL can see the borrow from object_member ends
                // after String::from(k) before the next reborrow.
                loop {
                    let maybe_key = parser.object_member(&mut *str_buf)?;
                    let key = match maybe_key {
                        None => break,
                        Some(k) => alloc::string::String::from(k),
                    };
                    let value = V::deserialize(parser, &mut *str_buf)?;
                    map.insert(key, value);
                }
                parser.object_end()?;
                Ok(map)
            }
        }
    };
}

#[cfg(feature = "alloc")]
impl_deserialize_map!(
    alloc::collections::BTreeMap<alloc::string::String, V>,
    alloc::collections::BTreeMap::new()
);

#[cfg(feature = "std")]
impl_deserialize_map!(
    std::collections::HashMap<std::string::String, V>,
    std::collections::HashMap::new()
);

// ---- Convenience free functions ----

/// Parse using a hand-written closure with a stack-allocated scratch buffer of `STR_BUF` bytes.
///
/// The closure receives both a `&mut Parser` and a `&mut [u8]` scratch buffer.
/// Pass the buffer to `string()` and `object_member()` calls.
///
/// # Example
/// ```
/// let (x, y) = nanojson::parse_manual_sized(b"{\"x\":3,\"y\":4}", &mut [0u8; 16], |p, buf| {
///     p.object_begin()?;
///     let mut x = 0i64; let mut y = 0i64;
///     while let Some(k) = p.object_member(buf)? {
///         match k {
///             "x" => x = p.number_str()?.parse().unwrap(),
///             "y" => y = p.number_str()?.parse().unwrap(),
///             _ => return Err(p.unknown_field()),
///         }
///     }
///     p.object_end()?;
///     Ok((x, y))
/// }).unwrap();
/// assert_eq!((x, y), (3, 4));
/// ```
pub fn parse_manual_sized<T>(
    src: &[u8],
    buf: &mut [u8],
    f: impl for<'a, 'b> FnOnce(&mut Parser<'a>, &'b mut [u8]) -> Result<T, ParseError>,
) -> Result<T, ParseError> {
    let mut parser = Parser::new(src);
    f(&mut parser, buf)
}

/// Deserialize a `T: Deserialize` value with a stack-allocated scratch buffer of `STR_BUF` bytes.
///
/// # Example
/// ```
/// let n: i64 = nanojson::parse_sized(b"42", &mut [0; 0]).unwrap();
/// assert_eq!(n, 42);
/// ```
#[inline]
pub fn parse_sized<'s, T>(
    src: &'s [u8],
    buf: &mut [u8],
) -> Result<T, ParseError>
where
    T: for<'b> Deserialize<'s, 'b>,
{
    T::deserialize(&mut Parser::new(src), buf)
}

/// Deserialize a fully-owned type from raw bytes.
/// The scratch buffer is auto-allocated at `src.len()` bytes (safe upper bound
/// for string decoding: a decoded string is never longer than its escaped form).
///
/// # Example
/// ```
/// let n: i64 = nanojson::parse_bytes(b"42").unwrap();
/// assert_eq!(n, 42);
/// ```
#[cfg(feature = "std")]
#[inline]
pub fn parse_bytes<T>(src: &[u8]) -> Result<T, ParseError>
where
    T: for<'s, 'b> Deserialize<'s, 'b>,
{
    let mut scratch = std::vec![0u8; src.len().max(1)];
    T::deserialize(&mut Parser::new(src), scratch.as_mut_slice())
}

/// Deserialize a fully-owned type from a `&str`.
/// The scratch buffer is auto-allocated; no size choice required.
///
/// # Example
/// ```
/// let n: i64 = nanojson::parse("42").unwrap();
/// assert_eq!(n, 42);
/// ```
#[cfg(feature = "std")]
#[inline]
pub fn parse<T>(src: &str) -> Result<T, ParseError>
where
    T: for<'s, 'b> Deserialize<'s, 'b>,
{
    parse_bytes(src.as_bytes())
}

/// Drive the parser manually with an auto-sized heap-allocated scratch buffer.
/// The scratch buffer is sized to `src.len()` (safe upper bound for string decoding).
/// `T` must be a fully owned type (no borrows from the parser).
///
/// The closure receives both a `&mut Parser` and a `&mut [u8]` scratch buffer.
/// Pass the buffer to `string()` and `object_member()` calls.
///
/// # Example
/// ```
/// let (x, y) = nanojson::parse_manual(b"{\"x\":3,\"y\":4}", |p, buf| {
///     p.object_begin()?;
///     let mut x = 0i64; let mut y = 0i64;
///     while let Some(k) = p.object_member(buf)? {
///         match k {
///             "x" => x = p.number_str()?.parse().unwrap(),
///             "y" => y = p.number_str()?.parse().unwrap(),
///             _ => return Err(p.unknown_field()),
///         }
///     }
///     p.object_end()?;
///     Ok((x, y))
/// }).unwrap();
/// assert_eq!((x, y), (3, 4));
/// ```
#[cfg(feature = "std")]
#[inline]
pub fn parse_manual<T>(
    src: &[u8],
    f: impl for<'a, 'b> FnOnce(&mut Parser<'a>, &'b mut [u8]) -> Result<T, ParseError>,
) -> Result<T, ParseError> {
    let mut scratch = std::vec![0u8; src.len().max(1)];
    let mut parser = Parser::new(src);
    f(&mut parser, &mut scratch)
}