jzon-rs 0.2.1

Zero-copy JSON serialization with compile-time generated typed parsers
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
//! `ToJson` trait and primitive implementations.

use std::collections::{BTreeMap, HashMap};

pub trait ToJson {
    fn json_write(&self, w: &mut Vec<u8>);

    /// Hint for the approximate number of bytes this value will serialize to.
    ///
    /// This is used by `to_json_bytes` to pre-allocate the output buffer,
    /// avoiding reallocations for the common case.  Implementations should
    /// return a value that is *at least* as large as the serialized form in
    /// the common case — over-estimating is fine, under-estimating causes a
    /// single reallocation.  The default (64) is conservative.
    #[inline]
    fn json_size_hint(&self) -> usize { 64 }

    #[must_use]
    fn to_json_bytes(&self) -> Vec<u8> {
        let mut buf = Vec::with_capacity(self.json_size_hint());
        self.json_write(&mut buf);
        buf
    }

    #[must_use]
    fn to_json_string(&self) -> String {
        // SAFETY: ToJson implementations only write valid UTF-8 bytes.
        // The expect path is unreachable for any correct impl; using expect (not
        // unwrap_unchecked) to preserve a clear panic message for buggy custom impls.
        String::from_utf8(self.to_json_bytes())
            .expect("ToJson implementations always emit valid UTF-8")
    }
}

// ── string escaping ───────────────────────────────────────────────────────────
//
// `write_escaped_str` delegates byte scanning to `crate::simd::find_escape`,
// which dispatches to the widest available implementation:
//   - nightly + simd feature  → 32-byte std::simd lanes  (find_escape_simd32)
//   - stable + simd feature   → 16-byte u128 SWAR        (find_escape_simd16)
//   - no simd feature         → scalar byte-by-byte      (find_escape_scalar)
//
// This keeps ser.rs free of SWAR arithmetic — all the bit tricks live in simd.rs.

#[inline]
pub fn write_escaped_str(s: &str, w: &mut Vec<u8>) {
    w.push(b'"');
    // Pre-reserve: common case is no escaping, so reserve s.len() + 1 (closing quote).
    // Avoids all reallocations in the fast (no-escape) path.
    w.reserve(s.len() + 1);
    let bytes = s.as_bytes();
    let mut start = 0usize; // start of current unescaped run

    let mut i = start;
    while i < bytes.len() {
        // Find the next byte that needs escaping using the widest available path.
        let stop = crate::simd::find_escape(bytes, i);
        if stop >= bytes.len() {
            // No more bytes need escaping; flush the rest in one go.
            break;
        }
        // Flush safe bytes [start..stop], then emit the escape sequence.
        w.extend_from_slice(&bytes[start..stop]);
        escape_one(bytes[stop], w);
        i = stop + 1;
        start = i;
    }

    // Flush the final safe run.
    w.extend_from_slice(&bytes[start..]);
    w.push(b'"');
}

#[inline(always)]
fn escape_one(b: u8, w: &mut Vec<u8>) {
    match b {
        b'"'  => w.extend_from_slice(b"\\\""),
        b'\\' => w.extend_from_slice(b"\\\\"),
        b'\n' => w.extend_from_slice(b"\\n"),
        b'\r' => w.extend_from_slice(b"\\r"),
        b'\t' => w.extend_from_slice(b"\\t"),
        0x08  => w.extend_from_slice(b"\\b"),
        0x0C  => w.extend_from_slice(b"\\f"),
        b     => {
            // Other control characters as \u00XX
            let hi = b >> 4;
            let lo = b & 0xF;
            w.extend_from_slice(&[
                b'\\', b'u', b'0', b'0',
                if hi < 10 { b'0' + hi } else { b'a' + hi - 10 },
                if lo < 10 { b'0' + lo } else { b'a' + lo - 10 },
            ]);
        }
    }
}

// ── primitive impls ───────────────────────────────────────────────────────────

impl ToJson for bool {
    #[inline]
    fn json_write(&self, w: &mut Vec<u8>) {
        w.extend_from_slice(if *self { b"true" } else { b"false" });
    }
    #[inline] fn json_size_hint(&self) -> usize { 5 } // "false"
}

impl ToJson for str {
    #[inline]
    fn json_write(&self, w: &mut Vec<u8>) { write_escaped_str(self, w); }
    #[inline] fn json_size_hint(&self) -> usize { self.len() + 2 }
}

impl ToJson for String {
    #[inline]
    fn json_write(&self, w: &mut Vec<u8>) { write_escaped_str(self, w); }
    #[inline] fn json_size_hint(&self) -> usize { self.len() + 2 }
}

impl ToJson for crate::scanner::JsonStr<'_> {
    #[inline]
    fn json_write(&self, w: &mut Vec<u8>) {
        use crate::scanner::JsonStr;
        match self {
            JsonStr::BorrowedNoEsc(s) => {
                // Provably escape-free — skip find_escape scan.
                w.reserve(s.len() + 2);
                w.push(b'"');
                w.extend_from_slice(s.as_bytes());
                w.push(b'"');
            }
            JsonStr::Borrowed(s) => write_escaped_str(s, w),
            JsonStr::Owned(s)    => write_escaped_str(s, w),
        }
    }
    #[inline]
    fn json_size_hint(&self) -> usize { self.as_str().len() + 2 }
}

impl<T: ToJson + ?Sized> ToJson for &T {
    #[inline]
    fn json_write(&self, w: &mut Vec<u8>) { (**self).json_write(w); }
    #[inline] fn json_size_hint(&self) -> usize { (**self).json_size_hint() }
}

impl<T: ToJson> ToJson for Box<T> {
    #[inline]
    fn json_write(&self, w: &mut Vec<u8>) { (**self).json_write(w); }
    #[inline] fn json_size_hint(&self) -> usize { (**self).json_size_hint() }
}

impl<T: ToJson> ToJson for Option<T> {
    #[inline]
    fn json_write(&self, w: &mut Vec<u8>) {
        match self {
            Some(v) => v.json_write(w),
            None    => w.extend_from_slice(b"null"),
        }
    }
    #[inline]
    fn json_size_hint(&self) -> usize {
        match self {
            Some(v) => v.json_size_hint(),
            None    => 4, // "null"
        }
    }
}

impl<T: ToJson> ToJson for Vec<T> {
    fn json_write(&self, w: &mut Vec<u8>) {
        w.push(b'[');
        let mut first = true;
        for item in self {
            if !first { w.push(b','); }
            item.json_write(w);
            first = false;
        }
        w.push(b']');
    }
    #[inline]
    fn json_size_hint(&self) -> usize {
        if self.is_empty() { return 2; }
        // Use the first element's hint as a sample; add separating commas.
        2 + self.len() * (self[0].json_size_hint() + 1)
    }
}

impl<T: ToJson, const N: usize> ToJson for [T; N] {
    fn json_write(&self, w: &mut Vec<u8>) {
        w.push(b'[');
        let mut first = true;
        for item in self {
            if !first { w.push(b','); }
            item.json_write(w);
            first = false;
        }
        w.push(b']');
    }
    #[inline]
    fn json_size_hint(&self) -> usize {
        if N == 0 { return 2; }
        2 + N * (self[0].json_size_hint() + 1)
    }
}

impl<T: ToJson> ToJson for [T] {
    fn json_write(&self, w: &mut Vec<u8>) {
        w.push(b'[');
        let mut first = true;
        for item in self {
            if !first { w.push(b','); }
            item.json_write(w);
            first = false;
        }
        w.push(b']');
    }
    #[inline]
    fn json_size_hint(&self) -> usize {
        if self.is_empty() { return 2; }
        2 + self.len() * (self[0].json_size_hint() + 1)
    }
}

// Note: A specialized impl for Vec<f64> is not possible on stable Rust due to
// the coherence rules (conflicts with impl<T: ToJson> ToJson for Vec<T>).
// The generic Vec<T> impl with json_size_hint delegating to f64::json_size_hint (10)
// covers the f64 case correctly through monomorphization.

// ── integer writers (no format! overhead) ─────────────────────────────────────

#[inline(always)]
pub fn write_u64(mut n: u64, w: &mut Vec<u8>) {
    if n == 0 { w.push(b'0'); return; }
    let mut tmp = [0u8; 20];
    let mut len = 0usize;
    while n > 0 { tmp[len] = b'0' + (n % 10) as u8; n /= 10; len += 1; }
    tmp[..len].reverse();
    w.extend_from_slice(&tmp[..len]);
}

