jshift 0.1.0

Schema-guided, safe in-place JSON path reader and mutator for raw byte buffers.
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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
#![forbid(unsafe_code)]

//! `jshift` — Schema-guided, safe in-place JSON path reader and mutator.
//!
//! This crate provides a 100% safe Rust engine to selectively read, mutate, upsert,
//! and delete values inside raw JSON byte buffers (`&[u8]` and `Vec<u8>`) without
//! building a full AST. Path scans return zero-copy slices; mutations resize the
//! buffer and shift the tail with safe slice rotations.
//!
//! # Features
//! * **Zero-copy reads:** Find values as slices into the raw buffer.
//! * **In-place mutations:** Safe byte-shifting (including resize) via slice rotations.
//! * **Macro-generated schemas:** `#[derive(JsonMutatorSchema)]` for typed readers and mutators.
//! * **Array and object CRUD:** Insert, update, append, and delete dynamically.
//! * **JSON string escaping:** `ToJsonBytes` and key upserts escape special characters.
//!
//! # Quick Start
//! ```
//! use jshift::{find_value, mutate_value, parse_path};
//!
//! let mut json = b"{\"user\": \"farmer\", \"score\": 9.5}".to_vec();
//! let path = parse_path("score");
//!
//! // Read value
//! let score_bytes = find_value(&json, &path).unwrap();
//! assert_eq!(score_bytes, b"9.5");
//!
//! // Mutate in-place
//! mutate_value(&mut json, &path, b"10.0").unwrap();
//! assert_eq!(json, b"{\"user\": \"farmer\", \"score\": 10.0}".to_vec());
//! ```
//!
//! # High-Impact Real-World Use Case: LLM Dataset Processing (JSONL)
//! In AI training pipelines (e.g., LoRA finetuning), datasets are stored as JSONL files.
//! You can inspect token lengths and mark records as skipped or cleaned in-place:
//!
//! ```
//! use jshift::JsonMutatorSchema;
//!
//! #[derive(JsonMutatorSchema)]
//! struct TrainingRecord {
//!     #[json(path = "tokens")]
//!     tokens: usize,
//!     #[json(path = "status")]
//!     status: String,
//! }
//!
//! let mut line = b"{\"instruction\": \"Translate...\", \"tokens\": 1024, \"status\": \"pending\"}".to_vec();
//!
//! // Parse selectively
//! let record = TrainingRecord::read_from_json(&line).unwrap();
//!
//! // Skip long contexts in-place!
//! if record.tokens > 512 {
//!     let mut mutator = TrainingRecord::mutator(&mut line);
//!     mutator.set_status("skipped").unwrap();
//! }
//!
//! assert_eq!(
//!     line,
//!     b"{\"instruction\": \"Translate...\", \"tokens\": 1024, \"status\": \"skipped\"}".to_vec()
//! );
//! ```

use memchr::memchr;

pub use jshift_derive::JsonMutatorSchema;

/// Errors returned by scanning and mutating operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
    /// The specified JSON path was not found in the document.
    PathNotFound,
    /// The JSON document is structurally malformed.
    InvalidJsonSyntax { 
        /// Byte offset in the JSON buffer where the syntax error was detected.
        pos: usize, 
        /// Informative message describing the syntax error.
        msg: &'static str 
    },
    /// The array index is larger than the number of elements in the array.
    IndexOutOfBounds { 
        /// The index that was queried.
        index: usize 
    },
    /// The parsed type does not match the JSON value format.
    TypeMismatch { 
        /// Expected type name (e.g. `"array"`, `"object"`).
        expected: &'static str, 
        /// Encountered type name (e.g. `"primitive"`).
        found: &'static str 
    },
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::PathNotFound => write!(f, "Path not found in JSON"),
            Error::InvalidJsonSyntax { pos, msg } => {
                write!(f, "Invalid JSON syntax at position {}: {}", pos, msg)
            }
            Error::IndexOutOfBounds { index } => {
                write!(f, "Array index out of bounds: {}", index)
            }
            Error::TypeMismatch { expected, found } => {
                write!(
                    f,
                    "Type mismatch: expected '{}', found '{}'",
                    expected, found
                )
            }
        }
    }
}

impl std::error::Error for Error {}

/// Represents a segment in a JSON path (either a string key or a zero-indexed array index).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PathSegment<'a> {
    /// A string key representing an object field (e.g., `user` in `{"user": 1}`).
    Key(&'a str),
    /// A numeric index representing an array position (e.g., `0` in `[10, 20]`).
    Index(usize),
}

/// Parses a dot-and-bracket notation path string into a vector of zero-copy path segments.
///
/// # Examples
/// ```
/// use jshift::{parse_path, PathSegment};
///
/// let path = parse_path("metadata.tags[0].name");
/// assert_eq!(path, vec![
///     PathSegment::Key("metadata"),
///     PathSegment::Key("tags"),
///     PathSegment::Index(0),
///     PathSegment::Key("name")
/// ]);
/// ```
pub fn parse_path(mut s: &str) -> Vec<PathSegment<'_>> {
    let mut segments = Vec::new();
    while !s.is_empty() {
        if s.starts_with('.') {
            s = &s[1..];
        }
        if s.is_empty() {
            break;
        }
        if s.starts_with('[') {
            let end_idx = s.find(']').unwrap_or(s.len());
            let idx_str = &s[1..end_idx];
            if let Ok(idx) = idx_str.parse::<usize>() {
                segments.push(PathSegment::Index(idx));
            }
            s = if end_idx < s.len() { &s[end_idx + 1..] } else { "" };
        } else {
            let end_key = s.find(|c| c == '.' || c == '[').unwrap_or(s.len());
            let key = &s[..end_key];
            segments.push(PathSegment::Key(key));
            s = &s[end_key..];
        }
    }
    segments
}

