-
It's quite possible I'm missing something relatively simple but I believe I'm following the documentation correctly. This code: use chrono::{DateTime, Utc};
use serde::Deserialize;
use serde_with::serde_as;
pub const JSON: &str = r#"
{
"market": "LINK-USD",
"rate": "0.0000111465",
"price": "14.7109999997",
"effectiveAt": "2024-01-29T04:00:00.000Z"
}
"#;
#[derive(Clone, Debug, Deserialize)]
#[serde_as]
pub struct Foo {
pub market: String,
#[serde_as(as = "DisplayFromStr")]
pub rate: f64,
#[serde_as(as = "DisplayFromStr")]
pub price: f64,
pub effective_at: DateTime<Utc>,
}
fn main() {
let obj: Result<Foo, serde_json::Error> = serde_json::from_str(&JSON);
dbg!(&obj);
} With these dependencies: [package]
name = "fundser"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = { version = "0.4.33", features = ["serde"] }
serde = "1.0.196"
serde_json = "1.0.113"
serde_with = { version = "3.5.1", features = ["macros", "json"] } Results in:
Despite |
Beta Was this translation helpful? Give feedback.
Answered by
jonasbb
Jan 29, 2024
Replies: 1 comment
-
The macro order in Rust is semantically important. The #[serde_as]
#[derive(Clone, Debug, Deserialize)] |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
jonasbb
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The macro order in Rust is semantically important. The
#[serde_as]
must come before the#[derive(...)]
.