4 releases

0.2.2 Nov 3, 2023
0.2.1 Apr 6, 2022
0.2.0 Jul 1, 2019
0.1.0 Jun 30, 2019

#123 in Parser implementations

Download history 13585/week @ 2024-06-24 11346/week @ 2024-07-01 13239/week @ 2024-07-08 14155/week @ 2024-07-15 13588/week @ 2024-07-22 12848/week @ 2024-07-29 12354/week @ 2024-08-05 12533/week @ 2024-08-12 13231/week @ 2024-08-19 13828/week @ 2024-08-26 13573/week @ 2024-09-02 14601/week @ 2024-09-09 14214/week @ 2024-09-16 14948/week @ 2024-09-23 13781/week @ 2024-09-30 13675/week @ 2024-10-07

57,217 downloads per month
Used in 101 crates (19 directly)

Apache-2.0

15KB
266 lines

json-comments-rs

Build Status Docs

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 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

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

No runtime deps