/// Locates a JSON value byte-slice within a raw JSON buffer by its path.
///
/// Returns the slice of the raw JSON value (e.g., `b"123"`, `b"\"hello\""`, or `b"{\"nested\": true}"`).
///
/// # Examples
/// ```
/// use jshift::{find_value, parse_path};
///
/// let json = b"{\"metadata\": {\"author\": \"farmer\"}}";
/// let val = find_value(json, &parse_path("metadata.author")).unwrap();
/// assert_eq!(val, b"\"farmer\"");
/// ```
pub fn find_value<'a>(json: &'a [u8], path: &[PathSegment]) -> Result<&'a [u8], Error> {
    let (start, end) = find_value_offsets(json, path)?;
    Ok(&json[start..end])
}

/// Helper that locates a JSON value byte-slice boundaries within a raw JSON buffer by its path.
/// Returns a tuple of `(start_index, end_index)`.
fn find_value_offsets(json: &[u8], path: &[PathSegment]) -> Result<(usize, usize), Error> {
    if path.is_empty() {
        return Ok((0, json.len()));
    }
    let pos = skip_whitespace(json, 0);
    if pos >= json.len() {
        return Err(Error::InvalidJsonSyntax { pos, msg: "Unexpected EOF" });
    }
    
    match &path[0] {
        PathSegment::Key(_) => {
            if json[pos] != b'{' {
                return Err(Error::InvalidJsonSyntax { pos, msg: "Expected opening brace '{' for object" });
            }
            find_in_object_offsets(json, pos + 1, path)
        }
        PathSegment::Index(_) => {
            if json[pos] != b'[' {
                return Err(Error::InvalidJsonSyntax { pos, msg: "Expected opening bracket '[' for array" });
            }
            find_in_array_offsets(json, pos + 1, path)
        }
    }
}

/// Recursively or iteratively scans an object starting after the '{' or after a key-value comma.
fn find_in_object_offsets(json: &[u8], mut pos: usize, path: &[PathSegment]) -> Result<(usize, usize), Error> {
    let target_key = match &path[0] {
        PathSegment::Key(key) => key.as_bytes(),
        _ => return Err(Error::PathNotFound),
    };
    
    loop {
        pos = skip_whitespace(json, pos);
        if pos >= json.len() {
            return Err(Error::InvalidJsonSyntax { pos, msg: "Unexpected EOF" });
        }

        // Check if we hit the end of the object before finding the key
        if json[pos] == b'}' {
            return Err(Error::PathNotFound);
        }

        // We expect a string key starting with '"'
        if json[pos] != b'"' {
            return Err(Error::InvalidJsonSyntax { pos, msg: "Expected object key string starting with double quote" });
        }
        
        let key_start = pos + 1;
        let key_end = find_string_end(json, key_start)?;
        let key = &json[key_start..key_end];
        
        pos = key_end + 1; // move past closing '"'
        
        // Skip whitespace and locate the ':' delimiter
        pos = skip_whitespace(json, pos);
        if pos >= json.len() || json[pos] != b':' {
            return Err(Error::InvalidJsonSyntax { pos, msg: "Expected colon ':' key separator" });
        }
        pos += 1; // move past ':'
        
        pos = skip_whitespace(json, pos);
        let val_start = pos;
        let val_end = skip_value(json, val_start)?;
        
        if key == target_key {
            // We found the matching key!
            if path.len() == 1 {
                return Ok((val_start, val_end));
            } else {
                // We need to go deeper
                match &path[1] {
                    PathSegment::Key(_) => {
                        if json[val_start] == b'{' {
                            return find_in_object_offsets(json, val_start + 1, &path[1..]);
                        } else {
                            return Err(Error::TypeMismatch { expected: "object", found: "primitive/array" });
                        }
                    }
                    PathSegment::Index(_) => {
                        if json[val_start] == b'[' {
                            return find_in_array_offsets(json, val_start + 1, &path[1..]);
                        } else {
                            return Err(Error::TypeMismatch { expected: "array", found: "primitive/object" });
                        }
                    }
                }
            }
        }
        
        // Key didn't match, skip this value and look for the next comma ',' or object end '}'
        pos = val_end;
        pos = skip_whitespace(json, pos);
        if pos >= json.len() {
            return Err(Error::InvalidJsonSyntax { pos, msg: "Unexpected EOF" });
        }
        
        if json[pos] == b',' {
            pos += 1; // Move past comma to scan next key-value pair
        } else if json[pos] == b'}' {
            return Err(Error::PathNotFound); // End of object
        } else {
            return Err(Error::InvalidJsonSyntax { pos, msg: "Expected comma ',' or closing brace '}'" });
        }
    }
}

