json-tools-rs 0.9.2

A high-performance Rust library for advanced JSON manipulation with SIMD-accelerated parsing, Rayon parallelism, and Python bindings with DataFrame/Series support
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
//! Key and value replacement transformations.
//!
//! Applies regex-based (with literal fallback) replacements to JSON keys and
//! values. Uses cached regex compilation and SIMD-accelerated substring search.
//!
//! Normal-mode processing uses tape-based streaming: scan the input once with
//! `scan_and_fixup`, then walk the tape writing directly to output with inline
//! value transforms, key transforms, and rollback-based filtering.

use crate::cache::{get_cached_regex, parse_pattern, ParsedPattern};
use crate::config::ProcessingConfig;
use crate::convert::try_convert_string_to_json_bytes;
use crate::error::JsonToolsError;
use crate::flatten::{
    apply_value_replacement_cow, escape_json_string, scan_and_fixup, skip_tape_value,
    tape_content_str, tape_is_empty_array, tape_is_empty_object, tape_quoted_str,
    tape_scalar_bytes, trim_ascii, unescape_json_string, write_json_escaped_key, EntryKind,
    TapeEntry,
};
use crate::fxhash::FxHashMap;
use memchr::memmem;
use serde_json::Value;
use smallvec::SmallVec;
use std::borrow::Cow;

// ================================================================================================
// Key/Value Replacement Core Logic
// ================================================================================================

/// Core key replacement logic that works with both string keys and Cow<str>
/// This eliminates duplication between flatten and unflatten key replacement functions
/// Optimized to minimize string allocations by using efficient Cow operations
///
/// A pattern wrapped as `r'...'` (e.g. `r'^admin_'`) is a regex; a bare pattern is matched
/// literally. See `crate::cache::ParsedPattern`. A malformed r'...' regex is silently
/// skipped (treated as no match) rather than propagating a compile error through this
/// hot path -- there's no config-time validation point for replacement patterns today.
#[inline(always)]
pub(crate) fn apply_key_replacement_patterns(
    key: &str,
    patterns: &[(String, String)],
) -> Result<Option<String>, JsonToolsError> {
    let mut new_key = Cow::Borrowed(key);
    let mut changed = false;

    // Apply each replacement pattern
    for (pattern, replacement) in patterns {
        match parse_pattern(pattern) {
            ParsedPattern::Regex(inner) => {
                if let Ok(regex) = get_cached_regex(inner) {
                    // Use replace_all's Cow return to detect matches without a separate
                    // is_match() scan -- Owned means replacement happened, Borrowed means not.
                    if let Cow::Owned(s) = regex.replace_all(&new_key, replacement.as_str()) {
                        new_key = Cow::Owned(s);
                        changed = true;
                    }
                }
            }
            ParsedPattern::Literal(lit) => {
                // SIMD-accelerated memmem::find instead of str::contains
                if memmem::find(new_key.as_bytes(), lit.as_bytes()).is_some() {
                    new_key = Cow::Owned(new_key.replace(lit, replacement));
                    changed = true;
                }
            }
        }
    }

    if changed {
        Ok(Some(new_key.into_owned()))
    } else {
        Ok(None)
    }
}

/// Core value replacement logic that works with any Value type.
/// Delegates to `apply_key_replacement_patterns` for the shared regex/literal
/// replacement logic, then writes the result back into the Value.
#[inline]
pub(crate) fn apply_value_replacement_patterns(
    value: &mut Value,
    patterns: &[(String, String)],
) -> Result<(), JsonToolsError> {
    if let Value::String(s) = value {
        if let Some(replaced) = apply_key_replacement_patterns(s, patterns)? {
            *s = replaced;
        }
    }
    Ok(())
}

// ================================================================================================
// Normal Mode Processing — Tape-Based Streaming
// ================================================================================================

