c12-parser 1.0.2

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

<!-- automdrs:badges showCrateVersion="true" showCrateDownloads="true" showCrateDocs="true" showCommitActivity="true" showRepoStars="true" -->

![Crates.io Version](https://img.shields.io/crates/v/c12-parser)
![Crates.io Total Downloads](https://img.shields.io/crates/d/c12-parser)
![docs.rs](https://img.shields.io/docsrs/c12-parser)
![GitHub commit activity](https://img.shields.io/github/commit-activity/m/un-rust/c12-parser)
![GitHub Repo stars](https://img.shields.io/github/stars/un-rust/c12-parser)

<!-- /automdrs -->

<!-- automdrs:description -->

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

<!-- /automdrs -->

**[Full documentation →](https://docs.rs/template/)**

## Quick start

<!-- automdrs:cargo-add -->

```sh
cargo add c12-parser
```

<!-- /automdrs -->

## Usage

<!-- automdrs:file src="./examples/parse_ini.rs" -->

```rust
//! 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);
}
```

<!-- /automdrs -->

<!-- automdrs:file src="./examples/parse_json.rs" -->

```rust
//! 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);
}
```

<!-- /automdrs -->

<!-- automdrs:file src="./examples/parse_json5.rs" -->

```rust
//! 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);
}
```

<!-- /automdrs -->

<!-- automdrs:file src="./examples/parse_jsonc.rs" -->

```rust
//! 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);
}
```

<!-- /automdrs -->

<!-- automdrs:file src="./examples/parse_toml.rs" -->

```rust
//! 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);
}
```

<!-- /automdrs -->

<!-- automdrs:file src="./examples/parse_yaml.rs" -->

```rust
//! 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);
}
```

<!-- /automdrs -->

## License

<!-- automdrs:contributors author="UnRUST" license="Apache-2.0" -->

Published under the [Apache-2.0](./LICENSE) license.
Made by [@UnRUST](https://github.com/un-rust) 💛
<br><br>
<a href="https://github.com/un-rust/c12-parser/graphs/contributors">
<img src="https://contrib.rocks/image?repo=un-rust/c12-parser" />
</a>

<!-- /automdrs -->

<!-- automdrs:with-automdrs -->

---

_🛠️ auto updated with [automd-rs](https://github.com/betterhyq/automd-rs)_

<!-- /automdrs -->