Function to_value

Source
pub fn to_value<T>(value: T) -> Result<Value, SerializeError>
where T: Serialize,
Expand description

Serializes the given value into a JSON Value.

ยงExample

use serde::Serialize;
use json_syntax::{json, Value};

#[derive(Serialize)]
struct User {
    fingerprint: String,
    location: String,
}

let u = User {
  fingerprint: "0xF9BA143B95FF6D82".to_owned(),
  location: "Menlo Park, CA".to_owned(),
};

let expected: Value = json!({
  "fingerprint": "0xF9BA143B95FF6D82",
  "location": "Menlo Park, CA",
});

let v = json_syntax::to_value(u).unwrap();
assert_eq!(v, expected);