jsonx-lib 0.2.0

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
//! The configured parser and the tree builder.

use alloc::{borrow::Cow, vec::Vec};

use crate::{
    Array, Limits, Object, Reader, Value,
    error::{Error, ErrorKind},
    reader::Event,
    tree::Member,
};

/// What to do when an object names the same key twice.
///
/// RFC 8259 says names within an object SHOULD be unique and leaves the
/// behaviour for duplicates to the implementation; implementations disagree,
/// which makes duplicates a classic way to smuggle a different value past one
/// reader than another sees. The default is therefore to refuse them.
/// Keys are compared after unescaping, so `"a"` and `"\u0061"` are the same
/// key.
///
/// The enum is `#[non_exhaustive]`: a later version may add a policy.
///
/// # Examples
///
/// ```
/// use jsonx_lib::{DuplicateKeys, ErrorKind, Parser};
///
/// let text = r#"{"a": 1, "b": 2, "a": 3}"#;
///
/// let err = Parser::new().parse(text).unwrap_err();
/// assert_eq!((err.kind(), err.offset()), (ErrorKind::DuplicateKey, 17));
///
/// let first = Parser::new().duplicate_keys(DuplicateKeys::First).parse(text)?;
/// assert_eq!(first.to_string(), r#"{"a":1,"b":2}"#);
///
/// let last = Parser::new().duplicate_keys(DuplicateKeys::Last).parse(text)?;
/// assert_eq!(last.to_string(), r#"{"a":3,"b":2}"#);
///
/// let all = Parser::new().duplicate_keys(DuplicateKeys::KeepAll).parse(text)?;
/// assert_eq!(all.to_string(), r#"{"a":1,"b":2,"a":3}"#);
/// # Ok::<(), jsonx_lib::Error>(())
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DuplicateKeys {
    /// Refuse the document with [`ErrorKind::DuplicateKey`] at the second
    /// occurrence's opening quote. The default.
    #[default]
    Error,
    /// Keep the first occurrence and its value; drop the later ones.
    First,
    /// Keep the last occurrence's value, at the first occurrence's position,
    /// as inserting each member into an ordered map would.
    Last,
    /// Keep every member as written. The writer reproduces the duplicates.
    KeepAll,
}

/// A configured parser: [`Limits`] and a [`DuplicateKeys`] policy.
///
/// [`parse`](crate::parse) is `Parser::new().parse`. Build a `Parser` to
/// change a budget or the duplicate-key policy; it is a small `Copy` value
/// that can be kept in a `const` or a struct and reused for any number of
/// documents.
///
/// # Examples
///
/// ```
/// use jsonx_lib::{DuplicateKeys, ErrorKind, Limits, Parser};
///
/// // Messages from the network: small and shallow.
/// const WIRE: Parser = Parser::new()
///     .limits(Limits::new().with_max_input_bytes(1 << 20).with_max_depth(32))
///     .duplicate_keys(DuplicateKeys::Error);
///
/// assert!(WIRE.parse(r#"{"ok": true}"#).is_ok());
/// let deep = "[".repeat(33);
/// assert_eq!(WIRE.parse(&deep).unwrap_err().kind(), ErrorKind::DepthLimitExceeded);
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[must_use]
pub struct Parser {
    limits: Limits,
    duplicates: DuplicateKeys,
}

impl Parser {
    /// A parser with the default [`Limits`] and [`DuplicateKeys::Error`].
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(jsonx_lib::Parser::new().parse("[]")?.to_string(), "[]");
    /// # Ok::<(), jsonx_lib::Error>(())
    /// ```
    pub const fn new() -> Self {
        Self {
            limits: Limits::new(),
            duplicates: DuplicateKeys::Error,
        }
    }

    /// Replaces the budgets.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{ErrorKind, Limits, Parser};
    ///
    /// let p = Parser::new().limits(Limits::new().with_max_string_bytes(3));
    /// assert_eq!(p.parse(r#""four""#).unwrap_err().kind(), ErrorKind::StringTooLong);
    /// ```
    pub const fn limits(mut self, limits: Limits) -> Self {
        self.limits = limits;
        self
    }

