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
//! Cleaning strategies that normalize and remove garbage from extracted JSON.
use crate::{error::Result, parser::Candidate};
/// Trait for strategies that clean/normalize extracted JSON candidates.
///
/// Cleaners run between extraction and fixing to remove garbage characters,
/// normalize whitespace, and perform other preprocessing.
pub trait Cleaner: Send + Sync + std::fmt::Debug {
/// Returns the name of this cleaner for debugging.
fn name(&self) -> &'static str;
/// Cleans the candidate, returning a new candidate if changes were made.
///
/// Returns None if no cleaning was needed.
fn clean(&self, candidate: &Candidate) -> Result<Option<Candidate>>;
}
/// Garbage cleaner that removes common invalid patterns.
///
/// Removes:
/// - Multiple consecutive commas: `,,,,` → `,`
/// - Trailing commas before closing braces/brackets (if not caught by fixer)
/// - Extra whitespace in structural positions
/// - Invalid characters between JSON elements
#[derive(Debug, Clone, Default)]
pub struct GarbageCleaner;
impl GarbageCleaner {
/// Creates a new garbage cleaner.
pub fn new() -> Self {
Self
}
/// Removes multiple consecutive commas.
fn clean_multiple_commas(&self, input: &str) -> String {
let mut result = String::with_capacity(input.len());
let mut last_was_comma = false;
let mut in_string = false;
let mut escape_next = false;
for ch in input.chars() {
if escape_next {
result.push(ch);
escape_next = false;
continue;
}
match ch {
'\\' if in_string => {
escape_next = true;
result.push(ch);
}
'"' => {
in_string = !in_string;
last_was_comma = false;
result.push(ch);
}
',' if !in_string => {
if !last_was_comma {
result.push(ch);
last_was_comma = true;
}
// Skip additional commas
}
_ => {
if !ch.is_whitespace() {
last_was_comma = false;
}
result.push(ch);
}
}
}
result
}
/// Removes unnecessary backslashes before quotes.
///
/// LLMs sometimes output JSON with literal backslashes:
/// `{\"name\": \"Alice\"}` → `{"name": "Alice"}`
///
/// This is different from double-escaped JSON (which wraps in quotes).
/// We replace `\"` with `"` unless the backslash itself is escaped.
///
/// IMPORTANT: We only remove backslashes that appear OUTSIDE of JSON string values.
/// Inside string values, `\"` is valid JSON and should be preserved.
pub fn fix_unnecessary_backslashes(&self, input: &str) -> String {
let mut result = String::with_capacity(input.len());
let chars: Vec<char> = input.chars().collect();
let mut i = 0;
let mut in_string = false;
let mut string_escape_next = false;
while i < chars.len() {
let ch = chars[i];
// Track if we're inside a JSON string value
if ch == '"' && !string_escape_next {
in_string = !in_string;
result.push(ch);
i += 1;
string_escape_next = false;
continue;
}
if in_string {
// Inside a string value - preserve everything as-is, including `\"`
if string_escape_next {
string_escape_next = false;
} else if ch == '\\' {
string_escape_next = true;
}
result.push(ch);
i += 1;
continue;
}
// Outside a string value - check for unnecessary backslashes
if ch == '\\' && i + 1 < chars.len() {
// Check if this backslash is itself escaped
let num_preceding_backslashes =
(0..i).rev().take_while(|&j| chars[j] == '\\').count();
// If odd number of preceding backslashes, this one is escaped
let is_escaped = num_preceding_backslashes % 2 == 1;
if !is_escaped && chars[i + 1] == '"' {
// Unnecessary backslash before quote OUTSIDE string - skip it, keep quote
result.push('"');
i += 2;
continue;
}
}
result.push(ch);
i += 1;
}
result
}
/// Detects and extracts content from excessively nested structures.
///
/// If the input has more than `max_depth` levels of nesting (e.g., `{{{{...}}}}`),
/// this extracts the innermost valid JSON object or array.
///
/// This prevents stack overflow errors from deeply nested structures.
pub fn extract_from_deep_nesting(&self, input: &str, max_depth: usize) -> Option<String> {
let trimmed = input.trim();
// Quick check: does it even look nested?
if !trimmed.starts_with('{') && !trimmed.starts_with('[') {
return None;
}
// Count the depth of opening braces/brackets
let mut max_brace_depth: usize = 0;
let mut max_bracket_depth: usize = 0;
let mut current_brace_depth: usize = 0;
let mut current_bracket_depth: usize = 0;
let mut in_string = false;
let mut escape_next = false;
for ch in trimmed.chars() {
if escape_next {
escape_next = false;
continue;
}
match ch {
'\\' if in_string => escape_next = true,
'"' => in_string = !in_string,
'{' if !in_string => {
current_brace_depth += 1;
max_brace_depth = max_brace_depth.max(current_brace_depth);
}
'}' if !in_string => {
current_brace_depth = current_brace_depth.saturating_sub(1);
}
'[' if !in_string => {
current_bracket_depth += 1;
max_bracket_depth = max_bracket_depth.max(current_bracket_depth);
}
']' if !in_string => {
current_bracket_depth = current_bracket_depth.saturating_sub(1);
}
_ => {}
}
}
// Check if either depth exceeds the limit
if max_brace_depth <= max_depth && max_bracket_depth <= max_depth {
return None; // Not too deep, no extraction needed
}
// Extract the innermost actual JSON object/array
// Strategy: Find the first position where we have actual content (not just delimiters)
let chars: Vec<char> = trimmed.chars().collect();
// Skip all opening delimiters to find start of actual content
let mut start = 0;
for (i, &ch) in chars.iter().enumerate() {
match ch {
'{' | '[' => {
// Keep looking
start = i + 1;
}
c if c.is_whitespace() => {
// Skip whitespace
}
_ => {
// Found actual content, backtrack to last delimiter
if i > 0 {
start = i - 1;
// Find the actual opening delimiter before any whitespace
while start > 0 && chars[start].is_whitespace() {
start -= 1;
}
}
break;
}
}
}
// Skip all closing delimiters from the end to find end of actual content
let mut end = chars.len();
for (i, &ch) in chars.iter().enumerate().rev() {
match ch {
'}' | ']' => {
// Keep looking
end = i;
}
c if c.is_whitespace() => {
// Skip whitespace
}
_ => {
// Found actual content, advance to next delimiter
if i + 1 < chars.len() {
end = i + 1;
// Find the actual closing delimiter after any whitespace
while end < chars.len() && chars[end].is_whitespace() {
end += 1;
}
if end < chars.len() {
end += 1; // Include the closing delimiter
}
}
break;
}
}
}
if start < end && end <= chars.len() {
let extracted: String = chars[start..end].iter().collect();
let trimmed_extracted = extracted.trim();
// Validate it looks like JSON
if (trimmed_extracted.starts_with('{') && trimmed_extracted.ends_with('}'))
|| (trimmed_extracted.starts_with('[') && trimmed_extracted.ends_with(']'))
{
return Some(extracted);
}
}
None
}
/// Removes invisible characters that can break JSON parsing.
///
/// Removes:
/// - Zero-width space (U+200B)
/// - Zero-width non-joiner (U+200C)
/// - Zero-width joiner (U+200D)
/// - Byte Order Mark (U+FEFF)
/// - Other zero-width and control characters
pub fn remove_invisible_chars(&self, input: &str) -> String {
input.replace(
[
'\u{200B}', '\u{200C}', '\u{200D}', '\u{FEFF}', '\u{200E}', '\u{200F}', '\u{202A}',
'\u{202B}', '\u{202C}', '\u{202D}', '\u{202E}',
],
"",
) // Right-to-left override
}
/// Handles double-escaped JSON (JSON serialized as a string).
///
/// Converts: `"{\"name\": \"Alice\"}"` → `{"name": "Alice"}`
/// Converts: `"[{\"id\": 1}]"` → `[{"id": 1}]`
fn fix_double_escaped(&self, input: &str) -> String {
let trimmed = input.trim();
// Check if this looks like double-escaped JSON
// Pattern: starts with " followed by { or [, ends with } or ] followed by "
if trimmed.len() < 4 {
return input.to_string();
}
let starts_with_quote_brace = trimmed.starts_with("\"{") || trimmed.starts_with("\"[");
let ends_with_brace_quote = trimmed.ends_with("}\"") || trimmed.ends_with("]\"");
if !starts_with_quote_brace || !ends_with_brace_quote {
return input.to_string();
}
// Try to parse as a JSON string first
match serde_json::from_str::<String>(trimmed) {
Ok(unescaped) => {
// Verify the unescaped version is valid JSON
if serde_json::from_str::<serde_json::Value>(&unescaped).is_ok() {
unescaped
} else {
input.to_string()
}
}
Err(_) => input.to_string(),
}
}
/// Normalizes excessive whitespace.
fn normalize_whitespace(&self, input: &str) -> String {
let mut result = String::with_capacity(input.len());
let mut in_string = false;
let mut escape_next = false;
let mut last_was_space = false;
for ch in input.chars() {
if escape_next {
result.push(ch);
escape_next = false;
continue;
}
match ch {
'\\' if in_string => {
escape_next = true;
result.push(ch);
}
'"' => {
in_string = !in_string;
last_was_space = false;
result.push(ch);
}
_ if in_string => {
last_was_space = false;
result.push(ch);
}
_ if ch.is_whitespace() && !in_string => {
if !last_was_space {
result.push(' '); // Normalize all whitespace to space
last_was_space = true;
}
}
_ => {
last_was_space = false;
result.push(ch);
}
}
}
result.trim().to_string()
}
/// Normalizes field names to snake_case for fuzzy matching.
///
/// DISABLED: Field normalization is now handled by the struct deserializer's
/// fuzzy field matching (FieldMatcher). This preserves HashMap keys correctly
/// while still allowing flexible struct field matching.
///
/// Only normalizes if the input is valid JSON. If not valid JSON,
/// returns the input unchanged (fixes will handle it later).
pub fn normalize_field_names(&self, input: &str) -> String {
// DISABLED: Return input unchanged to preserve HashMap keys
// Field matching is handled by struct deserializer's FieldMatcher
input.to_string()
// Original implementation (disabled):
// Try to parse as JSON first
// let value: serde_json::Value = match serde_json::from_str(input) {
// Ok(v) => v,
// Err(_) => return input.to_string(), // Not valid JSON, return as-is
// };
//
// // Recursively normalize all field names
// let (normalized_value, modified) = Self::normalize_value(value);
//
// // Only re-serialize if field names were actually changed
// if modified {
// match serde_json::to_string(&normalized_value) {
// Ok(result) => result,
// Err(_) => input.to_string(),
// }
// } else {
// input.to_string()
// }
}
}
impl Cleaner for GarbageCleaner {
fn name(&self) -> &'static str {
"garbage"
}
fn clean(&self, candidate: &Candidate) -> Result<Option<Candidate>> {
// OPTIMIZATION: Apply cleaning steps sequentially and track if any changes were made
// This avoids unnecessary string allocations if no cleaning is needed
let mut content = &candidate.content;
let mut owned_content: Option<String> = None;
// Step -2: Remove invisible characters (very first!)
let invisible_cleaned = self.remove_invisible_chars(content);
if invisible_cleaned != *content {
owned_content = Some(invisible_cleaned);
content = owned_content.as_ref().unwrap();
}
// Step -1: Fix unnecessary backslashes
let backslash_cleaned = self.fix_unnecessary_backslashes(content);
if backslash_cleaned != *content {
owned_content = Some(backslash_cleaned);
content = owned_content.as_ref().unwrap();
}
// Step 0: Handle double-escaped JSON (must be early!)
let double_escape_cleaned = self.fix_double_escaped(content);
if double_escape_cleaned != *content {
owned_content = Some(double_escape_cleaned);
content = owned_content.as_ref().unwrap();
}
// Step 1: Clean multiple commas
let comma_cleaned = self.clean_multiple_commas(content);
if comma_cleaned != *content {
owned_content = Some(comma_cleaned);
content = owned_content.as_ref().unwrap();
}
// Step 2: Normalize whitespace
let ws_cleaned = self.normalize_whitespace(content);
if ws_cleaned != *content {
owned_content = Some(ws_cleaned);
content = owned_content.as_ref().unwrap();
}
// Step 3: Normalize field names (only if valid JSON)
let field_cleaned = self.normalize_field_names(content);
if field_cleaned != *content {
owned_content = Some(field_cleaned);
}
// OPTIMIZATION: Only clone the candidate if we made changes
if let Some(cleaned_content) = owned_content {
let mut cleaned = candidate.clone();
cleaned.content = cleaned_content;
Ok(Some(cleaned))
} else {
// No changes made, return None to indicate original should be used
Ok(None)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::CandidateSource;
#[test]
fn test_clean_multiple_commas() {
let cleaner = GarbageCleaner::new();
let input = r#"{"name": "Alice",,,,, "age": 30}"#;
let expected = r#"{"name": "Alice", "age": 30}"#;
let result = cleaner.clean_multiple_commas(input);
assert_eq!(result, expected);
}
#[test]
fn test_clean_multiple_commas_in_string() {
let cleaner = GarbageCleaner::new();
let input = r#"{"text": "Hello,,,,world"}"#;
let result = cleaner.clean_multiple_commas(input);
// Commas inside strings should be preserved
assert_eq!(result, input);
}
#[test]
fn test_normalize_whitespace() {
let cleaner = GarbageCleaner::new();
let input = " { \"name\" : \"Alice\" , \"age\" : 30 } ";
let expected = r#"{ "name" : "Alice" , "age" : 30 }"#;
let result = cleaner.normalize_whitespace(input);
assert_eq!(result, expected);
}
#[test]
fn test_cleaner_trait() {
let cleaner = GarbageCleaner::new();
let candidate = Candidate {
content: r#"{"name": "Alice",,,, "age": 30}"#.to_string(),
source: CandidateSource::Direct,
};
let result = cleaner.clean(&candidate).unwrap();
assert!(result.is_some());
let cleaned = result.unwrap();
assert_eq!(cleaned.content, r#"{"name": "Alice", "age": 30}"#);
}
#[test]
fn test_no_cleaning_needed() {
let cleaner = GarbageCleaner::new();
let candidate = Candidate {
content: r#"{"name": "Alice", "age": 30}"#.to_string(),
source: CandidateSource::Direct,
};
let result = cleaner.clean(&candidate).unwrap();
assert!(result.is_none()); // No changes needed
}
#[test]
fn test_fix_unnecessary_backslashes() {
let cleaner = GarbageCleaner::new();
let input = r#"{\"name\": \"Alice\", \"age\": 30}"#;
println!("Input: {}", input);
println!("Input contains backslash: {}", input.contains('\\'));
let result = cleaner.fix_unnecessary_backslashes(input);
println!("Result: {}", result);
assert_eq!(result, r#"{"name": "Alice", "age": 30}"#);
// Verify result is valid JSON
assert!(serde_json::from_str::<serde_json::Value>(&result).is_ok());
}
#[test]
fn test_extract_from_deep_nesting() {
let cleaner = GarbageCleaner::new();
// Test with 10 levels of nesting (exceeds max_depth of 3)
let input = format!(
"{}{{\"name\": \"Alice\", \"age\": 30}}{}",
"{".repeat(10),
"}".repeat(10)
);
println!("Input (first 50 chars): {}", &input[..50.min(input.len())]);
let result = cleaner.extract_from_deep_nesting(&input, 3);
println!("Result: {:?}", result);
assert!(result.is_some());
let extracted = result.unwrap();
println!("Extracted: {}", extracted);
// Should extract the inner content
assert!(extracted.contains("\"name\""));
assert!(extracted.contains("Alice"));
// Verify it's valid JSON
assert!(serde_json::from_str::<serde_json::Value>(&extracted).is_ok());
}
#[test]
fn test_extract_from_deep_nesting_not_too_deep() {
let cleaner = GarbageCleaner::new();
// Test with 2 levels (does not exceed max_depth of 3)
let input = r#"{{"name": "Alice"}}"#;
let result = cleaner.extract_from_deep_nesting(input, 3);
// Should return None (not too deep)
assert!(result.is_none());
}
#[test]
fn test_extract_from_very_deep_nesting() {
let cleaner = GarbageCleaner::new();
// Test with 100 levels (like the brutal_reality test)
let input = format!(
"{}{{\"name\": \"Alice\", \"age\": 30}}{}",
"{".repeat(100),
"}".repeat(100)
);
let result = cleaner.extract_from_deep_nesting(&input, 50);
assert!(result.is_some());
let extracted = result.unwrap();
// Should extract valid JSON
let parsed: serde_json::Value = serde_json::from_str(&extracted).unwrap();
assert_eq!(parsed["name"], "Alice");
assert_eq!(parsed["age"], 30);
}
}