| Crates.io | json-fixer |
| lib.rs | json-fixer |
| version | 0.1.0 |
| created_at | 2025-01-22 08:53:44.931655+00 |
| updated_at | 2025-01-22 08:53:44.931655+00 |
| description | A robust library for fixing and formatting malformed JSON with support for type conversion |
| homepage | https://github.com/hashdiese/json-fixer |
| repository | https://github.com/hashdiese/json-fixer |
| max_upload_size | |
| id | 1526437 |
| size | 87,422 |
A robust Rust library for parsing and fixing malformed JSON. This library helps you handle common JSON formatting issues while maintaining the original data structure.
Add this to your Cargo.toml:
[dependencies]
json-fixer = "0.1.0" # Basic functionality
# Or with serde support:
json-fixer = { version = "0.1.0", features = ["serde"] }
use json_fixer::JsonFixer;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Example of fixing malformed JSON
let input = r#"{
name: 'John'
age: 30
hobbies: ['reading' 'coding']
}"#;
// Using default configuration
let fixed_json = JsonFixer::fix(input)?;
println!("Fixed JSON: {}", fixed_json);
// Result: {"name":"John","age":30,"hobbies":["reading","coding"]}
Ok(())
}
use json_fixer::{JsonFixer, JsonFixerConfig};
// Pretty printing
let pretty_json = JsonFixer::fix_pretty(input)?;
// Result:
// {
// "name": "John",
// "age": 30,
// "hobbies": [
// "reading",
// "coding"
// ]
// }
// Add spaces between keys and values
let spaced_json = JsonFixer::fix_with_space_between(input)?;
// Result: { "name": "John", "age": 30, "hobbies": ["reading", "coding"] }
// Custom configuration
let mut config = JsonFixerConfig::default();
config.sort_keys = true;
config.indent_size = 2;
let custom_json = JsonFixer::fix_with_config(input, config)?;
When enabled with the serde feature, you can convert between JSON and Rust types:
use json_fixer::JsonFixer;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug)]
struct Person {
name: String,
age: u32,
hobbies: Vec<String>,
}
// Parse malformed JSON directly into a type
let input = r#"{ name: 'John', age: 30, hobbies: ['reading' 'coding'] }"#;
let person: Person = JsonFixer::from_fixed(input, None)?;
// Convert a type to properly formatted JSON
let json_string = JsonFixer::to_json(&person, None)?;
// Parse valid JSON into a type
let valid_json = r#"{"name":"John","age":30,"hobbies":["reading","coding"]}"#;
let person: Person = JsonFixer::from_str(valid_json)?;
The library provides detailed error information through the JsonFixerError enum:
pub enum JsonFixerError {
Syntax(SyntaxError),
Format(JsonFormatError),
IO(std::fmt::Error),
#[cfg(feature = "serde")]
SerdeError(String),
}
pub enum SyntaxError {
UnexpectedCharacter(char, Position),
UnmatchedQuotes(Position),
UnexpectedEndOfInput(Position),
MissingComma(Position),
InvalidNumber(String, Position),
UnexpectedToken(String, Position),
}
pub enum JsonFormatError {
LineTooLong {
line: usize,
length: usize,
max: usize,
},
InvalidIndentation {
line: usize,
},
}
// In arrays
let input = r#"[1 2 3 4]"#;
let fixed = JsonFixer::fix(input)?;
// Result: [1,2,3,4]
// In objects
let input = r#"{
name: "Hicham-dine"
age: 36
job: "programmer"
}"#;
let fixed = JsonFixer::fix(input)?;
// Result: {"name":"Hicham-dine","age":36,"job":"programmer"}
let input = r#"{
c: 3,
a: 1,
b: 2
}"#;
let mut config = JsonFixerConfig::default();
config.sort_keys = true;
let fixed = JsonFixer::fix_with_config(input, config)?;
// Result: {"a":1,"b":2,"c":3}
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
Full documentation is available at docs.rs.