/// Core normal-mode logic for a single JSON string.
/// Uses tape-based streaming: scan once, walk once, write directly to output.
/// Two paths: fast (no key transforms) and slow (with key transforms + collision handling).
#[inline]
pub(crate) fn process_single_json_normal(
    json: &str,
    config: &ProcessingConfig,
) -> Result<String, JsonToolsError> {
    let input = json.as_bytes();
    if input.len() > u32::MAX as usize {
        return Err(JsonToolsError::input_validation_error(
            "Input exceeds 4 GiB limit",
        ));
    }

    let trimmed = trim_ascii(input);
    if trimmed.is_empty() {
        return Ok(json.to_string());
    }

    // Root primitives: handle without tape
    match trimmed[0] {
        b'{' | b'[' => {}
        _ => return handle_root_primitive(trimmed, config),
    }

    let tape = scan_and_fixup(input)?;
    if tape.is_empty() {
        return Ok(json.to_string());
    }

    let needs_slow = config.lowercase_keys
        || config.replacements.has_key_replacements()
        || config.collision.has_collision_handling();

    if needs_slow {
        normal_slow_walk(input, &tape, config)
    } else {
        Ok(normal_fast_walk(input, &tape, config))
    }
}

/// Handle root-level primitives (strings, numbers, booleans, null)
fn handle_root_primitive(
    trimmed: &[u8],
    config: &ProcessingConfig,
) -> Result<String, JsonToolsError> {
    // SAFETY: input is valid UTF-8 (came from &str)
    let s = unsafe { std::str::from_utf8_unchecked(trimmed) };

    // Check if it's a quoted string
    if trimmed.len() >= 2 && trimmed[0] == b'"' && trimmed[trimmed.len() - 1] == b'"' {
        let content = &s[1..s.len() - 1];

        // Value replacement
        if config.replacements.has_value_replacements() {
            let unescaped = if memchr::memchr(b'\\', content.as_bytes()).is_some() {
                unescape_json_string(content)
            } else {
                Cow::Borrowed(content)
            };
            if let Some(replaced) = apply_value_replacement_cow(
                unescaped.as_ref(),
                &config.replacements.value_replacements,
            ) {
                // Re-escape and return
                let escaped = escape_json_string(&replaced);
                return Ok(format!("\"{}\"", escaped));
            }
        }

        // Type conversion
        if config.auto_convert_types {
            let unescaped = if memchr::memchr(b'\\', content.as_bytes()).is_some() {
                unescape_json_string(content)
            } else {
                Cow::Borrowed(content)
            };
            if let Some(converted) = try_convert_string_to_json_bytes(unescaped.as_ref()) {
                return Ok(converted.into_owned());
            }
        }
    }

    // Filtering for null
    if config.filtering.remove_nulls && trimmed == b"null" {
        // Root null with remove_nulls — return "null" since we can't remove the root
        return Ok(s.to_string());
    }

    Ok(s.to_string())
}

// ================================================================================================
// Shared String Emission Logic
// ================================================================================================

/// Shared logic for emitting a string value with replacements, type conversion, and filtering.
/// Used by both NormalFastWalker (fast path) and NormalSlowWalker (key-transform path).
///
/// Returns `(was_filtered, output_was_written)`. When `output_was_written` is false, the caller
/// should fall back to zero-copy output of the raw quoted string.
#[inline(always)]
fn emit_string_value_shared(
    output: &mut String,
    input: &[u8],
    entry: TapeEntry,
    config: &ProcessingConfig,
) -> (bool, bool) {
    let content_str = tape_content_str(input, entry);
    let has_replacements = config.replacements.has_value_replacements();
    let auto_convert = config.auto_convert_types;

    // Early exit: nothing to transform
    if !has_replacements && !auto_convert {
        return (false, false);
    }

    // Unescape once if needed by either replacement or conversion
    let unescaped = if entry.string_has_escapes() {
        unescape_json_string(content_str)
    } else {
        Cow::Borrowed(content_str)
    };

    // Value replacement
    if has_replacements {
        if let Some(replaced) =
            apply_value_replacement_cow(unescaped.as_ref(), &config.replacements.value_replacements)
        {
            if config.filtering.remove_empty_strings && replaced.is_empty() {
                return (true, true);
            }
            // Type convert the replaced value
            if auto_convert {
                if let Some(converted) = try_convert_string_to_json_bytes(&replaced) {
                    if config.filtering.remove_nulls && converted == "null" {
                        return (true, true);
                    }
                    output.push_str(&converted);
                    return (false, true);
                }
            }
            let escaped = escape_json_string(&replaced);
            output.push('"');
            output.push_str(escaped.as_ref());
            output.push('"');
            return (false, true);
        }
    }

    // Type conversion (no replacement occurred)
    if auto_convert {
        if let Some(converted) = try_convert_string_to_json_bytes(unescaped.as_ref()) {
            if config.filtering.remove_nulls && converted == "null" {
                return (true, true);
            }
            output.push_str(&converted);
            return (false, true);
        }
    }

    (false, false)
}

