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解析器实现

Download history 9310/week @ 2024-03-14 8107/week @ 2024-03-21 7800/week @ 2024-03-28 9831/week @ 2024-04-04 8991/week @ 2024-04-11 7773/week @ 2024-04-18 7698/week @ 2024-04-25 9244/week @ 2024-05-02 8266/week @ 2024-05-09 8753/week @ 2024-05-16 9005/week @ 2024-05-23 11824/week @ 2024-05-30 10346/week @ 2024-06-06 10929/week @ 2024-06-13 12497/week @ 2024-06-20 11162/week @ 2024-06-27

47,068 每月下载量
97 crate(17个直接)中使用

Apache-2.0

15KB
266

json-comments-rs

Build Status Docs

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(())
}

无运行时依赖