Function serde_json::to_vec

source ·
pub fn to_vec<T>(value: &T) -> Result<Vec<u8>>where
    T: ?Sized + Serialize,
Available on crate feature std only.
Expand description

Serialize the given data structure as a JSON byte vector.

Errors

Serialization can fail if T’s implementation of Serialize decides to fail, or if T contains a map with non-string keys.

Examples found in repository?
src/ser.rs (line 2143)
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
pub fn to_string<T>(value: &T) -> Result<String>
where
    T: ?Sized + Serialize,
{
    let vec = tri!(to_vec(value));
    let string = unsafe {
        // We do not emit invalid UTF-8.
        String::from_utf8_unchecked(vec)
    };
    Ok(string)
}