Crates.io | json-repair |
lib.rs | json-repair |
version | 0.4.0 |
source | src |
created_at | 2024-12-04 00:24:39.768876 |
updated_at | 2024-12-05 21:38:41.781944 |
description | A well tested crate for repairing malformed JSON strings and repairing/parsing them into valid JSON values |
homepage | |
repository | https://github.com/klebs6/klebs-general |
max_upload_size | |
id | 1470838 |
size | 193,698 |
A Rust library for repairing malformed JSON strings and parsing them into valid JSON values.
json-repair
is a Rust crate that provides utilities to repair and parse malformed JSON strings. It handles common JSON syntax errors such as:
This library is particularly useful when dealing with JSON data from unreliable sources where the JSON may not be well-formed.
Add json-repair
to your Cargo.toml
dependencies:
[dependencies]
json-repair = "0.1.0"
Then, include it in your Rust code:
use json_repair::repair_json_string;
Here's how you can use json-repair
to repair a malformed JSON string:
use json_repair::repair_json_string;
use serde_json::Value;
fn main() {
let malformed_json = r#"{
'key1': 'value1',
'key2': "value2",
"key3": 'value3',
"text": "Don't stop believing",
'another_text': 'It\'s a kind of magic',
"nested": {
'inner_key': 'inner_value'
}
}"#;
match repair_json_string(malformed_json) {
Ok(json_value) => {
println!("Repaired JSON: {}", json_value);
},
Err(e) => {
eprintln!("Failed to repair JSON: {}", e);
}
}
}
Output:
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"text": "Don't stop believing",
"another_text": "It's a kind of magic",
"nested": {
"inner_key": "inner_value"
}
}
repair_json_string
fn repair_json_string(input: &str) -> Result<Value, JsonRepairError>
Attempts to repair the given JSON string and parse it into a serde_json::Value
.
input
: The malformed JSON string to repair.Ok(Value)
: The repaired JSON parsed into a serde_json::Value
.Err(JsonRepairError)
: An error indicating why the repair failed.The JsonRepairError
enum provides detailed error variants for different failure cases:
FailedToParseRepairedJson
: The repaired string could not be parsed into JSON.Unrepairable(String)
: The input string is unrepairable, with details provided.AllAttemptedRepairsFailed
: All repair attempts have failed.CouldNotConvertTheOutputOfDuplicateQuoteRemovalToJson
: Failed to parse JSON after attempting to remove duplicate quotes.SerdeParseError
: An error from serde_json
during parsing.The library attempts the following repairs:
true
or false
literals.use json_repair::repair_json_string;
use serde_json::json;
let malformed_json = r#"{
"key": [
"value1",
"value2"
"value3", // Missing comma
'value4', // Single quotes instead of double quotes
"value5"
]
}"#;
let repaired = repair_json_string(malformed_json).unwrap();
assert_eq!(repaired, json!({
"key": [
"value1",
"value2",
"value3",
"value4",
"value5"
]
}));
use json_repair::repair_json_string;
use serde_json::json;
let truncated_json = r#"{
"list": [1, 2, 3
"#; // Missing closing brackets
let repaired = repair_json_string(truncated_json).unwrap();
assert_eq!(repaired, json!({
"list": [1, 2, 3]
}));
use json_repair::repair_json_string;
use serde_json::json;
let malformed_json = r#"{
"data": [
{"id": 1, "value": "A"},
{"id": 2, "value": "B"],
"status": "ok"
}"#; // Mismatched brackets
let repaired = repair_json_string(malformed_json).unwrap();
assert_eq!(repaired, json!({
"data": [
{"id": 1, "value": "A"},
{"id": 2, "value": "B"}
],
"status": "ok"
}));
While json-repair
can handle many common JSON syntax errors, it cannot repair all possible malformed JSON inputs. If the input is too corrupted or ambiguous, the repair may fail, and an appropriate error will be returned.
Contributions are welcome! Please open an issue or submit a pull request if you have suggestions or improvements.
This project is licensed under the MIT License.
Note: This crate relies on the serde_json
library for JSON parsing and the json5
library for initial parsing attempts. It also uses the regex
crate for pattern matching during the repair process.