c12-parser 1.0.1

A parser for C12 configuration files, support JSON, JSON5, JSONC, YAML, TOML, INI
Documentation

c12-parser

Crates.io Version Crates.io Total Downloads Crates.io License

c12-parser is a parser for C12 configuration files, support JSON, JSON5, JSONC, YAML, TOML, INI.

Installation

Add this crate by cargo

cargo add c12-parser

Usage

use c12_parser::{
    parse_json,
    stringify_json,
    FormatOptions,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let text = r#"
{
  "name": "c12-parser",
  "version": "1.0.0"
}
"#;

    // Parse JSON into a serde_json::Value
    let value = parse_json(text)?;

    // Mutate the config as needed
    let mut obj = value.as_object().cloned().unwrap();
    obj.insert("debug".into(), true.into());

    // Control how formatting is preserved
    let opts = FormatOptions {
        indent: None,               // auto-detect indent from original text
        preserve_indentation: true, // keep original indentation where possible
        preserve_whitespace: true,  // keep leading/trailing whitespace
        sample_size: 1024,
    };

    // Stringify back to JSON while preserving formatting
    let output = stringify_json(&obj.into(), Some(&opts))?;
    println!("{output}");

    Ok(())
}

Contribution

  • Clone this repository
  • Install the latest version of Rust
  • Run tests using cargo test or cargo run

Credits

c12-loader has been inspired by several outstanding projects in the community:

  • @confbox - Compact YAML, TOML, JSONC and JSON5 and INI parser and serializer

License

Published under the MIT license. Made by @YONGQI 💛