// ================================================================================================
// Fast Path — NormalFastWalker (no key transforms)
// ================================================================================================

/// Tape-based streaming walker for normal mode without key transforms.
/// Single-pass: walks tape and writes directly to output with inline value transforms
/// and rollback-based filtering.
struct NormalFastWalker<'a> {
    input: &'a [u8],
    tape: &'a [TapeEntry],
    config: &'a ProcessingConfig,
    output: String,
}

/// Walk the tape in fast mode (no key transforms) and produce output directly.
fn normal_fast_walk(input: &[u8], tape: &[TapeEntry], config: &ProcessingConfig) -> String {
    let mut walker = NormalFastWalker {
        input,
        tape,
        config,
        output: String::with_capacity(input.len()),
    };
    walker.walk_value(0);
    walker.output
}

impl<'a> NormalFastWalker<'a> {
    /// Walk a single value. Returns (next_tape_idx, was_filtered).
    #[inline(always)]
    fn walk_value(&mut self, idx: usize) -> (usize, bool) {
        if idx >= self.tape.len() {
            return (idx, false);
        }
        let entry = self.tape[idx];
        match entry.kind() {
            EntryKind::ObjectStart => self.walk_object(idx),
            EntryKind::ArrayStart => self.walk_array(idx),
            EntryKind::StringStart => self.emit_string_value(idx),
            EntryKind::ScalarStart => self.emit_scalar_value(idx),
            _ => (idx + 1, false),
        }
    }

    /// Walk an object: emit {key:value,...} with filtering rollback.
    fn walk_object(&mut self, start_idx: usize) -> (usize, bool) {
        let end_idx = self.tape[start_idx].aux() as usize;

        // Empty object check
        if tape_is_empty_object(self.tape, start_idx) {
            if self.config.filtering.remove_empty_objects {
                return (end_idx + 1, true);
            }
            self.output.push_str("{}");
            return (end_idx + 1, false);
        }

        let obj_start = self.output.len();
        self.output.push('{');
        let mut first = true;
        let mut cursor = start_idx + 1;

        while cursor < end_idx {
            let entry = self.tape[cursor];
            if entry.kind() == EntryKind::StringStart {
                // This is a key
                let checkpoint = self.output.len();

                // Emit comma separator
                if !first {
                    self.output.push(',');
                }

                // Emit key (raw copy from input including quotes)
                let key_with_quotes = tape_quoted_str(self.input, entry);
                self.output.push_str(key_with_quotes);

                // Skip key and colon
                cursor += 1;
                if cursor < end_idx && self.tape[cursor].kind() == EntryKind::Colon {
                    self.output.push(':');
                    cursor += 1;
                }

                // Walk value
                let (next, filtered) = self.walk_value(cursor);
                cursor = next;

                if filtered {
                    // Rollback: remove comma + key + colon + value
                    self.output.truncate(checkpoint);
                } else {
                    first = false;
                }
            } else if entry.kind() == EntryKind::Comma {
                // Skip comma tape entries — we manage commas ourselves
                cursor += 1;
            } else {
                cursor += 1;
            }
        }

        // Check if object became empty after filtering
        if self.output.len() == obj_start + 1 && self.config.filtering.remove_empty_objects {
            self.output.truncate(obj_start);
            return (end_idx + 1, true);
        }

        self.output.push('}');
        (end_idx + 1, false)
    }

