Miniserde provides Serialize and Deserialize derive macros for strongly typed data structures. Use miniserde::json::to_string to convert a type to a JSON string, and miniserde::json::from_str to parse a JSON string back into a type.
Note that serialization is infallible (always succeeds) and returns a String. Deserialization returns a miniserde::Result, where errors are represented by a unit struct containing no specific error information.
use miniserde::{json, Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Example {
code: u32,
message: String,
}
fn main() -> miniserde::Result<()> {
let example = Example {
code: 200,
message: "reminiscent of Serde".to_owned(),
};
// Serialize to JSON string
let j = json::to_string(&example);
println!("{}", j);
// Deserialize from JSON string
let out: Example = json::from_str(&j)?;
println!("{:?}", out);
Ok(())
}