    /// Sets the duplicate-key policy.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{DuplicateKeys, Parser};
    ///
    /// let p = Parser::new().duplicate_keys(DuplicateKeys::Last);
    /// assert_eq!(p.parse(r#"{"k": 1, "k": 2}"#)?["k"], 2);
    /// # Ok::<(), jsonx_lib::Error>(())
    /// ```
    pub const fn duplicate_keys(mut self, policy: DuplicateKeys) -> Self {
        self.duplicates = policy;
        self
    }

    /// The parser's budgets.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(jsonx_lib::Parser::new().get_limits().max_depth, 128);
    /// ```
    #[must_use]
    pub const fn get_limits(&self) -> Limits {
        self.limits
    }

    /// The parser's duplicate-key policy.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{DuplicateKeys, Parser};
    ///
    /// assert_eq!(Parser::new().get_duplicate_keys(), DuplicateKeys::Error);
    /// ```
    #[must_use]
    pub const fn get_duplicate_keys(&self) -> DuplicateKeys {
        self.duplicates
    }

    /// Parses one JSON document.
    ///
    /// The whole input must be exactly one value, with optional whitespace
    /// around it (space, tab, line feed, carriage return; nothing else, not
    /// even a byte-order mark). Strings without escapes and all numbers borrow
    /// from `text`.
    ///
    /// # Errors
    ///
    /// The first syntax error or exceeded budget, at its byte offset; see
    /// [`ErrorKind`] for every case.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{ErrorKind, Parser};
    ///
    /// let v = Parser::new().parse(" [1, \"two\"] ")?;
    /// assert_eq!(v[1], "two");
    ///
    /// let err = Parser::new().parse("[1] [2]").unwrap_err();
    /// assert_eq!((err.kind(), err.offset()), (ErrorKind::TrailingCharacters, 4));
    /// # Ok::<(), jsonx_lib::Error>(())
    /// ```
    pub fn parse<'a>(&self, text: &'a str) -> Result<Value<'a>, Error> {
        let mut reader = self.reader(text);
        let value = reader
            .read_value()?
            .ok_or(Error::new(ErrorKind::UnexpectedEnd, text.len()))?;
        reader.finish()?;
        Ok(value)
    }

    /// Parses one JSON document from bytes, which must be UTF-8 (RFC 8259
    /// §8.1). The size budget is checked first, then the encoding, then the
    /// syntax.
    ///
    /// # Errors
    ///
    /// [`ErrorKind::InputTooLarge`], then [`ErrorKind::InvalidUtf8`] at the
    /// first byte of the first invalid sequence, then as
    /// [`parse`](Self::parse).
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{ErrorKind, Parser};
    ///
    /// assert_eq!(Parser::new().parse_bytes(b"[\"\xC3\xA9\"]")?[0], "\u{e9}");
    /// let err = Parser::new().parse_bytes(b"[\"\xC3\"]").unwrap_err();
    /// assert_eq!((err.kind(), err.offset()), (ErrorKind::InvalidUtf8, 2));
    /// # Ok::<(), jsonx_lib::Error>(())
    /// ```
    pub fn parse_bytes<'a>(&self, bytes: &'a [u8]) -> Result<Value<'a>, Error> {
        self.parse(self.check_bytes(bytes)?)
    }

    /// A [`Reader`] over `text` with this parser's budgets and duplicate-key
    /// policy (which [`Reader::read_value`] applies).
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{ErrorKind, Limits, Parser};
    ///
    /// let mut r = Parser::new().limits(Limits::new().with_max_depth(1)).reader("[[]]");
    /// assert!(r.next_event().is_ok());
    /// assert_eq!(r.next_event().unwrap_err().kind(), ErrorKind::DepthLimitExceeded);
    /// ```
    #[must_use]
    pub fn reader<'a>(&self, text: &'a str) -> Reader<'a> {
        Reader::with_config(text, self.limits, self.duplicates)
    }

    /// A [`Reader`] over bytes, which must be UTF-8. The size budget and the
    /// encoding are checked here, before any event is read.
    ///
    /// # Errors
    ///
    /// [`ErrorKind::InputTooLarge`] or [`ErrorKind::InvalidUtf8`].
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{Event, Parser};
    ///
    /// let mut r = Parser::new().reader_bytes(b"null")?;
    /// assert_eq!(r.next_event()?, Some(Event::Null));
    /// # Ok::<(), jsonx_lib::Error>(())
    /// ```
    pub fn reader_bytes<'a>(&self, bytes: &'a [u8]) -> Result<Reader<'a>, Error> {
        Ok(self.reader(self.check_bytes(bytes)?))
    }

    fn check_bytes<'a>(&self, bytes: &'a [u8]) -> Result<&'a str, Error> {
        if bytes.len() > self.limits.max_input_bytes {
            return Err(Error::new(
                ErrorKind::InputTooLarge,
                self.limits.max_input_bytes,
            ));
        }
        core::str::from_utf8(bytes).map_err(|e| Error::new(ErrorKind::InvalidUtf8, e.valid_up_to()))
    }
}

/// An open container in the builder: where its children start on the shared
/// value and key stacks.
#[derive(Clone, Copy)]
struct Frame {
    values: usize,
    keys: usize,
}

/// Buffers reused by every object's duplicate check in one build.
#[derive(Default)]
struct Scratch {
    order: Vec<usize>,
    keep: Vec<bool>,
}

/// Builds the value whose first event comes next from `reader`.
///
/// Children of every open container sit on one shared value stack; when a
/// container closes, its children are moved off the top into a vector of
/// exactly their size. Nothing recurses, and no container's vector ever grows
/// by reallocation.
pub(crate) fn build<'a>(
    reader: &mut Reader<'a>,
    policy: DuplicateKeys,
) -> Result<Value<'a>, Error> {
    let mut values: Vec<Value<'a>> = Vec::new();
    let mut keys: Vec<(Cow<'a, str>, usize)> = Vec::new();
    let mut frames: Vec<Frame> = Vec::new();
    let mut scratch = Scratch::default();
    loop {
        let Some(event) = reader.next_event()? else {
            // The reader ends a document only after a complete value.
            return Err(Error::new(ErrorKind::UnexpectedEnd, reader.offset()));
        };
        let value = match event {
            Event::StartArray | Event::StartObject => {
                frames.push(Frame {
                    values: values.len(),
                    keys: keys.len(),
                });
                continue;
            }
            Event::Key(key) => {
                keys.push((key, reader.event_offset()));
                continue;
            }
            Event::EndArray => {
                let Some(frame) = frames.pop() else {
                    return Err(Error::new(ErrorKind::ExpectedValue, reader.event_offset()));
                };
                Value::Array(Array::from(values.split_off(frame.values)))
            }
            Event::EndObject => {
                let Some(frame) = frames.pop() else {
                    return Err(Error::new(ErrorKind::ExpectedValue, reader.event_offset()));
                };
                Value::Object(close_object(
                    &mut values,
                    &mut keys,
                    frame,
                    policy,
                    &mut scratch,
                )?)
            }
            Event::String(s) => Value::String(s),
            Event::Number(n) => Value::Number(n),
            Event::Bool(b) => Value::Bool(b),
            Event::Null => Value::Null,
        };
        if frames.is_empty() {
            return Ok(value);
        }
        values.push(value);
    }
}

#[inline]
fn key_at<'k>(keys: &'k [(Cow<'_, str>, usize)], i: usize) -> &'k str {
    keys.get(i).map_or("", |(k, _)| k)
}

/// Moves the closing object's members off the stacks, applying `policy`.
fn close_object<'a>(
    values: &mut Vec<Value<'a>>,
    keys: &mut Vec<(Cow<'a, str>, usize)>,
    frame: Frame,
    policy: DuplicateKeys,
    scratch: &mut Scratch,
) -> Result<Object<'a>, Error> {
    let member_keys = keys.get(frame.keys..).unwrap_or(&[]);
    let mut dedupe = false;
    if policy != DuplicateKeys::KeepAll && has_duplicates(member_keys, &mut scratch.order) {
        match policy {
            DuplicateKeys::First | DuplicateKeys::Last => {
                mark_duplicates(member_keys, policy, scratch, values, frame.values);
                dedupe = true;
            }
            _ => {
                let at = first_repeat(member_keys, &scratch.order);
                let offset = member_keys.get(at).map_or(0, |(_, o)| *o);
                return Err(Error::new(ErrorKind::DuplicateKey, offset));
            }
        }
    }
    let pairs = keys
        .drain(frame.keys..)
        .map(|(k, _)| k)
        .zip(values.drain(frame.values..));
    let members: Vec<Member<'a>> = if dedupe {
        pairs
            .zip(scratch.keep.iter())
            .filter_map(|(member, &keep)| keep.then_some(member))
            .collect()
    } else {
        pairs.collect()
    };
    Ok(Object::from(members))
}