    /// Walk an array: emit [val,...] with filtering rollback.
    fn walk_array(&mut self, start_idx: usize) -> (usize, bool) {
        let end_idx = self.tape[start_idx].aux() as usize;

        // Empty array check
        if tape_is_empty_array(self.tape, start_idx) {
            if self.config.filtering.remove_empty_arrays {
                return (end_idx + 1, true);
            }
            self.output.push_str("[]");
            return (end_idx + 1, false);
        }

        let arr_start = self.output.len();
        self.output.push('[');
        let mut first = true;
        let mut cursor = start_idx + 1;

        while cursor < end_idx {
            let entry = self.tape[cursor];
            if entry.kind() == EntryKind::Comma {
                cursor += 1;
                continue;
            }

            let checkpoint = self.output.len();
            if !first {
                self.output.push(',');
            }

            let (next, filtered) = self.walk_value(cursor);
            cursor = next;

            if filtered {
                self.output.truncate(checkpoint);
            } else {
                first = false;
            }
        }

        // Check if array became empty after filtering
        if self.output.len() == arr_start + 1 && self.config.filtering.remove_empty_arrays {
            self.output.truncate(arr_start);
            return (end_idx + 1, true);
        }

        self.output.push(']');
        (end_idx + 1, false)
    }

    /// Emit a string value with inline transforms. Returns (next_idx, was_filtered).
    #[inline(always)]
    fn emit_string_value(&mut self, idx: usize) -> (usize, bool) {
        let entry = self.tape[idx];

        // Filtering: empty string
        if self.config.filtering.remove_empty_strings && entry.string_content_len() == 0 {
            return (idx + 1, true);
        }

        let (was_filtered, was_written) =
            emit_string_value_shared(&mut self.output, self.input, entry, self.config);
        if was_written {
            return (idx + 1, was_filtered);
        }

        // Zero-copy: raw string including quotes
        self.output.push_str(tape_quoted_str(self.input, entry));
        (idx + 1, false)
    }

    /// Emit a scalar value (number, bool, null). Returns (next_idx, was_filtered).
    #[inline(always)]
    fn emit_scalar_value(&mut self, idx: usize) -> (usize, bool) {
        let entry = self.tape[idx];
        let trimmed = trim_ascii(tape_scalar_bytes(self.input, entry));

        if self.config.filtering.remove_nulls && trimmed == b"null" {
            return (idx + 1, true);
        }

        let s = unsafe { std::str::from_utf8_unchecked(trimmed) };
        self.output.push_str(s);
        (idx + 1, false)
    }
}

// ================================================================================================
// Slow Path — NormalSlowWalker (key transforms + collision handling)
// ================================================================================================

/// Per-object entry with transformed key and tape index for the value.
struct SlowObjectEntry {
    key: String,
    val_start: usize,
}

/// Lowercase `s`, using full Unicode case-folding (e.g. 'Ñ' -> 'ñ') for correctness.
/// `str::to_lowercase()` always allocates a new String even when nothing changes, so this
/// first does a cheap scan for any uppercase character and returns `s` unchanged if none are
/// found -- the common case for real-world JSON keys, which are typically already lowercase.
#[inline]
fn lowercase_if_needed(s: String) -> String {
    if s.chars().any(char::is_uppercase) {
        s.to_lowercase()
    } else {
        s
    }
}

/// Walk the tape with key transforms and collision handling.
fn normal_slow_walk(
    input: &[u8],
    tape: &[TapeEntry],
    config: &ProcessingConfig,
) -> Result<String, JsonToolsError> {
    let mut walker = NormalSlowWalker {
        input,
        tape,
        config,
        output: String::with_capacity(input.len()),
    };
    walker.walk_value(0)?;
    Ok(walker.output)
}

struct NormalSlowWalker<'a> {
    input: &'a [u8],
    tape: &'a [TapeEntry],
    config: &'a ProcessingConfig,
    output: String,
}

impl<'a> NormalSlowWalker<'a> {
    /// Walk a single value. Returns (next_tape_idx, was_filtered).
    fn walk_value(&mut self, idx: usize) -> Result<(usize, bool), JsonToolsError> {
        if idx >= self.tape.len() {
            return Ok((idx, false));
        }
        let entry = self.tape[idx];
        match entry.kind() {
            EntryKind::ObjectStart => self.walk_object(idx),
            EntryKind::ArrayStart => self.walk_array(idx),
            EntryKind::StringStart => Ok(self.emit_string_value(idx)),
            EntryKind::ScalarStart => Ok(self.emit_scalar_value(idx)),
            _ => Ok((idx + 1, false)),
        }
    }

