4个版本
0.2.2 | 2023年11月3日 |
---|---|
0.2.1 | 2022年4月6日 |
0.2.0 | 2019年7月1日 |
0.1.0 | 2019年6月30日 |
#96 在 解析器实现 中
47,068 每月下载量
在 97 个 crate(17个直接)中使用
15KB
266 行
json-comments-rs
json_comments
是一个用于从类似JSON的文本中移除注释的库。通过首先通过一个 StripComments
适配器处理文本,可以使用标准的JSON解析器(例如带有包含注释的准JSON输入的 serde_json)。
事实上,此代码对输入的假设很少,可能还可以用于从其他类型的代码中移除注释,前提是字符串使用双引号,并且字符串中的转义字符使用反斜杠。
支持以下类型的注释
- C样式块注释 (
/* ... */
) - C样式行注释 (
// ...
) - Shell样式行注释 (
# ...
)
使用 serde_json 的示例
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(())
}