Function from_str

Source
pub fn from_str<'de, T>(input: &'de str) -> Result<T, Error>
where T: Deserialize<'de>,
Expand description

Parse a JSON5 string and map it to a type implementing Deserialize.

§Example

use serde_derive::Deserialize;

#[derive(Debug, PartialEq, Deserialize)]
struct Config<'a> {
    foo: u32,
    bar: &'a str,
}

let config: Config = json5::from_str("
  {
    // Note unquoted keys, comments, and trailing commas.
    foo: 42,
    bar: 'baz',
  }
")?;

assert_eq!(config, Config{ foo: 42, bar: "baz" });

§Errors

Fails if the JSON5 is malformed or we can’t map it to a T.