Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Wrapper to strip pseudo-JSON comments from an io::Read input stream #24

@dtolnay

Description

@dtolnay

Occasionally people ask for JSON deserialization with comments, without wanting to go all the way to JSON5 or Hjson.

This doesn't belong in serde_json which is intended strictly for JSON as described by https://json.org/.

I would like to be able to write:

use std::error::Error;
use std::fs::File;
use std::io::BufReader;

use serde::Deserialize;
use strip_json::StripComments;

fn main() -> Result<(), Box<Error>> {
    let input = File::open("path/to/input.json")?;
    let reader = BufReader::new(input);
    let clean = StripComments::new(reader);
    let mut de = serde_json::Deserializer::from_reader(clean);

    println!("{:#?}", serde_json::Value::deserialize(&mut de));

    Ok(())
}

Roughly the API would be:

pub struct StripComments<R>;

impl<R> StripComments<R> {
    pub fn new(stream: R) -> Self;
}

impl<R: Read> Read for StripComments<R>;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions