Crate figment_json5

Crate figment_json5 

Source
Expand description

A Format provider for JSON5.

This crate provides a Figment provider for JSON5 files. JSON5 is a superset of JSON that allows comments, trailing commas, and more.

This provider uses the json5 crate for parsing.

§Example

use figment::Figment;
use figment::providers::Format;
use figment_json5::Json5;
use serde::Deserialize;

#[derive(Deserialize)]
struct Config {
    name: String,
    description: String,

    #[serde(rename = "leadingDecimalPoint")]
    leading_decimal_point: f64,

    #[serde(rename = "andTrailing")]
    and_trailing: f64,

    #[serde(rename = "positiveSign")]
    positive_sign: i32,

    age: u32
}

let config = r#"{
    // Allow comments
    "name": "powerumc",
    "age": 0x28, // Allow hexadecimal numbers
    "description": "This is a \
test config", // Allow multiline strings
    "leadingDecimalPoint": .8675309,
    "andTrailing": 8675309.,
    "positiveSign": +1,
    "address": "Seoul", // Allow trailing commas
}"#;

let config: Config = Figment::new()
   .merge(Json5::string(config))
   .extract()
   .unwrap();

assert_eq!(config.name, "powerumc");
assert_eq!(config.description, "This is a test config");
assert_eq!(config.leading_decimal_point, 0.8675309);
assert_eq!(config.and_trailing, 8675309.0);
assert_eq!(config.positive_sign, 1);
assert_eq!(config.age, 40);

Structs§

Json5
A JSON5 Format Data provider.