/// Whether any key repeats. Small objects are checked pairwise; larger ones
/// by sorting indices (`O(n log n)`), which also leaves `order` sorted by
/// (key, position) for [`first_repeat`] and [`mark_duplicates`].
fn has_duplicates(keys: &[(Cow<'_, str>, usize)], order: &mut Vec<usize>) -> bool {
    const PAIRWISE: usize = 16;
    let n = keys.len();
    if n < 2 {
        return false;
    }
    if n <= PAIRWISE {
        // At most 120 one-word comparisons; the strings themselves are
        // compared only when their fingerprints match.
        let mut prints = [0u64; PAIRWISE];
        for (slot, (key, _)) in prints.iter_mut().zip(keys) {
            *slot = fingerprint(key);
        }
        let prints = prints.get(..n).unwrap_or(&[]);
        let any = prints.iter().enumerate().skip(1).any(|(i, a)| {
            prints
                .iter()
                .take(i)
                .enumerate()
                .any(|(j, b)| a == b && key_at(keys, i) == key_at(keys, j))
        });
        if !any {
            return false;
        }
    }
    order.clear();
    order.extend(0..n);
    order.sort_unstable_by(|&a, &b| key_at(keys, a).cmp(key_at(keys, b)).then(a.cmp(&b)));
    order.windows(2).any(|w| match w {
        [a, b] => key_at(keys, *a) == key_at(keys, *b),
        _ => false,
    })
}

/// A cheap summary of a key: equal keys have equal fingerprints. The length
/// and the first eight bytes, which tell most distinct keys apart.
#[inline]
fn fingerprint(key: &str) -> u64 {
    let bytes = key.as_bytes();
    let mut head = [0u8; 8];
    for (slot, &b) in head.iter_mut().zip(bytes) {
        *slot = b;
    }
    u64::from_le_bytes(head) ^ (bytes.len() as u64).rotate_left(56)
}

/// Calls `f(run)` for each run of equal keys in `order` (each run sorted by
/// position, the first being the first occurrence).
fn for_each_run(keys: &[(Cow<'_, str>, usize)], order: &[usize], mut f: impl FnMut(&[usize])) {
    let mut start = 0;
    while start < order.len() {
        let first = key_at(keys, order.get(start).copied().unwrap_or(0));
        let mut end = start + 1;
        while order.get(end).is_some_and(|&i| key_at(keys, i) == first) {
            end += 1;
        }
        if end - start > 1 {
            f(order.get(start..end).unwrap_or(&[]));
        }
        start = end;
    }
}

/// The position of the earliest member whose key repeats an earlier one.
fn first_repeat(keys: &[(Cow<'_, str>, usize)], order: &[usize]) -> usize {
    let mut earliest = usize::MAX;
    for_each_run(keys, order, |run| {
        if let Some(&second) = run.get(1) {
            earliest = earliest.min(second);
        }
    });
    earliest
}

/// Fills `scratch.keep` with which members survive the policy, and for
/// [`DuplicateKeys::Last`] moves each repeated key's last value to its first
/// position.
fn mark_duplicates(
    keys: &[(Cow<'_, str>, usize)],
    policy: DuplicateKeys,
    scratch: &mut Scratch,
    values: &mut [Value<'_>],
    base: usize,
) {
    let keep = &mut scratch.keep;
    keep.clear();
    keep.resize(keys.len(), true);
    for_each_run(keys, &scratch.order, |run| {
        let (Some(&first), Some(&last)) = (run.first(), run.last()) else {
            return;
        };
        if policy == DuplicateKeys::Last {
            values.swap(base + first, base + last);
        }
        for &i in run.get(1..).unwrap_or(&[]) {
            if let Some(slot) = keep.get_mut(i) {
                *slot = false;
            }
        }
    });
}

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

    #[test]
    fn test_duplicate_detection_small_and_large() {
        let small = r#"{"a":1,"b":2,"c":3}"#;
        assert!(Parser::new().parse(small).is_ok());
        let mut large = alloc::string::String::from("{");
        for i in 0..100 {
            large.push_str(&alloc::format!("\"k{i}\":{i},"));
        }
        large.push_str("\"k50\":0,\"k7\":0}");
        let err = Parser::new().parse(&large).unwrap_err();
        assert_eq!(err.kind(), ErrorKind::DuplicateKey);
        assert_eq!(large.get(err.offset()..err.offset() + 6), Some("\"k50\":"));
        let last = Parser::new()
            .duplicate_keys(DuplicateKeys::Last)
            .parse(&large);
        let last = last.unwrap_or_default();
        assert_eq!(last.as_object().map(Object::len), Some(100));
        assert_eq!(last["k50"], 0);
        assert_eq!(last["k7"], 0);
        assert_eq!(last.as_object().and_then(|o| o.keys().nth(50)), Some("k50"));
        let first = Parser::new()
            .duplicate_keys(DuplicateKeys::First)
            .parse(&large);
        assert_eq!(first.unwrap_or_default()["k50"], 50);
    }

    #[test]
    fn test_duplicates_compare_decoded_keys() {
        let err = Parser::new().parse(r#"{"a":1,"\u0061":2}"#).unwrap_err();
        assert_eq!((err.kind(), err.offset()), (ErrorKind::DuplicateKey, 7));
    }

    proptest::proptest! {
        /// Every policy matches a naive quadratic reference, across the
        /// pairwise and sorting paths, with keys that share long prefixes
        /// (equal fingerprints, different strings).
        #[test]
        fn prop_duplicate_policies_match_naive(picks in proptest::collection::vec(0usize..12, 0..40)) {
            use alloc::{format, string::String, vec::Vec};
            const KEYS: [&str; 12] = [
                "a", "b", "abcdefgh1", "abcdefgh2", "abcdefgh12", "abcdefghX", "", "k",
                "abcdefgh", "abcdefgi", "zz", "abcdefgh1\\u0000",
            ];
            let decoded = |k: &str| k.replace("\\u0000", "\u{0}");
            let mut text = String::from("{");
            let mut offsets = Vec::new();
            for (i, &p) in picks.iter().enumerate() {
                if i > 0 { text.push(','); }
                offsets.push(text.len());
                text.push_str(&format!("\"{}\":{i}", KEYS[p]));
            }
            text.push('}');
            let names: Vec<String> = picks.iter().map(|&p| decoded(KEYS[p])).collect();

            // Error: the earliest member whose key appeared before.
            let repeat = (0..names.len()).find(|&i| names[..i].contains(&names[i]));
            let got = Parser::new().parse(&text).err().map(|e| (e.kind(), e.offset()));
            proptest::prop_assert_eq!(got, repeat.map(|i| (ErrorKind::DuplicateKey, offsets[i])));

            // First and Last: one member per key, at its first position.
            let mut order: Vec<&String> = Vec::new();
            for n in &names { if !order.contains(&n) { order.push(n); } }
            let first = Parser::new().duplicate_keys(DuplicateKeys::First).parse(&text).unwrap_or_default();
            let last = Parser::new().duplicate_keys(DuplicateKeys::Last).parse(&text).unwrap_or_default();
            let (fo, lo) = (first.as_object().cloned().unwrap_or_default(), last.as_object().cloned().unwrap_or_default());
            proptest::prop_assert_eq!(fo.keys().collect::<Vec<_>>(), order.iter().map(|s| s.as_str()).collect::<Vec<_>>());
            proptest::prop_assert_eq!(lo.keys().collect::<Vec<_>>(), order.iter().map(|s| s.as_str()).collect::<Vec<_>>());
            for key in &order {
                let want_first = names.iter().position(|n| n == *key);
                let want_last = names.iter().rposition(|n| n == *key);
                proptest::prop_assert_eq!(fo.get(key).and_then(Value::as_u64), want_first.map(|i| i as u64));
                proptest::prop_assert_eq!(lo.get(key).and_then(Value::as_u64), want_last.map(|i| i as u64));
            }
        }
    }

    #[test]
    fn test_duplicate_reported_at_earliest_repeat() {
        // `b` repeats before `a` does.
        let err = Parser::new()
            .parse(r#"{"a":1,"b":2,"b":3,"a":4}"#)
            .unwrap_err();
        assert_eq!(err.offset(), 13);
    }
}