/// Recursively scans an array starting after the '[' or after an element comma.
fn find_in_array_offsets(json: &[u8], mut pos: usize, path: &[PathSegment]) -> Result<(usize, usize), Error> {
    let target_idx = match path[0] {
        PathSegment::Index(idx) => idx,
        _ => return Err(Error::PathNotFound),
    };
    
    // Skip elements to reach the target index
    for _ in 0..target_idx {
        pos = skip_whitespace(json, pos);
        if pos >= json.len() || json[pos] == b']' {
            return Err(Error::IndexOutOfBounds { index: target_idx });
        }
        pos = skip_value(json, pos)?;
        pos = skip_whitespace(json, pos);
        if pos >= json.len() {
            return Err(Error::InvalidJsonSyntax { pos, msg: "Unexpected EOF" });
        }
        if json[pos] != b',' {
            if json[pos] == b']' {
                return Err(Error::IndexOutOfBounds { index: target_idx });
            }
            return Err(Error::InvalidJsonSyntax { pos, msg: "Expected comma ',' array element separator" });
        }
        pos += 1; // skip comma
    }
    
    pos = skip_whitespace(json, pos);
    if pos >= json.len() || json[pos] == b']' {
        return Err(Error::IndexOutOfBounds { index: target_idx });
    }
    
    let val_start = pos;
    let val_end = skip_value(json, val_start)?;
    
    if path.len() == 1 {
        Ok((val_start, val_end))
    } else {
        // Go deeper
        match &path[1] {
            PathSegment::Key(_) => {
                if json[val_start] == b'{' {
                    find_in_object_offsets(json, val_start + 1, &path[1..])
                } else {
                    Err(Error::TypeMismatch { expected: "object", found: "primitive/array" })
                }
            }
            PathSegment::Index(_) => {
                if json[val_start] == b'[' {
                    find_in_array_offsets(json, val_start + 1, &path[1..])
                } else {
                    Err(Error::TypeMismatch { expected: "array", found: "primitive/object" })
                }
            }
        }
    }
}

/// Mutates a JSON value in-place inside a `Vec<u8>` buffer by its path.
///
/// If the new value's length is different from the old value's length,
/// this function shifts the remaining part of the JSON buffer using an
/// optimized, safe slice rotation.
///
/// Returns `Ok(())` on success, or `Err(Error)` on failure.
///
/// # Examples
/// ```
/// use jshift::{mutate_value, parse_path};
///
/// let mut json = b"{\"status\": \"idle\"}".to_vec();
/// mutate_value(&mut json, &parse_path("status"), b"\"running\"").unwrap();
/// assert_eq!(json, b"{\"status\": \"running\"}".to_vec());
/// ```
pub fn mutate_value(json: &mut Vec<u8>, path: &[PathSegment], new_value: &[u8]) -> Result<(), Error> {
    // 1. Locate the value offsets
    let (start, end) = find_value_offsets(json, path)?;
    
    let old_len = end - start;
    let new_len = new_value.len();
    
    if old_len == new_len {
        // Simple case: same length, just overwrite the slice
        json[start..end].copy_from_slice(new_value);
    } else {
        let old_total_len = json.len();
        
        if new_len > old_len {
            let delta = new_len - old_len;
            json.resize(old_total_len + delta, 0);
            
            // Shift the tail to the right using safe slice rotation
            let tail_slice = &mut json[end..];
            tail_slice.rotate_right(delta);
        } else {
            let delta = old_len - new_len;
            
            // Shift the tail to the left using safe slice rotation
            let tail_slice = &mut json[start + new_len..];
            tail_slice.rotate_left(delta);
            
            // Shrink the vector to remove trailing garbage
            json.truncate(old_total_len - delta);
        }
        
        // Write the new value into the gap
        json[start..start + new_len].copy_from_slice(new_value);
    }
    
    Ok(())
}

/// Appends a new value to the end of a JSON array located at the specified path.
///
/// If the array is currently empty, the value is written directly. If it contains
/// elements, a separating comma is injected before the new value.
///
/// # Examples
/// ```
/// use jshift::{append_to_array, parse_path};
///
/// let mut json = b"{\"list\": [10, 20]}".to_vec();
/// append_to_array(&mut json, &parse_path("list"), b"30").unwrap();
/// assert_eq!(json, b"{\"list\": [10, 20,30]}".to_vec());
/// ```
pub fn append_to_array(json: &mut Vec<u8>, path: &[PathSegment], new_element: &[u8]) -> Result<(), Error> {
    let (start, end) = find_value_offsets(json, path)?;
    
    if json[start] != b'[' {
        return Err(Error::TypeMismatch { expected: "array", found: "primitive/object" });
    }
    
    let insertion_point = end - 1;
    let is_empty = is_array_empty(json, start, end)?;
    let old_total_len = json.len();
    
    let delta = if is_empty {
        new_element.len()
    } else {
        1 + new_element.len()
    };
    
    json.resize(old_total_len + delta, 0);
    
    // Shift the closing bracket and everything after it to the right
    let tail_slice = &mut json[insertion_point..];
    tail_slice.rotate_right(delta);
    
    // Write the new element (and comma if not empty)
    if is_empty {
        json[insertion_point..insertion_point + new_element.len()].copy_from_slice(new_element);
    } else {
        json[insertion_point] = b',';
        json[insertion_point + 1..insertion_point + 1 + new_element.len()].copy_from_slice(new_element);
    }
    
    Ok(())
}