#[inline(always)]
pub fn write_i64(n: i64, w: &mut Vec<u8>) {
    if n < 0 { w.push(b'-'); write_u64(n.unsigned_abs(), w); } else { write_u64(n as u64, w); }
}

macro_rules! impl_uint {
    ($($t:ty, $hint:expr),*) => {$(
        impl ToJson for $t {
            #[inline] fn json_write(&self, w: &mut Vec<u8>) { write_u64(*self as u64, w); }
            #[inline] fn json_size_hint(&self) -> usize { $hint }
        }
    )*};
}
macro_rules! impl_sint {
    ($($t:ty, $hint:expr),*) => {$(
        impl ToJson for $t {
            #[inline] fn json_write(&self, w: &mut Vec<u8>) { write_i64(*self as i64, w); }
            #[inline] fn json_size_hint(&self) -> usize { $hint }
        }
    )*};
}
// Tight upper bounds (max digit count including sign for signed types):
//   u8:3, u16:5, u32:10, u64:20, u128:39, usize:20
//   i8:4, i16:6, i32:11, i64:20, i128:40, isize:20
impl_uint!(u8, 3, u16, 5, u32, 10, u64, 20, usize, 20);
impl_sint!(i8, 4, i16, 6, i32, 11, i64, 20, isize, 20);

// u128 / i128: cannot pass through u64/i64, need dedicated digit writers.
#[inline]
fn write_u128(mut n: u128, w: &mut Vec<u8>) {
    if n == 0 { w.push(b'0'); return; }
    let mut tmp = [0u8; 39];
    let mut len = 0usize;
    while n > 0 { tmp[len] = b'0' + (n % 10) as u8; n /= 10; len += 1; }
    tmp[..len].reverse();
    w.extend_from_slice(&tmp[..len]);
}
impl ToJson for u128 {
    #[inline] fn json_write(&self, w: &mut Vec<u8>) { write_u128(*self, w); }
    #[inline] fn json_size_hint(&self) -> usize { 39 }
}
impl ToJson for i128 {
    #[inline]
    fn json_write(&self, w: &mut Vec<u8>) {
        if *self < 0 { w.push(b'-'); write_u128(self.unsigned_abs(), w); } else { write_u128(*self as u128, w); }
    }
    #[inline] fn json_size_hint(&self) -> usize { 40 }
}

impl ToJson for f64 {
    #[inline]
    fn json_write(&self, w: &mut Vec<u8>) {
        if !self.is_finite() { w.extend_from_slice(b"null"); return; }
        // ECMA-404 / ECMA-262 §24.5.2.4: -0 must serialise as "0".
        // IEEE 754: -0.0 == 0.0, so this check catches both.
        if *self == 0.0 { w.extend_from_slice(b"0"); return; }
        #[cfg(feature = "zmij-float-ser")]
        {
            let mut buf = zmij::Buffer::new();
            w.extend_from_slice(buf.format_finite(*self).as_bytes());
            return;
        }
        #[cfg(all(feature = "fast-float", not(feature = "zmij-float-ser")))]
        {
            let mut buf = ryu::Buffer::new();
            w.extend_from_slice(buf.format_finite(*self).as_bytes());
            return;
        }
        #[cfg(not(any(feature = "fast-float", feature = "zmij-float-ser")))]
        w.extend_from_slice(format!("{}", self).as_bytes());
    }
    /// ryu's worst-case output for f64 is 24 characters, but the practical output for
    /// typical floats (integers, short decimals, small exponents) is 2–6 characters.
    /// Using 10 as the hint covers the vast majority of real-world floats without the
    /// 24-byte worst-case causing 96-byte allocations for small structs.  Under-estimation
    /// only causes a single reallocation, whereas over-estimation wastes allocator headroom
    /// and pushes small structs into larger (slower) allocator size classes.
    #[inline] fn json_size_hint(&self) -> usize { 10 }
}

impl ToJson for f32 {
    #[inline]
    fn json_write(&self, w: &mut Vec<u8>) {
        if !self.is_finite() { w.extend_from_slice(b"null"); return; }
        #[cfg(feature = "zmij-float-ser")]
        {
            let mut buf = zmij::Buffer::new();
            w.extend_from_slice(buf.format_finite(*self).as_bytes());
            return;
        }
        #[cfg(all(feature = "fast-float", not(feature = "zmij-float-ser")))]
        {
            let mut buf = ryu::Buffer::new();
            w.extend_from_slice(buf.format_finite(*self).as_bytes());
            return;
        }
        #[cfg(not(any(feature = "fast-float", feature = "zmij-float-ser")))]
        w.extend_from_slice(format!("{}", self).as_bytes());
    }
    /// ryu's output for f32 is at most 14 characters.
    #[inline] fn json_size_hint(&self) -> usize { 14 }
}

