https://github.com/tmccombs/json-comments-rs
Crate to strip out comments in json for Rust
https://github.com/tmccombs/json-comments-rs
Last synced: 11 months ago
JSON representation
Crate to strip out comments in json for Rust
- Host: GitHub
- URL: https://github.com/tmccombs/json-comments-rs
- Owner: tmccombs
- License: apache-2.0
- Created: 2019-06-29T23:14:56.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2023-11-03T06:07:04.000Z (over 2 years ago)
- Last Synced: 2024-05-02T01:07:57.182Z (about 2 years ago)
- Language: Rust
- Size: 19.5 KB
- Stars: 20
- Watchers: 4
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# json-comments-rs
[](https://github.com/tmccombs/json-comments-rs/actions)
[](https://docs.rs/json_comments/)
`json_comments` is a library to strip out comments from JSON-like text. By processing text
through a [`StripComments`] adapter first, it is possible to use a standard JSON parser (such
as [serde\_json](https://crates.io/crates/serde_json) with quasi-json input that contains
comments.
In fact, this code makes few assumptions about the input and could probably be used to strip
comments out of other types of code as well, provided that strings use double quotes and
backslashes are used for escapes in strings.
The following types of comments are supported:
- C style block comments (`/* ... */`)
- C style line comments (`// ...`)
- Shell style line comments (`# ...`)
## Example using serde\_json
```rust
use serde_json::{Result, Value};
use json_comments::StripComments;
fn main() -> Result<()> {
// Some JSON input data as a &str. Maybe this comes form the user.
let data = r#"
{
"name": /* full */ "John Doe",
"age": 43,
"phones": [
"+44 1234567", // work phone
"+44 2345678" // home phone
]
}"#;
// Strip the comments from the input (use `as_bytes()` to get a `Read`).
let stripped = StripComments::new(data.as_bytes());
// Parse the string of data into serde_json::Value.
let v: Value = serde_json::from_reader(stripped)?;
println!("Please call {} at the number {}", v["name"], v["phones"][0]);
Ok(())
}
```