fn is_array_empty(json: &[u8], start: usize, end: usize) -> Result<bool, Error> {
    if start + 1 >= end {
        return Err(Error::InvalidJsonSyntax { pos: start, msg: "Empty or truncated array offsets" });
    }
    let mut pos = start + 1;
    while pos < end - 1 {
        match json[pos] {
            b' ' | b'\t' | b'\n' | b'\r' => pos += 1,
            _ => return Ok(false),
        }
    }
    Ok(true)
}

/// Returns the number of elements in the array located at the specified path.
///
/// # Examples
/// ```
/// use jshift::{array_len, parse_path};
///
/// let json = b"{\"list\": [1, 2, 3]}";
/// assert_eq!(array_len(json, &parse_path("list")).unwrap(), 3);
/// ```
pub fn array_len(json: &[u8], path: &[PathSegment]) -> Result<usize, Error> {
    let (start, _end) = find_value_offsets(json, path)?;
    if json[start] != b'[' {
        return Err(Error::TypeMismatch { expected: "array", found: "primitive/object" });
    }
    
    let mut pos = skip_whitespace(json, start + 1);
    if pos >= json.len() {
        return Err(Error::InvalidJsonSyntax { pos, msg: "Unexpected EOF" });
    }
    if json[pos] == b']' {
        return Ok(0);
    }
    
    let mut count = 1;
    loop {
        pos = skip_value(json, pos)?;
        pos = skip_whitespace(json, pos);
        if pos >= json.len() {
            return Err(Error::InvalidJsonSyntax { pos, msg: "Unexpected EOF" });
        }
        if json[pos] == b',' {
            count += 1;
            pos += 1;
        } else if json[pos] == b']' {
            break;
        } else {
            return Err(Error::InvalidJsonSyntax { pos, msg: "Expected comma ',' or closing bracket ']'" });
        }
    }
    Ok(count)
}

/// Upserts (inserts or updates) a key-value pair in a JSON object located at the specified path.
///
/// If the key already exists, its value is overwritten. If it does not exist,
/// it is appended to the object with a comma prefix (if the object is not empty).
///
/// # Examples
/// ```
/// use jshift::{upsert_object_key, parse_path};
///
/// let mut json = b"{\"a\": 1}".to_vec();
/// upsert_object_key(&mut json, &[], "b", b"2").unwrap();
/// assert_eq!(json, b"{\"a\": 1,\"b\":2}".to_vec());
/// ```
pub fn upsert_object_key(json: &mut Vec<u8>, path: &[PathSegment], key: &str, new_value: &[u8]) -> Result<(), Error> {
    let mut target_path = path.to_vec();
    target_path.push(PathSegment::Key(key));
    
    if find_value_offsets(json, &target_path).is_ok() {
        // Key exists, perform standard mutation
        return mutate_value(json, &target_path, new_value);
    }
    
    let (start, end) = find_value_offsets(json, path)?;
    if json[start] != b'{' {
        return Err(Error::TypeMismatch { expected: "object", found: "primitive/array" });
    }
    
    let insertion_point = end - 1;
    let is_empty = is_object_empty(json, start, end)?;
    let old_total_len = json.len();
    
    let mut insertion_content = Vec::new();
    if !is_empty {
        insertion_content.push(b',');
    }
    write_json_string(&mut insertion_content, key);
    insertion_content.push(b':');
    insertion_content.extend_from_slice(new_value);
    
    let delta = insertion_content.len();
    json.resize(old_total_len + delta, 0);
    
    let tail_slice = &mut json[insertion_point..];
    tail_slice.rotate_right(delta);
    
    json[insertion_point..insertion_point + delta].copy_from_slice(&insertion_content);
    
    Ok(())
}

fn is_object_empty(json: &[u8], start: usize, end: usize) -> Result<bool, Error> {
    if start + 1 >= end {
        return Err(Error::InvalidJsonSyntax { pos: start, msg: "Empty or truncated object offsets" });
    }
    let mut pos = start + 1;
    while pos < end - 1 {
        match json[pos] {
            b' ' | b'\t' | b'\n' | b'\r' => pos += 1,
            _ => return Ok(false),
        }
    }
    Ok(true)
}

