# serde ## Notes ### Deserializing String UNIX timestamps https://stackoverflow.com/a/66879209 ```json { "best_header_timestamp": "1631159293", } ``` ```rust use serde_with::TimestampSeconds; use serde_with::formats::Strict; #[serde_with::serde_as] #[derive(Clone, Debug, Deserialize)] pub struct GetInfoResponse { #[serde_as(as = "TimestampSeconds<String, Strict>")] pub best_header_timestamp: DateTime<Utc>, ``` ### Deserializing numbers encoded as string https://docs.rs/serde_with/1.10.0/serde_with/struct.DisplayFromStr.html ```rust use serde_with::DisplayFromStr; use serde_with::serde_as; #[serde_as] #[derive(Deserialize)] pub struct Invoice { #[serde_as(as = "DisplayFromStr")] pub add_index: u64, } ``` - Also supports nesting, as noted in the second comment to this [[Stack Overflow]] question [here](https://stackoverflow.com/q/67635294) - Update: Not so sure about that, see the next section ### Deserializing number encoded as string wrapped within an `Option` ```rust #[derive(Clone, Debug, Deserialize)] pub struct U64(u64); #[derive(Clone, Debug, Deserialize)] pub struct Invoice { pub value: Option<U64>, } ``` Workaround source: https://github.com/serde-rs/json/issues/373#issuecomment-338831929 In the future, [[serde]] will support syntax like ```rust struct Foo { #[serde_as(as = "DisplayFromStr")] bar: Option<f64>, } ``` Tracking issue: https://github.com/serde-rs/serde/issues/723 ### De/serializing hex values https://docs.rs/serde_with/1.10.0/serde_with/hex/struct.Hex.html