Skip to content

Instantly share code, notes, and snippets.

@tcr
Created June 1, 2018 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcr/1d8dc5b2d324c8e6d69625e03587b639 to your computer and use it in GitHub Desktop.
Save tcr/1d8dc5b2d324c8e6d69625e03587b639 to your computer and use it in GitHub Desktop.
#![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