c12-parser 1.0.2

Toolbox for parsing and stringifying various formats, including JSON, JSON5, JSONC, INI, TOML, and YAML.
Documentation

c12-parser

Crates.io Version Crates.io Total Downloads docs.rs GitHub commit activity GitHub Repo stars

Toolbox for parsing and stringifying various formats, including JSON, JSON5, JSONC, INI, TOML, and YAML.

Full documentation →

Quick start

cargo add c12-parser

Usage

//! Example: parse INI into a nested map (formatting not preserved).

use c12_parser::{parse_ini, stringify_ini};

fn main() {
    let text = r#"
[server]
host = 127.0.0.1
port = 8080

[app]
name = my-app
"#;

    let map = parse_ini(text);
    println!("Sections: {:?}", map.keys().collect::<Vec<_>>());

    if let Some(server) = map.get("server") {
        println!(
            "server.host = {:?}",
            server.get("host").and_then(|v| v.as_deref())
        );
        println!(
            "server.port = {:?}",
            server.get("port").and_then(|v| v.as_deref())
        );
    }

    let out = stringify_ini(&map);
    println!("Stringify:\n{}", out);
}
//! Example: parse JSON with format preservation and optional custom indent.

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

fn main() {
    let text = r#"  { "a": 1, "b": [2, 3] }  "#;

    let formatted = parse_json::<Value>(text, None).expect("parse");
    println!("Parsed: {:?}", formatted.value);

    let out = stringify_json(&formatted, None).expect("stringify");
    println!("Stringify (preserves outer whitespace): {:?}", out);

    let mut opts = FormatOptions::default();
    opts.indent = Some(4);
    let out_indent = stringify_json(&formatted, Some(opts)).expect("stringify");
    println!("Stringify (indent=4):\n{}", out_indent);
}
//! Example: parse JSON5 (unquoted keys, trailing commas, single quotes).

use c12_parser::{parse_json5, stringify_json5};
use serde_json::Value;

fn main() {
    let text = r#"
{
  name: 'c12-parser',
  version: '1.0.0',
  tags: [ 'config', 'parser', ],
}
"#;

    let formatted = parse_json5::<Value>(text, None).expect("parse");
    println!(
        "Parsed: {} {}",
        formatted.value["name"], formatted.value["version"]
    );
    println!("Tags: {:?}", formatted.value["tags"]);

    let out = stringify_json5(&formatted, None).expect("stringify");
    println!("Stringify:\n{}", out);
}
//! Example: parse JSONC (JSON with comments and optional trailing commas).

use c12_parser::{JsoncExtraOptions, parse_jsonc, stringify_jsonc};

fn main() {
    let text = r#"
{
  // config for the app
  "name": "my-app",
  "port": 3000,
  "debug": true
}
"#;

    let formatted = parse_jsonc(text, None, None).expect("parse");
    println!(
        "Parsed: {} port={}",
        formatted.value["name"], formatted.value["port"]
    );

    let out = stringify_jsonc(&formatted, None).expect("stringify");
    println!("Stringify (comments stripped, valid JSON):\n{}", out);

    let opts = JsoncExtraOptions {
        allow_trailing_comma: true,
        ..Default::default()
    };
    let with_trailing = r#"{ "x": 1, }"#;
    let parsed = parse_jsonc(with_trailing, None, Some(opts)).expect("parse with trailing comma");
    println!("With trailing comma: {:?}", parsed.value);
}
//! Example: parse TOML and stringify with outer whitespace preserved.

use c12_parser::{parse_toml, stringify_toml};
use toml::Value;

fn main() {
    let text = r#"

[package]
name = "c12-parser"
version = "1.0.0"

[dependencies]
serde = "1.0"
"#;

    let formatted = parse_toml::<Value>(text, None).expect("parse");
    if let Some(Value::Table(deps)) = formatted.value.get("dependencies") {
        println!("Dependencies: {:?}", deps.keys().collect::<Vec<_>>());
    }
    if let Some(Value::Table(pkg)) = formatted.value.get("package") {
        println!(
            "Package: {} {}",
            pkg.get("name").and_then(|v| v.as_str()).unwrap_or(""),
            pkg.get("version").and_then(|v| v.as_str()).unwrap_or("")
        );
    }

    let out = stringify_toml(&formatted, None).expect("stringify");
    println!("Stringify (outer whitespace preserved):\n{}", out);
}
//! Example: parse YAML and stringify with outer whitespace preserved.

use c12_parser::{parse_yaml, stringify_yaml};
use serde_json::Value;

fn main() {
    let text = r#"

name: c12-parser
version: 1.0.0
keywords:
  - config
  - parser
"#;

    let formatted = parse_yaml::<Value>(text, None).expect("parse");
    println!(
        "Parsed: {} {}",
        formatted.value["name"], formatted.value["version"]
    );
    println!("Keywords: {:?}", formatted.value["keywords"]);

    let out = stringify_yaml(&formatted, None).expect("stringify");
    println!("Stringify:\n{}", out);
}

License

Published under the Apache-2.0 license. Made by @UnRUST 💛


🛠️ auto updated with automd-rs