Skip to main content

parse_to_serde_value

Function parse_to_serde_value 

Source
pub fn parse_to_serde_value<T: DeserializeOwned>(
    text: &str,
    parse_options: &ParseOptions,
) -> Result<Option<T>, ParseError>
Expand description

Parses a string containing JSONC to a serde_json::Value or any type that implements serde::Deserialize.

Requires the “serde” cargo feature:

jsonc-parser = { version = "...", features = ["serde"] }

§Example

Parsing to a serde_json::Value:

use jsonc_parser::parse_to_serde_value;

let json_value = parse_to_serde_value::<serde_json::Value>(
  r#"{ "test": 5 } // test"#,
  &Default::default(),
).unwrap();

Or to a concrete type:

use jsonc_parser::parse_to_serde_value;

#[derive(serde::Deserialize)]
struct Config {
  test: u32,
}

let config = parse_to_serde_value::<Config>(
  r#"{ "test": 5 } // test"#,
  &Default::default(),
).unwrap();