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
pub use ;
pub use ;
pub use RepairLogEntry;
pub use StreamRepairer;
use Write;
// ============================================================================
// Core API - Repair to String
// ============================================================================
/// Repair a potentially invalid JSON string into a valid JSON string.
///
/// This function focuses on common issues like unquoted keys/strings,
/// missing commas/colons, comments, and unclosed brackets/braces.
///
/// # Examples
///
/// ```
/// use jsonrepair::{repair_to_string, Options};
///
/// let broken = r#"{name: 'John', age: 30,}"#;
/// let repaired = repair_to_string(broken, &Options::default())?;
/// assert_eq!(repaired, r#"{"name":"John","age":30}"#);
/// # Ok::<(), jsonrepair::RepairError>(())
/// ```
/// Alias for [`repair_to_string`] - repairs broken JSON and returns a valid JSON string.
///
/// This naming is more intuitive and matches the Python `json_repair` library.
///
/// # Examples
///
/// ```
/// use jsonrepair::{repair_json, Options};
///
/// let broken = r#"{name: 'John', age: 30,}"#;
/// let repaired = repair_json(broken, &Options::default())?;
/// assert_eq!(repaired, r#"{"name":"John","age":30}"#);
/// # Ok::<(), jsonrepair::RepairError>(())
/// ```
// ============================================================================
// Writer-based API
// ============================================================================
/// Repair a potentially invalid JSON string and write the result into an `io::Write`.
///
/// This avoids an extra copy of the final string when the caller intends to stream to a sink.
///
/// # Examples
///
/// ```
/// use jsonrepair::{repair_to_writer, Options};
///
/// let broken = r#"{name: 'John'}"#;
/// let mut output = Vec::new();
/// repair_to_writer(broken, &Options::default(), &mut output)?;
/// assert_eq!(output, br#"{"name":"John"}"#);
/// # Ok::<(), jsonrepair::RepairError>(())
/// ```
/// Repair a potentially invalid JSON string and stream the output into a writer while parsing.
///
/// This reduces peak memory usage for very large inputs by flushing at semantic boundaries.
///
/// # Examples
///
/// ```
/// use jsonrepair::{repair_to_writer_streaming, Options};
///
/// let broken = r#"{a:1, items: [1, 2, 3]}"#;
/// let mut output = Vec::new();
/// repair_to_writer_streaming(broken, &Options::default(), &mut output)?;
/// assert!(output.len() > 0);
/// # Ok::<(), jsonrepair::RepairError>(())
/// ```
// ============================================================================
// Streaming Chunks API
// ============================================================================
/// Repair a sequence of UTF-8 chunks using the streaming engine and collect into a String.
///
/// If `opts.stream_ndjson_aggregate` is true, returns a single JSON array;
/// otherwise concatenates outputs.
///
/// # Examples
///
/// ```
/// use jsonrepair::{repair_chunks_to_string, Options};
///
/// let chunks = vec!["{a:", "1", "}"];
/// let repaired = repair_chunks_to_string(chunks, &Options::default())?;
/// assert_eq!(repaired, r#"{"a":1}"#);
/// # Ok::<(), jsonrepair::RepairError>(())
/// ```
/// Repair a sequence of UTF-8 chunks using the streaming engine and write into `writer`.
///
/// # Examples
///
/// ```
/// use jsonrepair::{repair_chunks_to_writer, Options};
///
/// let chunks = vec!["{a:", "1", "}"];
/// let mut output = Vec::new();
/// repair_chunks_to_writer(chunks, &Options::default(), &mut output)?;
/// assert_eq!(output, br#"{"a":1}"#);
/// # Ok::<(), jsonrepair::RepairError>(())
/// ```
// ============================================================================
// Parse to Value API (requires serde feature)
// ============================================================================
/// Repair and then parse into `serde_json::Value`.
///
/// This is a convenience function that combines repair and parsing.
///
/// # Examples
///
/// ```
/// use jsonrepair::{repair_to_value, Options};
///
/// let broken = r#"{name: 'John', age: 30}"#;
/// let value = repair_to_value(broken, &Options::default())?;
/// assert_eq!(value["name"], "John");
/// assert_eq!(value["age"], 30);
/// # Ok::<(), jsonrepair::RepairError>(())
/// ```
/// Alias for [`repair_to_value`] - repairs broken JSON and parses it into a `serde_json::Value`.
///
/// This naming matches the Python `json.loads()` and `json_repair.loads()` convention.
///
/// # Examples
///
/// ```
/// use jsonrepair::{loads, Options};
///
/// let broken = r#"{name: 'John', age: 30}"#;
/// let value = loads(broken, &Options::default())?;
/// assert_eq!(value["name"], "John");
/// assert_eq!(value["age"], 30);
/// # Ok::<(), jsonrepair::RepairError>(())
/// ```
// ============================================================================
// File and Reader API (requires serde feature)
// ============================================================================
/// Repair and parse JSON from a reader (e.g., file, network stream).
///
/// This is equivalent to reading all content from the reader and calling [`loads`].
///
/// # Examples
///
/// ```no_run
/// use jsonrepair::{load, Options};
/// use std::fs::File;
///
/// let file = File::open("broken.json")?;
/// let value = load(file, &Options::default())?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
/// Repair and parse JSON from a file path.
///
/// This is a convenience wrapper around [`load`] that opens the file for you.
///
/// # Examples
///
/// ```no_run
/// use jsonrepair::{from_file, Options};
///
/// let value = from_file("broken.json", &Options::default())?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
// ============================================================================
// Logging API
// ============================================================================
/// Repair a potentially invalid JSON string and return both the string result and a repair log.
///
/// This is useful for debugging or understanding what repairs were made.
///
/// # Examples
///
/// ```
/// use jsonrepair::{repair_to_string_with_log, Options};
///
/// let mut opts = Options::default();
/// opts.log_context_window = 12;
///
/// let (repaired, log) = repair_to_string_with_log("[1, 2 /*c*/, 3]", &opts)?;
/// assert_eq!(repaired, "[1,2,3]");
/// // Note: when built without the `logging` feature, `log` may be empty.
/// // With `logging` enabled, `log` will contain entries describing the fixes.
/// # Ok::<(), jsonrepair::RepairError>(())
/// ```