/// Deletes a key-value pair from a JSON object located at the specified path.
///
/// Automatically adjusts commas surrounding the deleted key-value pair.
///
/// # Examples
/// ```
/// use jshift::{delete_key, parse_path};
///
/// let mut json = b"{\"a\": 1, \"b\": 2}".to_vec();
/// delete_key(&mut json, &[], "b").unwrap();
/// assert_eq!(json, b"{\"a\": 1}".to_vec());
/// ```
pub fn delete_key(json: &mut Vec<u8>, path: &[PathSegment], key: &str) -> Result<(), Error> {
    let mut target_path = path.to_vec();
    target_path.push(PathSegment::Key(key));
    
    let (val_start, val_end) = find_value_offsets(json, &target_path)?;
    
    // Scan backwards from val_start to locate the key string and quotes
    let mut pos = val_start;
    pos = scan_backwards_whitespace(json, pos);
    if pos == 0 || json[pos - 1] != b':' {
        return Err(Error::InvalidJsonSyntax { pos, msg: "Expected colon key separator" });
    }
    pos -= 1; // skip ':'
    
    pos = scan_backwards_whitespace(json, pos);
    if pos == 0 || json[pos - 1] != b'"' {
        return Err(Error::InvalidJsonSyntax { pos, msg: "Expected closing quote of key" });
    }
    pos -= 1; // skip '"'
    
    while pos > 0 && json[pos - 1] != b'"' {
        pos -= 1;
    }
    if pos == 0 {
        return Err(Error::InvalidJsonSyntax { pos: 0, msg: "Unclosed key string" });
    }
    let key_start = pos - 1;
    
    // Preceding comma detection
    let mut prev_comma_pos = key_start;
    prev_comma_pos = scan_backwards_whitespace(json, prev_comma_pos);
    
    let delete_start;
    let delete_end;
    
    if prev_comma_pos > 0 && json[prev_comma_pos - 1] == b',' {
        delete_start = prev_comma_pos - 1;
        delete_end = val_end;
    } else {
        // Trailing comma detection
        let mut next_comma_pos = val_end;
        next_comma_pos = skip_whitespace(json, next_comma_pos);
        if next_comma_pos < json.len() && json[next_comma_pos] == b',' {
            delete_start = key_start;
            delete_end = next_comma_pos + 1;
        } else {
            delete_start = key_start;
            delete_end = val_end;
        }
    }
    
    let delta = delete_end - delete_start;
    let old_total_len = json.len();
    
    let tail_slice = &mut json[delete_start..];
    tail_slice.rotate_left(delta);
    json.truncate(old_total_len - delta);
    
    Ok(())
}

/// Deletes an element from a JSON array located at the specified path by its index.
///
/// Automatically adjusts commas surrounding the deleted array element.
///
/// # Examples
/// ```
/// use jshift::{delete_index, parse_path};
///
/// let mut json = b"[10, 20, 30]".to_vec();
/// delete_index(&mut json, &[], 1).unwrap();
/// assert_eq!(json, b"[10, 30]".to_vec());
/// ```
pub fn delete_index(json: &mut Vec<u8>, path: &[PathSegment], index: usize) -> Result<(), Error> {
    let mut target_path = path.to_vec();
    target_path.push(PathSegment::Index(index));
    
    let (val_start, val_end) = find_value_offsets(json, &target_path)?;
    
    let mut prev_comma_pos = val_start;
    prev_comma_pos = scan_backwards_whitespace(json, prev_comma_pos);
    
    let delete_start;
    let delete_end;
    
    if prev_comma_pos > 0 && json[prev_comma_pos - 1] == b',' {
        delete_start = prev_comma_pos - 1;
        delete_end = val_end;
    } else {
        let mut next_comma_pos = val_end;
        next_comma_pos = skip_whitespace(json, next_comma_pos);
        if next_comma_pos < json.len() && json[next_comma_pos] == b',' {
            delete_start = val_start;
            delete_end = next_comma_pos + 1;
        } else {
            delete_start = val_start;
            delete_end = val_end;
        }
    }
    
    let delta = delete_end - delete_start;
    let old_total_len = json.len();
    
    let tail_slice = &mut json[delete_start..];
    tail_slice.rotate_left(delta);
    json.truncate(old_total_len - delta);
    
    Ok(())
}

fn scan_backwards_whitespace(json: &[u8], mut pos: usize) -> usize {
    while pos > 0 {
        match json[pos - 1] {
            b' ' | b'\t' | b'\n' | b'\r' => pos -= 1,
            _ => break,
        }
    }
    pos
}

/// Skips a JSON value (primitive, string, array, or object) starting at `pos`.
/// Returns the index immediately following the value.
fn skip_value(json: &[u8], mut pos: usize) -> Result<usize, Error> {
    pos = skip_whitespace(json, pos);
    if pos >= json.len() {
        return Err(Error::InvalidJsonSyntax { pos, msg: "Unexpected EOF" });
    }

    match json[pos] {
        b'"' => {
            let end = find_string_end(json, pos + 1)?;
            Ok(end + 1)
        }
        b'{' => {
            // Scan and balance curly braces, taking string escapes into account
            let mut depth = 1;
            pos += 1;
            while depth > 0 && pos < json.len() {
                match json[pos] {
                    b'"' => {
                        pos = find_string_end(json, pos + 1)? + 1;
                    }
                    b'{' => {
                        depth += 1;
                        pos += 1;
                    }
                    b'}' => {
                        depth -= 1;
                        pos += 1;
                    }
                    _ => pos += 1,
                }
            }
            if depth == 0 {
                Ok(pos)
            } else {
                Err(Error::InvalidJsonSyntax { pos, msg: "Unclosed object brace '}'" })
            }
        }
        b'[' => {
            // Scan and balance square brackets
            let mut depth = 1;
            pos += 1;
            while depth > 0 && pos < json.len() {
                match json[pos] {
                    b'"' => {
                        pos = find_string_end(json, pos + 1)? + 1;
                    }
                    b'[' => {
                        depth += 1;
                        pos += 1;
                    }
                    b']' => {
                        depth -= 1;
                        pos += 1;
                    }
                    _ => pos += 1,
                }
            }
            if depth == 0 {
                Ok(pos)
            } else {
                Err(Error::InvalidJsonSyntax { pos, msg: "Unclosed array bracket ']'" })
            }
        }
        _ => {
            // Primitive (number, true, false, null)
            // Stop at structural JSON characters or whitespace
            while pos < json.len() {
                match json[pos] {
                    b' ' | b'\t' | b'\n' | b'\r' | b',' | b'}' | b']' => break,
                    _ => pos += 1,
                }
            }
            Ok(pos)
        }
    }
}

