| Crates.io | jsonfixer |
| lib.rs | jsonfixer |
| version | 0.1.0 |
| created_at | 2025-08-21 06:22:08.488447+00 |
| updated_at | 2025-08-21 06:22:08.488447+00 |
| description | A Rust library to repair broken JSON strings, commonly used to parse the output of LLMs |
| homepage | |
| repository | https://github.com/July-Tea/jsonfixer |
| max_upload_size | |
| id | 1804369 |
| size | 78,540 |
一个用于修复损坏的 JSON 字符串的 Rust 库。
A Rust library for fixing malformed JSON strings.
serde_json::Value 对象 / Flexible Output: Return repaired JSON strings or parsed serde_json::Value objects在 Cargo.toml 中添加 / Add to Cargo.toml:
[dependencies]
jsonfixer = "0.1.0"
use jsonfixer::{repair_json, loads, JsonRepairOptions};
// 修复 JSON 字符串并返回字符串 / Repair JSON string and return string
let options = JsonRepairOptions::default();
let repaired = repair_json(r#"{"name": John, "age": 30}"#, options).unwrap();
// 返回: "{\"name\": \"John\", \"age\": 30}" / Returns: "{\"name\": \"John\", \"age\": 30}"
// 修复 JSON 并返回解析后的 Value / Repair JSON and return parsed Value
let parsed = loads(r#"{"name": John, "age": 30}"#, options).unwrap();
// 返回 serde_json::Value / Returns serde_json::Value
// 从文件修复 JSON / Repair JSON from file
let from_file = jsonfixer::from_file("broken.json", options).unwrap();
use jsonfixer::JsonRepairOptions;
let options = JsonRepairOptions {
return_objects: true, // 返回解析后的对象而非字符串 / Return parsed objects instead of strings
skip_json_loads: false, // 跳过标准 JSON 解析器的首次尝试 / Skip first attempt with standard JSON parser
logging: true, // 启用修复日志 / Enable repair logs
ensure_ascii: false, // 转义非 ASCII 字符 / Escape non-ASCII characters
stream_stable: false, // 保持流式 JSON 的修复结果稳定 / Keep streaming JSON repairs stable
chunk_length: 1024, // 文件处理的块长度 / Chunk length for file processing
indent: Some(2), // 输出格式的缩进 / Indentation for output formatting
..Default::default()
};
修复前 / Before repair:
{
name: John,
age: 30,
hobbies: ["reading", "swimming",]
city: New York
}
修复后 / After repair:
{
"name": "John",
"age": 30,
"hobbies": [
"reading",
"swimming"
],
"city": "New York"
}
本项目采用 MIT 许可证 - 详情请查看 LICENSE 文件。 This project is licensed under the MIT License - see the LICENSE file for details.
灵感来源于 Py 版本的 json_repair 项目。 Inspired by the Python version of json_repair project.