[go: up one dir, main page]

FromJson

Trait FromJson 

Source
pub trait FromJson: for<'a> Deserialize<'a> {
    // Provided methods
    fn from_json_file<P: AsRef<Path>>(path: P) -> Result<Self> { ... }
    fn from_json_reader<R: Read>(reader: R) -> Result<Self> { ... }
    fn from_json_str(json: &str) -> Result<Self> { ... }
    fn from_json_slice(json: &[u8]) -> Result<Self> { ... }
}
Expand description

This trait allows to convert JSON objects to deserializable values.

§Examples

extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serdeconv;

use serdeconv::FromJson;

// Defines a deserializable struct.
#[derive(Deserialize)]
struct Foo {
    bar: String,
    baz: usize
}
impl FromJson for Foo {}

// Converts from the JSON string to a `Foo` value.
let json = r#"{
    "bar": "aaa",
    "baz": 123
}"#;
let foo = Foo::from_json_str(json).unwrap();
assert_eq!(foo.bar, "aaa");
assert_eq!(foo.baz, 123);

Provided Methods§

Source

fn from_json_file<P: AsRef<Path>>(path: P) -> Result<Self>

Converts from the JSON file to an instance of this implementation.

Source

fn from_json_reader<R: Read>(reader: R) -> Result<Self>

Reads a JSON string from the reader and converts it to an instance of this implementation.

Source

fn from_json_str(json: &str) -> Result<Self>

Converts from the JSON string to an instance of this implementation.

Source

fn from_json_slice(json: &[u8]) -> Result<Self>

Converts from the JSON bytes to an instance of this implementation.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§