#json5 #figment #provider #format #comma #json #comments

figment-json5

This crate provides a Figment provider for JSON5 format

2 releases

0.1.1 Dec 23, 2024
0.1.0 Dec 23, 2024

#269 in Configuration

Download history 200/week @ 2024-12-18 66/week @ 2024-12-25 1/week @ 2025-01-01 35/week @ 2025-01-08

302 downloads per month

MIT license

12KB
84 lines

Figment JSON5 Provider ci.svg crates.io docs.rs

Figment provider for JSON5 format

examples/config.json5

{
  // Allow comments
  "name": "json5",
  "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
}
use figment::Figment;
use figment::providers::Format;
use serde::Deserialize;
use figment_json5::Json5;

#[derive(Debug, 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
}

fn main() {
    let config: Config = Figment::new()
        .merge(Json5::file("./examples/config.json5"))
        .extract()
        .unwrap();

    println!("{:#?}", config)

    // print result
    /*
Config {
    name: "json5",
    description: "This is a test config",
    leading_decimal_point: 0.8675309,
    and_trailing: 8675309.0,
    positive_sign: 1,
    age: 40,
}
    */
}

Overview

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

What is JSON5?

JSON5 is an extension to the popular JSON file format that aims to be easier to write and maintain by hand (e.g. for config files). It is not intended to be used for machine-to-machine communication. (Keep using JSON or other file formats for that. 🙂)

Usage

Add the following to your Cargo.toml:

[dependencies]
figment = "0.10"
figment-json5 = "0.1.1"

License

figment-json5 is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).

Dependencies

~3.5MB
~71K SLoC