// ── char ──────────────────────────────────────────────────────────────────────

impl ToJson for char {
    #[inline]
    fn json_write(&self, w: &mut Vec<u8>) {
        let mut buf = [0u8; 4];
        write_escaped_str(self.encode_utf8(&mut buf), w);
    }
    /// At most 4 UTF-8 bytes + 2 surrounding quotes.
    #[inline] fn json_size_hint(&self) -> usize { 6 }
}

// ── unit → null ───────────────────────────────────────────────────────────────

impl ToJson for () {
    #[inline] fn json_write(&self, w: &mut Vec<u8>) { w.extend_from_slice(b"null"); }
    #[inline] fn json_size_hint(&self) -> usize { 4 }
}

// ── HashMap / BTreeMap → JSON objects ────────────────────────────────────────

impl<K: ToJson, V: ToJson> ToJson for HashMap<K, V> {
    fn json_write(&self, w: &mut Vec<u8>) {
        w.push(b'{');
        let mut first = true;
        for (k, v) in self {
            if !first { w.push(b','); }
            first = false;
            k.json_write(w);
            w.push(b':');
            v.json_write(w);
        }
        w.push(b'}');
    }
    #[inline]
    fn json_size_hint(&self) -> usize {
        if self.is_empty() { return 2; }
        let (k, v) = self.iter().next().unwrap();
        2 + self.len() * (k.json_size_hint() + 1 + v.json_size_hint() + 1)
    }
}

impl<K: ToJson, V: ToJson> ToJson for BTreeMap<K, V> {
    fn json_write(&self, w: &mut Vec<u8>) {
        w.push(b'{');
        let mut first = true;
        for (k, v) in self {
            if !first { w.push(b','); }
            first = false;
            k.json_write(w);
            w.push(b':');
            v.json_write(w);
        }
        w.push(b'}');
    }
    #[inline]
    fn json_size_hint(&self) -> usize {
        if self.is_empty() { return 2; }
        let (k, v) = self.iter().next().unwrap();
        2 + self.len() * (k.json_size_hint() + 1 + v.json_size_hint() + 1)
    }
}

// ── tuples → JSON arrays (1- to 12-element) ───────────────────────────────────

macro_rules! impl_tuple_to_json {
    ($($T:ident . $idx:tt),+) => {
        impl<$($T: ToJson),+> ToJson for ($($T,)+) {
            fn json_write(&self, w: &mut Vec<u8>) {
                w.push(b'[');
                let mut first = true;
                $( if !first { w.push(b','); } first = false; self.$idx.json_write(w); )+
                let _ = first;
                w.push(b']');
            }
            #[inline]
            fn json_size_hint(&self) -> usize {
                2 + $( self.$idx.json_size_hint() + 1 + )+ 0
                  - 1 // subtract trailing extra comma count
            }
        }
    };
}

impl_tuple_to_json!(A.0);
impl_tuple_to_json!(A.0, B.1);
impl_tuple_to_json!(A.0, B.1, C.2);
impl_tuple_to_json!(A.0, B.1, C.2, D.3);
impl_tuple_to_json!(A.0, B.1, C.2, D.3, E.4);
impl_tuple_to_json!(A.0, B.1, C.2, D.3, E.4, F.5);
impl_tuple_to_json!(A.0, B.1, C.2, D.3, E.4, F.5, G.6);
impl_tuple_to_json!(A.0, B.1, C.2, D.3, E.4, F.5, G.6, H.7);
impl_tuple_to_json!(A.0, B.1, C.2, D.3, E.4, F.5, G.6, H.7, I.8);
impl_tuple_to_json!(A.0, B.1, C.2, D.3, E.4, F.5, G.6, H.7, I.8, J.9);
impl_tuple_to_json!(A.0, B.1, C.2, D.3, E.4, F.5, G.6, H.7, I.8, J.9, K.10);
impl_tuple_to_json!(A.0, B.1, C.2, D.3, E.4, F.5, G.6, H.7, I.8, J.9, K.10, L.11);