Module json5

JSON5 parsing and serialization.


json5 provides parsing and serialization for the JSON5 format using the serde framework. JSON5 is a superset of JSON that allows comments, trailing commas, unquoted keys, single-quoted strings, and other conveniences that make it friendlier for hand-edited config files.

The main functions are from_str for deserialization and to_string for serialization.

Examples

Deserializing a JSON5 config with comments and trailing commas:

use serde::Deserialize;

#[derive(Deserialize, Debug, PartialEq)]
struct Config {
    name: String,
    port: u16,
    debug: bool,
}

let json5_str = "{
    // server name
    name: 'my-app',
    port: 8080,
    debug: true,
}";

let config: Config = json5::from_str(json5_str).unwrap();
assert_eq!(config.name, "my-app");
assert_eq!(config.port, 8080);
assert!(config.debug);

Serializing a struct to JSON5:

use serde::Serialize;

#[derive(Serialize)]
struct Settings {
    title: String,
    max_retries: u32,
}

let settings = Settings {
    title: "Example".to_string(),
    max_retries: 3,
};

let output = json5::to_string(&settings).unwrap();
assert!(output.contains("Example"));
assert!(output.contains("3"));