[][src]Module serde_with::rust::default_on_null

Deserialize default value if encountering null.

One use case are JSON APIs in which the null value represents some default state. This adapter allows to turn the null directly into the Default value of the type.


struct A {
    #[serde(deserialize_with = "serde_with::rust::default_on_null::deserialize")]
    value: u32,

let a: A = serde_json::from_str(r#"{"value": 123}"#).unwrap();
assert_eq!(123, a.value);

let a: A = serde_json::from_str(r#"{"value": null}"#).unwrap();
assert_eq!(0, a.value);

// String is invalid type
assert!(serde_json::from_str::<A>(r#"{"value": "123"}"#).is_err());



Deserialize T and return the Default value if original value is null


Serialize value with the default serializer