/// Finds the end of a JSON string starting *after* the opening double-quote.
/// Returns the index of the closing double-quote.
fn find_string_end(json: &[u8], mut pos: usize) -> Result<usize, Error> {
    while pos < json.len() {
        // Fast scan for next quote or backslash
        if let Some(next_pos) = memchr(b'"', &json[pos..]) {
            let found_idx = pos + next_pos;
            // Check if quote is escaped by counting backslashes before it
            let mut backslashes = 0;
            let mut check_idx = found_idx as isize - 1;
            while check_idx >= 0 && json[check_idx as usize] == b'\\' {
                backslashes += 1;
                check_idx -= 1;
            }
            if backslashes % 2 == 0 {
                return Ok(found_idx); // unescaped quote
            } else {
                pos = found_idx + 1; // escaped quote, keep scanning
            }
        } else {
            return Err(Error::InvalidJsonSyntax { pos, msg: "Unclosed string literal" });
        }
    }
    Err(Error::InvalidJsonSyntax { pos, msg: "Unclosed string literal" })
}

/// Skips whitespace characters starting at `pos`.
#[inline(always)]
fn skip_whitespace(json: &[u8], mut pos: usize) -> usize {
    while pos < json.len() {
        match json[pos] {
            b' ' | b'\t' | b'\n' | b'\r' => pos += 1,
            _ => break,
        }
    }
    pos
}

/// Trait implemented by types that can be parsed directly from a raw JSON byte slice.
pub trait FromJsonSlice: Sized {
    /// Attempts to parse an instance of `Self` from the provided raw JSON byte slice.
    fn from_json_slice(slice: &[u8]) -> Option<Self>;
}

impl FromJsonSlice for String {
    fn from_json_slice(slice: &[u8]) -> Option<Self> {
        if slice.len() >= 2 && slice[0] == b'"' && slice[slice.len() - 1] == b'"' {
            std::str::from_utf8(&slice[1..slice.len() - 1]).ok().map(String::from)
        } else {
            std::str::from_utf8(slice).ok().map(String::from)
        }
    }
}

impl FromJsonSlice for bool {
    fn from_json_slice(slice: &[u8]) -> Option<Self> {
        match slice {
            b"true" => Some(true),
            b"false" => Some(false),
            _ => None,
        }
    }
}

impl<T: FromJsonSlice> FromJsonSlice for Vec<T> {
    fn from_json_slice(slice: &[u8]) -> Option<Self> {
        let mut pos = skip_whitespace(slice, 0);
        if pos >= slice.len() || slice[pos] != b'[' {
            return None;
        }
        pos += 1;
        
        let mut vec = Vec::new();
        loop {
            pos = skip_whitespace(slice, pos);
            if pos >= slice.len() {
                return None;
            }
            if slice[pos] == b']' {
                break;
            }
            let val_start = pos;
            let val_end = skip_value(slice, val_start).ok()?;
            let val_slice = &slice[val_start..val_end];
            let item = T::from_json_slice(val_slice)?;
            vec.push(item);
            
            pos = val_end;
            pos = skip_whitespace(slice, pos);
            if pos >= slice.len() {
                return None;
            }
            if slice[pos] == b',' {
                pos += 1;
            } else if slice[pos] == b']' {
                // Handled in next loop iteration
            } else {
                return None;
            }
        }
        Some(vec)
    }
}

macro_rules! impl_from_json_numeric {
    ($($t:ty),*) => {
        $(
            impl FromJsonSlice for $t {
                fn from_json_slice(slice: &[u8]) -> Option<Self> {
                    std::str::from_utf8(slice).ok()?.parse().ok()
                }
            }
        )*
    };
}

impl_from_json_numeric!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64);

/// Trait implemented by types that can be serialized directly into a JSON byte representation.
pub trait ToJsonBytes {
    /// Serializes the value into a raw JSON byte vector.
    ///
    /// String implementations produce a JSON string literal with required escapes
    /// (`"`, `\`, and control characters).
    fn to_json_bytes(&self) -> Vec<u8>;
}