    /// Walk an object with key transforms and collision handling.
    fn walk_object(&mut self, start_idx: usize) -> Result<(usize, bool), JsonToolsError> {
        let end_idx = self.tape[start_idx].aux() as usize;

        // Empty object check
        if tape_is_empty_object(self.tape, start_idx) {
            if self.config.filtering.remove_empty_objects {
                return Ok((end_idx + 1, true));
            }
            self.output.push_str("{}");
            return Ok((end_idx + 1, false));
        }

        // Phase 1: Collect entries with transformed keys
        let mut entries: SmallVec<[SlowObjectEntry; 16]> = SmallVec::new();
        let mut cursor = start_idx + 1;

        while cursor < end_idx {
            let entry = self.tape[cursor];
            if entry.kind() == EntryKind::StringStart {
                // Extract and transform key
                let key_str = tape_content_str(self.input, entry);

                let key_unescaped = if entry.string_has_escapes() {
                    unescape_json_string(key_str)
                } else {
                    Cow::Borrowed(key_str)
                };

                // Apply key replacement patterns
                let mut transformed_key = if self.config.replacements.has_key_replacements() {
                    apply_key_replacement_patterns(
                        key_unescaped.as_ref(),
                        &self.config.replacements.key_replacements,
                    )?
                    .unwrap_or_else(|| key_unescaped.into_owned())
                } else {
                    key_unescaped.into_owned()
                };

                // Apply lowercase (full Unicode case-folding, e.g. 'Ñ' -> 'ñ')
                if self.config.lowercase_keys {
                    transformed_key = lowercase_if_needed(transformed_key);
                }

                // Skip key and colon
                cursor += 1;
                if cursor < end_idx && self.tape[cursor].kind() == EntryKind::Colon {
                    cursor += 1;
                }

                // Determine value tape position and advance cursor past it
                let val_start = cursor;
                cursor = skip_tape_value(self.tape, cursor);

                entries.push(SlowObjectEntry {
                    key: transformed_key,
                    val_start,
                });
            } else {
                // Skip commas and any other structural entries
                cursor += 1;
            }
        }

        // Phase 2: Detect collisions
        let has_collisions = if self.config.collision.has_collision_handling() {
            let mut seen = FxHashMap::with_capacity_and_hasher(entries.len(), Default::default());
            entries.iter().any(|e| seen.insert(&e.key, ()).is_some())
        } else {
            false
        };

        // Phase 3: Serialize
        let obj_start = self.output.len();
        self.output.push('{');

        if has_collisions {
            self.serialize_with_collisions(&entries)?;
        } else {
            self.serialize_no_collisions(&entries)?;
        }

        // Check if object became empty after filtering
        if self.output.len() == obj_start + 1 && self.config.filtering.remove_empty_objects {
            self.output.truncate(obj_start);
            return Ok((end_idx + 1, true));
        }

        self.output.push('}');
        Ok((end_idx + 1, false))
    }

    /// Serialize object entries without collisions.
    fn serialize_no_collisions(
        &mut self,
        entries: &[SlowObjectEntry],
    ) -> Result<(), JsonToolsError> {
        let mut first = true;
        for entry in entries {
            let checkpoint = self.output.len();
            if !first {
                self.output.push(',');
            }

            // Emit escaped key
            self.output.push('"');
            write_json_escaped_key(&mut self.output, &entry.key);
            self.output.push_str("\":");

            // Recurse into value
            let (_, filtered) = self.walk_value(entry.val_start)?;

            if filtered {
                self.output.truncate(checkpoint);
            } else {
                first = false;
            }
        }
        Ok(())
    }

