Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#![allow(unused)]
extern crate ron;
extern crate serde;
#[macro_use] extern crate serde_derive;
extern crate serde_with;
use serde::{Deserialize, Serialize, Serializer, Deserializer};
#[derive(Serialize, Deserialize)]
struct Doc {
always: String,
#[serde(skip_serializing_if = "Option::is_none", default, with = "serde_with::option::unwrap_or_skip")]
optional: Option<String>,
}
#[derive(Serialize, Deserialize)]
struct NoDependencies {
always: String,
#[serde(skip_serializing_if = "Option::is_none")]
optional: Option<String>,
}
fn main() {
// with serde_with
println!("serde_with");
let value_a = Doc {
always: format!("hi"),
optional: None,
};
let value_b = Doc {
always: format!("hi"),
optional: Some(format!("hello")),
};
let a = ron::ser::to_string(&value_a).unwrap();
println!(" a: {:?}", a);
let a_2: Doc = ron::de::from_str(&a).unwrap();
println!(" a': {:?}", a);
let b = ron::ser::to_string(&value_b).unwrap();
println!(" b: {:?}", b);
let b_2: Doc = ron::de::from_str(&b).unwrap();
println!(" b': {:?}", b);
// no dependencies
println!("no deps");
let value_a = NoDependencies {
always: format!("hi"),
optional: None,
};
let value_b = NoDependencies {
always: format!("hi"),
optional: Some(format!("hello")),
};
let a = ron::ser::to_string(&value_a).unwrap();
println!(" a: {:?}", a);
let a_2: NoDependencies = ron::de::from_str(&a).unwrap();
println!(" a': {:?}", a);
let b = ron::ser::to_string(&value_b).unwrap();
println!(" b: {:?}", b);
let b_2: NoDependencies = ron::de::from_str(&b).unwrap();
println!(" b': {:?}", b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.