pub struct JsonFixer;
Expand description
A utility for parsing and fixing malformed JSON input.
This struct provides static methods to handle various JSON formatting and parsing tasks:
- Fix common JSON syntax errors
- Apply different formatting styles
- Convert between JSON and Rust types (with serde feature)
§Features
- Fix malformed JSON with missing quotes, commas, and brackets
- Multiple formatting options including pretty printing and key sorting
- Serde integration for type conversion (optional)
§Examples
Basic JSON fixing:
use json_fixer::JsonFixer;
let input = r#"{ name: "John", age: 30, }"#; // Note: unquoted keys and trailing comma
let result = JsonFixer::fix(input).unwrap();
assert_eq!(result, r#"{"name":"John","age":30}"#);
Pretty printing:
use json_fixer::JsonFixer;
let input = r#"{name:"John",age:30}"#;
let result = JsonFixer::fix_pretty(input).unwrap();
// Result:
// {
// "name": "John",
// "age": 30
// }
Implementations§
Source§impl JsonFixer
impl JsonFixer
Sourcepub fn fix_with_config(
input: &str,
config: JsonFixerConfig,
) -> Result<String, JsonFixerError>
pub fn fix_with_config( input: &str, config: JsonFixerConfig, ) -> Result<String, JsonFixerError>
Fixes JSON input using custom configuration options.
This method allows full control over the fixing and formatting process through the provided configuration.
§Arguments
input
- The JSON string to fixconfig
- Configuration options for fixing and formatting
§Returns
Ok(String)
- The fixed JSON stringErr(JsonFixerError)
- If the input is too malformed to be fixed
§Examples
use json_fixer::{JsonFixer, JsonFixerConfig};
let input = r#"{
c: 3,
a: 1,
b: 2
}"#;
let mut config = JsonFixerConfig::default();
config.sort_keys = true;
config.beautify = true;
let result = JsonFixer::fix_with_config(input, config).unwrap();
Sourcepub fn fix(input: &str) -> Result<String, JsonFixerError>
pub fn fix(input: &str) -> Result<String, JsonFixerError>
Fixes malformed JSON using default configuration.
This method attempts to fix common JSON syntax errors while maintaining a compact output format.
§Arguments
input
- The JSON string to fix
§Returns
Ok(String)
- The fixed JSON stringErr(JsonFixerError)
- If the input is too malformed to be fixed
§Examples
use json_fixer::JsonFixer;
let input = r#"{ name: 'John', age: 30 hobbies: ['reading' 'coding'] }"#;
let result = JsonFixer::fix(input).unwrap();
assert_eq!(result, r#"{"name":"John","age":30,"hobbies":["reading","coding"]}"#);
Sourcepub fn fix_with_space_between(input: &str) -> Result<String, JsonFixerError>
pub fn fix_with_space_between(input: &str) -> Result<String, JsonFixerError>
Fixes JSON and adds spaces between keys, values, and punctuation.
This method applies minimal formatting to make the JSON more readable while keeping it on a single line.
§Arguments
input
- The JSON string to fix
§Returns
Ok(String)
- The fixed JSON string with added spacingErr(JsonFixerError)
- If the input is too malformed to be fixed
§Examples
use json_fixer::JsonFixer;
let input = r#"{name:"John",age:30}"#;
let result = JsonFixer::fix_with_space_between(input).unwrap();
assert_eq!(result, r#"{ "name": "John", "age": 30 }"#);
Sourcepub fn fix_pretty(input: &str) -> Result<String, JsonFixerError>
pub fn fix_pretty(input: &str) -> Result<String, JsonFixerError>
Fixes JSON and applies pretty printing with proper indentation.
This method formats the JSON to be human-readable with proper indentation and line breaks.
§Arguments
input
- The JSON string to fix
§Returns
Ok(String)
- The fixed and formatted JSON stringErr(JsonFixerError)
- If the input is too malformed to be fixed
§Examples
use json_fixer::JsonFixer;
let input = r#"{name:"John",age:30,hobbies:["reading","coding"]}"#;
let result = JsonFixer::fix_pretty(input).unwrap();
// Result will be:
// {
// "name": "John",
// "age": 30,
// "hobbies": [
// "reading",
// "coding"
// ]
// }
Source§impl JsonFixer
impl JsonFixer
Sourcepub fn to_json<T: Serialize>(
value: &T,
config: Option<JsonFixerConfig>,
) -> Result<String, JsonFixerError>
pub fn to_json<T: Serialize>( value: &T, config: Option<JsonFixerConfig>, ) -> Result<String, JsonFixerError>
Converts a Rust type to a JSON string with optional formatting.
This method is only available when the serde
feature is enabled.
§Type Parameters
T
- The type to serialize, must implementserde::Serialize
§Arguments
value
- The value to convert to JSONconfig
- Optional configuration for JSON formatting
§Returns
Ok(String)
- The JSON string representationErr(JsonFixerError)
- If serialization fails
§Examples
use json_fixer::JsonFixer;
use serde::Serialize;
#[derive(Serialize)]
struct Person {
name: String,
age: u32,
}
let person = Person {
name: "John".to_string(),
age: 30,
};
let json = JsonFixer::to_json(&person, None).unwrap();
Sourcepub fn from_str<T: for<'de> Deserialize<'de>>(
input: &str,
) -> Result<T, JsonFixerError>
pub fn from_str<T: for<'de> Deserialize<'de>>( input: &str, ) -> Result<T, JsonFixerError>
Parses a JSON string into a Rust type without fixing.
This method is only available when the serde
feature is enabled.
§Type Parameters
T
- The type to deserialize into, must implementserde::Deserialize
§Arguments
input
- The JSON string to parse
§Returns
Ok(T)
- The deserialized valueErr(JsonFixerError)
- If parsing fails
§Examples
use json_fixer::JsonFixer;
use serde::Deserialize;
#[derive(Deserialize)]
struct Person {
name: String,
age: u32,
}
let json = r#"{"name":"John","age":30}"#;
let person: Person = JsonFixer::from_str(json).unwrap();
Sourcepub fn from_fixed<T: for<'de> Deserialize<'de>>(
input: &str,
config: Option<JsonFixerConfig>,
) -> Result<T, JsonFixerError>
pub fn from_fixed<T: for<'de> Deserialize<'de>>( input: &str, config: Option<JsonFixerConfig>, ) -> Result<T, JsonFixerError>
Fixes malformed JSON and then parses it into a Rust type.
This method is only available when the serde
feature is enabled.
§Type Parameters
T
- The type to deserialize into, must implementserde::Deserialize
§Arguments
input
- The potentially malformed JSON string to fix and parseconfig
- Optional configuration for JSON fixing
§Returns
Ok(T)
- The deserialized valueErr(JsonFixerError)
- If fixing or parsing fails
§Examples
use json_fixer::JsonFixer;
use serde::Deserialize;
#[derive(Deserialize)]
struct Person {
name: String,
age: u32,
}
let json = r#"{ name: "John", age: 30 }"#; // Note: unquoted keys
let person: Person = JsonFixer::from_fixed(json, None).unwrap();