Trait serdeconv::ToJson

source ·
pub trait ToJson: Serialize {
    // Provided methods
    fn to_json_file<P: AsRef<Path>>(&self, path: P) -> Result<()> { ... }
    fn to_json_writer<W: Write>(&self, writer: W) -> Result<()> { ... }
    fn to_json_writer_pretty<W: Write>(&self, writer: W) -> Result<()> { ... }
    fn to_json_string(&self) -> Result<String> { ... }
    fn to_json_string_pretty(&self) -> Result<String> { ... }
}
Expand description

This trait allows to convert serializable values to JSON objects.

Examples

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

use serdeconv::ToJson;

// Defines a serializable struct.
#[derive(Serialize)]
struct Foo {
    bar: &'static str,
    baz: usize
}
impl ToJson for Foo {}

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

Provided Methods§

source

fn to_json_file<P: AsRef<Path>>(&self, path: P) -> Result<()>

Converts this to a JSON string and writes it to the speficied file.

source

fn to_json_writer<W: Write>(&self, writer: W) -> Result<()>

Converts this to a JSON string and writes it to the writer.

source

fn to_json_writer_pretty<W: Write>(&self, writer: W) -> Result<()>

Converts this to a pretty printed JSON string and writes it to the writer.

source

fn to_json_string(&self) -> Result<String>

Converts this to a JSON string.

source

fn to_json_string_pretty(&self) -> Result<String>

Converts this to a pretty printed JSON string.

Implementors§