How do I avoid generating JSON when serializing a value that is null or a default value?

The link Skip serializing field give me the answer. And the fixed code: #[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Ord, PartialOrd, Eq)] pub struct WeightWithOptionGroup { #[serde(skip_serializing_if = “Option::is_none”)] #[serde(default)] pub group: Option<String>, #[serde(skip_serializing_if = “is_zero”)] #[serde(default)] pub proportion: u32, } /// This is only used for serialize #[allow(clippy::trivially_copy_pass_by_ref)] fn is_zero(num: &u32) -> bool { … Read more

How do I use Serde to serialize a HashMap with structs as keys to JSON?

You can use serde_as from the serde_with crate to encode the HashMap as a sequence of key-value pairs: use serde_with::serde_as; // 1.5.1 #[serde_as] #[derive(Serialize, Deserialize, Debug)] struct Bar { #[serde_as(as = “Vec<(_, _)>”)] x: HashMap<Foo, f64>, } Which will serialize to (and deserialize from) this: { “x”:[ [{“x”: 0}, 0.0], [{“x”: 1}, 0.0], [{“x”: 2}, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)