Crate json_strip_comments

source ·
Expand description

Replace json comments and trailing commas in place.

A fork of a fork:

json-strip-comments is a library to strip out comments from JSON. 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

use serde_json::Value;

fn main() {
    let mut data = String::from(
        r#"
     {
         "name": /* full */ "John Doe",
         "age": 43,
         "phones": [
             "+44 1234567", // work phone
             "+44 2345678", // home phone
         ] /** comment **/
     }"#,
    );

    json_strip_comments::strip(&mut data).unwrap();
    let value: Value = serde_json::from_str(&data).unwrap();

    println!("{value}");
}

Structs§

  • Settings for StripComments
  • A Read that transforms another Read so that it changes all comments to spaces so that a downstream json parser (such as json-serde) doesn’t choke on them.

Functions§