    /// Serialize object entries with collision merging.
    fn serialize_with_collisions(
        &mut self,
        entries: &[SlowObjectEntry],
    ) -> Result<(), JsonToolsError> {
        // Group by key, preserving order of first occurrence
        let mut key_order: Vec<&str> = Vec::new();
        let mut key_indices: FxHashMap<&str, SmallVec<[usize; 2]>> =
            FxHashMap::with_capacity_and_hasher(entries.len(), Default::default());

        for (i, entry) in entries.iter().enumerate() {
            key_indices
                .entry(entry.key.as_str())
                .and_modify(|v| v.push(i))
                .or_insert_with(|| {
                    key_order.push(entry.key.as_str());
                    SmallVec::from_elem(i, 1)
                });
        }

        let mut first = true;
        for key in &key_order {
            let indices = &key_indices[key];

            if indices.len() == 1 {
                // Single entry — no collision
                let entry = &entries[indices[0]];
                let checkpoint = self.output.len();
                if !first {
                    self.output.push(',');
                }

                self.output.push('"');
                write_json_escaped_key(&mut self.output, &entry.key);
                self.output.push_str("\":");

                let (_, filtered) = self.walk_value(entry.val_start)?;
                if filtered {
                    self.output.truncate(checkpoint);
                } else {
                    first = false;
                }
            } else {
                // Collision — merge values into array
                let checkpoint = self.output.len();
                if !first {
                    self.output.push(',');
                }

                self.output.push('"');
                write_json_escaped_key(&mut self.output, key);
                self.output.push_str("\":[");

                let arr_start = self.output.len();
                let mut arr_first = true;

                for &idx in indices {
                    let entry = &entries[idx];
                    let val_checkpoint = self.output.len();
                    if !arr_first {
                        self.output.push(',');
                    }

                    let (_, filtered) = self.walk_value(entry.val_start)?;
                    if filtered {
                        self.output.truncate(val_checkpoint);
                    } else {
                        arr_first = false;
                    }
                }

                // If all values in the collision array were filtered, remove the entire entry
                if self.output.len() == arr_start {
                    self.output.truncate(checkpoint);
                } else {
                    self.output.push(']');
                    first = false;
                }
            }
        }

        Ok(())
    }

    /// Walk an array with filtering rollback.
    fn walk_array(&mut self, start_idx: usize) -> Result<(usize, bool), JsonToolsError> {
        let end_idx = self.tape[start_idx].aux() as usize;

        // Empty array check
        if tape_is_empty_array(self.tape, start_idx) {
            if self.config.filtering.remove_empty_arrays {
                return Ok((end_idx + 1, true));
            }
            self.output.push_str("[]");
            return Ok((end_idx + 1, false));
        }

        let arr_start = self.output.len();
        self.output.push('[');
        let mut first = true;
        let mut cursor = start_idx + 1;

        while cursor < end_idx {
            let entry = self.tape[cursor];
            if entry.kind() == EntryKind::Comma {
                cursor += 1;
                continue;
            }

            let checkpoint = self.output.len();
            if !first {
                self.output.push(',');
            }

            let (next, filtered) = self.walk_value(cursor)?;
            cursor = next;

            if filtered {
                self.output.truncate(checkpoint);
            } else {
                first = false;
            }
        }

        // Check if array became empty after filtering
        if self.output.len() == arr_start + 1 && self.config.filtering.remove_empty_arrays {
            self.output.truncate(arr_start);
            return Ok((end_idx + 1, true));
        }

        self.output.push(']');
        Ok((end_idx + 1, false))
    }

    /// Emit a string value with inline transforms. Returns (next_idx, was_filtered).
    #[inline(always)]
    fn emit_string_value(&mut self, idx: usize) -> (usize, bool) {
        let entry = self.tape[idx];

        // Filtering: empty string
        if self.config.filtering.remove_empty_strings && entry.string_content_len() == 0 {
            return (idx + 1, true);
        }

        let (was_filtered, was_written) =
            emit_string_value_shared(&mut self.output, self.input, entry, self.config);
        if was_written {
            return (idx + 1, was_filtered);
        }

        // Zero-copy: raw string including quotes
        self.output.push_str(tape_quoted_str(self.input, entry));
        (idx + 1, false)
    }

    /// Emit a scalar value. Returns (next_idx, was_filtered).
    #[inline(always)]
    fn emit_scalar_value(&mut self, idx: usize) -> (usize, bool) {
        let entry = self.tape[idx];
        let trimmed = trim_ascii(tape_scalar_bytes(self.input, entry));

        if self.config.filtering.remove_nulls && trimmed == b"null" {
            return (idx + 1, true);
        }

        let s = unsafe { std::str::from_utf8_unchecked(trimmed) };
        self.output.push_str(s);
        (idx + 1, false)
    }
}