/// Append a JSON string literal (including surrounding quotes) for `s` into `out`.
///
/// Escapes `"`, `\`, and ASCII control characters per RFC 8259.
pub fn write_json_string(out: &mut Vec<u8>, s: &str) {
    out.reserve(s.len() + 2);
    out.push(b'"');
    for &b in s.as_bytes() {
        match b {
            b'"' => out.extend_from_slice(br#"\""#),
            b'\\' => out.extend_from_slice(br#"\\"#),
            b'\n' => out.extend_from_slice(br#"\n"#),
            b'\r' => out.extend_from_slice(br#"\r"#),
            b'\t' => out.extend_from_slice(br#"\t"#),
            b'\x08' => out.extend_from_slice(br#"\b"#),
            b'\x0c' => out.extend_from_slice(br#"\f"#),
            c if c < 0x20 => {
                const HEX: &[u8; 16] = b"0123456789abcdef";
                out.extend_from_slice(br#"\u00"#);
                out.push(HEX[(c >> 4) as usize]);
                out.push(HEX[(c & 0xf) as usize]);
            }
            c => out.push(c),
        }
    }
    out.push(b'"');
}

/// Serialize `s` as a JSON string literal (including surrounding quotes).
pub fn escape_json_string(s: &str) -> Vec<u8> {
    let mut v = Vec::with_capacity(s.len() + 2);
    write_json_string(&mut v, s);
    v
}

impl ToJsonBytes for String {
    fn to_json_bytes(&self) -> Vec<u8> {
        escape_json_string(self)
    }
}

impl ToJsonBytes for str {
    fn to_json_bytes(&self) -> Vec<u8> {
        escape_json_string(self)
    }
}

impl ToJsonBytes for bool {
    fn to_json_bytes(&self) -> Vec<u8> {
        if *self { b"true".to_vec() } else { b"false".to_vec() }
    }
}

macro_rules! impl_to_json_numeric {
    ($($t:ty),*) => {
        $(
            impl ToJsonBytes for $t {
                fn to_json_bytes(&self) -> Vec<u8> {
                    self.to_string().into_bytes()
                }
            }
        )*
    };
}

impl_to_json_numeric!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64);

impl<T: ToJsonBytes> ToJsonBytes for Vec<T> {
    fn to_json_bytes(&self) -> Vec<u8> {
        let mut v = Vec::new();
        v.push(b'[');
        for (i, item) in self.iter().enumerate() {
            if i > 0 {
                v.push(b',');
            }
            v.extend_from_slice(&item.to_json_bytes());
        }
        v.push(b']');
        v
    }
}

impl<T: ToJsonBytes> ToJsonBytes for [T] {
    fn to_json_bytes(&self) -> Vec<u8> {
        let mut v = Vec::with_capacity(self.len() * 10 + 2);
        v.push(b'[');
        for (i, item) in self.iter().enumerate() {
            if i > 0 {
                v.push(b',');
            }
            v.extend_from_slice(&item.to_json_bytes());
        }
        v.push(b']');
        v
    }
}

#[cfg(test)]
mod tests {
    extern crate self as jshift;
    use super::*;

    #[test]
    fn test_find_simple_values() {
        let json = b"{\"a\": 123, \"b\": \"hello\", \"c\": true}";
        
        assert_eq!(find_value(json, &parse_path("a")), Ok(&b"123"[..]));
        assert_eq!(find_value(json, &parse_path("b")), Ok(&b"\"hello\""[..]));
        assert_eq!(find_value(json, &parse_path("c")), Ok(&b"true"[..]));
        assert_eq!(find_value(json, &parse_path("d")), Err(Error::PathNotFound));
    }

    #[test]
    fn test_find_nested_values() {
        let json = b"{\"metadata\": {\"version\": 1, \"author\": \"farmer\"}, \"data\": [1,2,3]}";
        
        assert_eq!(find_value(json, &parse_path("metadata.version")), Ok(&b"1"[..]));
        assert_eq!(find_value(json, &parse_path("metadata.author")), Ok(&b"\"farmer\""[..]));
        assert_eq!(find_value(json, &parse_path("data")), Ok(&b"[1,2,3]"[..]));
    }

    #[test]
    fn test_mutate_equal_size() {
        let mut json = b"{\"a\": 123, \"b\": \"hello\"}".to_vec();
        mutate_value(&mut json, &parse_path("a"), b"999").unwrap();
        assert_eq!(json, b"{\"a\": 999, \"b\": \"hello\"}");
    }

    #[test]
    fn test_mutate_smaller_size() {
        let mut json = b"{\"a\": 12345, \"b\": \"hello\"}".to_vec();
        mutate_value(&mut json, &parse_path("a"), b"9").unwrap();
        assert_eq!(json, b"{\"a\": 9, \"b\": \"hello\"}");
    }

    #[test]
    fn test_mutate_larger_size() {
        let mut json = b"{\"a\": 1, \"b\": \"hello\"}".to_vec();
        mutate_value(&mut json, &parse_path("a"), b"99999").unwrap();
        assert_eq!(json, b"{\"a\": 99999, \"b\": \"hello\"}");
    }

    #[test]
    fn test_mutate_nested() {
        let mut json = b"{\"meta\": {\"ver\": 1}, \"data\": true}".to_vec();
        mutate_value(&mut json, &parse_path("meta.ver"), b"100").unwrap();
        assert_eq!(json, b"{\"meta\": {\"ver\": 100}, \"data\": true}");
    }

    #[test]
    fn test_array_indexing() {
        let json = b"{\"data\": [{\"id\": 1}, {\"id\": 2}], \"tags\": [\"a\", \"b\"]}";
        
        assert_eq!(find_value(json, &parse_path("data[0].id")), Ok(&b"1"[..]));
        assert_eq!(find_value(json, &parse_path("data[1].id")), Ok(&b"2"[..]));
        assert_eq!(find_value(json, &parse_path("tags[1]")), Ok(&b"\"b\""[..]));
        assert_eq!(find_value(json, &parse_path("tags[2]")), Err(Error::IndexOutOfBounds { index: 2 }));
    }

    #[test]
    fn test_array_append_raw() {
        let mut json = b"{\"list\": []}".to_vec();
        append_to_array(&mut json, &parse_path("list"), b"1").unwrap();
        assert_eq!(json, b"{\"list\": [1]}");
        
        append_to_array(&mut json, &parse_path("list"), b"2").unwrap();
        assert_eq!(json, b"{\"list\": [1,2]}");
    }

    #[test]
    fn test_array_len() {
        let json = b"{\"empty\": [], \"list\": [1, 2, 3]}";
        assert_eq!(array_len(json, &parse_path("empty")), Ok(0));
        assert_eq!(array_len(json, &parse_path("list")), Ok(3));
    }

    #[test]
    fn test_upsert_object_key() {
        let mut json = b"{\"a\": 1}".to_vec();
        // Insert new key
        upsert_object_key(&mut json, &[], "b", b"2").unwrap();
        assert_eq!(json, b"{\"a\": 1,\"b\":2}");
        
        // Update existing key
        upsert_object_key(&mut json, &[], "a", b"99").unwrap();
        assert_eq!(json, b"{\"a\": 99,\"b\":2}");
    }

    #[test]
    fn test_delete_key() {
        let mut json = b"{\"a\": 1, \"b\": 2, \"c\": 3}".to_vec();
        delete_key(&mut json, &[], "b").unwrap();
        assert_eq!(json, b"{\"a\": 1, \"c\": 3}");
        
        delete_key(&mut json, &[], "a").unwrap();
        assert_eq!(json, b"{ \"c\": 3}");
        
        delete_key(&mut json, &[], "c").unwrap();
        assert_eq!(json, b"{ }");
    }

    #[test]
    fn test_delete_index() {
        let mut json = b"[10, 20, 30]".to_vec();
        delete_index(&mut json, &[], 1).unwrap();
        assert_eq!(json, b"[10, 30]");
        
        delete_index(&mut json, &[], 0).unwrap();
        assert_eq!(json, b"[ 30]");
        
        delete_index(&mut json, &[], 0).unwrap();
        assert_eq!(json, b"[ ]");
    }

    #[derive(JsonMutatorSchema)]
    struct Config {
        #[json(path = "metadata.version")]
        version: u32,
        #[json(path = "user.score")]
        score: f64,
        #[json(path = "user.name")]
        name: String,
        #[json(path = "user.tags")]
        tags: Vec<String>,
    }

    #[test]
    fn test_procedural_macro() {
        let mut json = b"{\"metadata\": {\"version\": 1}, \"user\": {\"score\": 9.5, \"name\": \"farmer\", \"tags\": [\"rust\", \"fast\"]}}".to_vec();
        
        let config = Config::read_from_json(&json).unwrap();
        assert_eq!(config.version, 1);
        assert_eq!(config.score, 9.5);
        assert_eq!(config.name, "farmer");
        assert_eq!(config.tags, vec!["rust".to_string(), "fast".to_string()]);
        
        let mut mutator = Config::mutator(&mut json);
        mutator.set_version(&2).unwrap();
        mutator.set_score(&99.9).unwrap();
        mutator.set_name("new_name").unwrap();
        mutator.append_tags("cool").unwrap();
        
        let updated = Config::read_from_json(&json).unwrap();
        assert_eq!(updated.version, 2);
        assert_eq!(updated.score, 99.9);
        assert_eq!(updated.name, "new_name");
        assert_eq!(updated.tags, vec!["rust".to_string(), "fast".to_string(), "cool".to_string()]);
    }

    #[test]
    fn test_escape_json_string() {
        assert_eq!(escape_json_string("plain"), br#""plain""#);
        assert_eq!(escape_json_string(r#"say "hi""#), br#""say \"hi\"""#);
        assert_eq!(escape_json_string("a\\b"), br#""a\\b""#);
        assert_eq!(escape_json_string("a\nb\tc"), br#""a\nb\tc""#);
        assert_eq!(escape_json_string("\u{0001}"), br#""\u0001""#);
    }

    #[test]
    fn test_to_json_bytes_escapes_strings() {
        assert_eq!(r#"he"llo"#.to_json_bytes(), br#""he\"llo""#);
        assert_eq!(String::from("x\ny").to_json_bytes(), br#""x\ny""#);
    }

    #[test]
    fn test_upsert_escapes_keys() {
        let mut json = b"{}".to_vec();
        upsert_object_key(&mut json, &[], r#"a"b"#, b"1").unwrap();
        assert_eq!(json, br#"{"a\"b":1}"#);

        // Path matching compares raw key bytes inside quotes (escaped form).
        assert_eq!(find_value(&json, &parse_path(r#"a\"b"#)), Ok(&b"1"[..]));
    }

    #[test]
    fn test_mutate_via_to_json_bytes_keeps_valid_json() {
        let mut json = br#"{"msg":"old"}"#.to_vec();
        let bytes = r#"say "hi""#.to_json_bytes();
        mutate_value(&mut json, &parse_path("msg"), &bytes).unwrap();
        assert_eq!(json, br#"{"msg":"say \"hi\""}"#);
        assert_eq!(find_value(&json, &parse_path("msg")), Ok(&br#""say \"hi\"""#[..]));
    }
}