Skip to main content

to_string

Function to_string 

Source
pub fn to_string<T: Serialize>(val: &T, format: Format) -> Result<String>
Expand description

Serialize a struct in the given format, outputting a string.

Obviously, only formats enabled with feature flags will be supported.

§Errors

Serializing to a string using msgpack will return an error due to MessagePack being a binary format.

§Examples

use polyglot::{ser, Format};
use serde::Serialize;

#[derive(Serialize)]
pub struct Person {
    pub name: String
}

let p = Person{name: String::from("John")};
let out_str = ser::to_string(&p, Format::JSON).unwrap();
assert_eq!(&out_str, "{\"name\":\"John\"}");