jsonc-to-json

CI Latest Version Docs License

Simple library for converting [JSON with Comments] into [JSON], in short it removes the following:

Note: The implementation does not use a full-blown [JSON with Comments] parser. Instead it uses a [JSON with Comments] tokenizer, which makes conversion a lot faster.

Currently #![no_std] is not supported. It will however be added, when some upstream changes have been applied.

See [jsonc_to_json()] for more information.

Example

```rust use jsonctojson::{jsonctojson, jsonctojson_into};

let jsonc = "{\"arr\": [1, 2,/* Comment */ 3, 4,,]}// Line Comment";

let json = jsonctojson(jsonc); println!("{}", json);

// Alternatively, use jsonc_to_json_into() to reuse an // already allocated String let mut json = String::new(); jsonctojson_into(jsonc, &mut json); println!("{}", json); ```

Both output the following:

text {"arr": [1, 2, 3, 4]}

Serde Example

```rust use jsonctojson::{jsonctojson, jsonctojson_into}; use serde::Deserialize;

[derive(Deserialize, Debug)]

struct Data { arr: Vec, } let jsonc = "{\"arr\": [1, 2,/* Comment */ 3, 4,,]}// Line Comment";

let json = jsonctojson(jsonc); let data: Data = serdejson::fromstr(&json)?;

println!("{}", json); println!("{:?}", data); ```

Which outputs the following:

text {"arr": [1, 2, 3, 4]} Data { arr: [1, 2, 3, 4] }

Non-Allocating & Zero-Copy Iterator Example

Non-allocating [Iterator] that yields string slices of valid [JSON].

```rust use jsonctojson::jsonctojson_iter;

let jsonc = r#"{foo}/**/[1,2,3,,]"bar""#;

let mut iter = jsonctojsoniter(jsonc); asserteq!(iter.next(), Some("{foo}")); // Line comment was removed asserteq!(iter.next(), Some("[1,2,3")); // Trailing commas was removed asserteq!(iter.next(), Some("]\"bar\"")); assert_eq!(iter.next(), None); ```