-
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't figure out how to get Option<js_sys::Date> to work when serializing to JS #62
Comments
Yeah not being able to apply Your best bet for now is probably to change the type of that field to |
You could also wrap this into a custom function compatible with fn deserialize_optional_date<'de, D: serde::Deserializer<'de>>(de: D) -> Result<Option<js_sys::Date>, D::Error> {
let value: JsValue = serde_wasm_bindgen::preserve::deserialize(de)?;
Ok(if value.is_undefined() {
None
} else {
Some(value.dyn_into().map_err(|e| /* convert error */)?)
})
}
/// ...
#[serde(deserialize_with = "deserialize_optional_date")]
pub testedTSAsDateTime: Option<js_sys::Date>, Hope this helps. |
Thank you Ingvar, I will try both approaches and update my findings here for the benefit of others. |
Just released a small crate to help work around the issue: https://crates.io/crates/serde_nested_with |
I used this solution for now, it works perfectly. Either setting the JsValue field to JsValue::NULL or a date.dyn_into(). |
Thats amazing, thank you for your help! I have a few structs which do not require "skip_deserialize" so for those I will test the new crate. |
I updated the crate to support additional attributes, would be nice if you could give it a shot. |
This is great! I am using the following struct statement and it works like a charm: struct TestStruc {
#[serde_nested(sub = "js_sys::Date", serde(with = "serde_wasm_bindgen::preserve"))]
#[serde(skip_deserializing)]
pub testTSAsDateTime: Option<js_sys::Date>,
} Thank you again, this is amazingly helpful and hopefully will find many users besides me. @RReverser Maybe this could be added to the README? |
It's not really serde-wasm-bindgen-specific, I'd rather suggest opening a PR against Serde's own docs so that it would be on https://serde.rs/. |
Off-topic, but I'm curious @ronnybremer, in examples like pub struct Params {
#[serde(
skip_deserializing,
default = "js_sys::Date::new_0",
with = "serde_wasm_bindgen::preserve"
)]
pub testedTSAsDateTime: js_sys::Date,
} do you really need |
The example is misleading, for sure. I have a large struct coming in from Json via a Rest call, deserialized with Serde. However, I would need to add a few calculated fields to the structure before passing it onto JS. Like converting a UNIX timestamp into a JsDate. Since those fields are not coming form the server, Serde would complain and hence I skip them during deserialization. |
I have a struct containing a js_sys::Date field, which works perfectly with the
preserve
module:However, I need to adopt it to hold an "undefined/null" JS value, so I tried this:
It fails to compile:
I tried various approaches to no avail. Could you give me any hint on how to approach this issue?
The text was updated successfully, but these errors were encountered: