Function to_string

Source
pub fn to_string<T: Serialize>(value: &T) -> Result<String, Error>
Expand description

Serialize a type implementing Serialize to a JSON5 string.

§Example

use serde_derive::Serialize;

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

let config = Config {
    foo: 42,
    bar: "baz",
};

assert_eq!(&json5::to_string(&config)?, "{
  foo: 42,
  bar: \"baz\",
}");

§Errors

Fails if we can’t express T in JSON5 (e.g. we try to serialize an object key without an obvious string representation).