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
Javascript breaking data when using Fetch API and Serde Serialization/Deserialization .json()
#2894
Comments
Thanks for the report. I would say that I actually expected "better" from the @hamza1311 might have an opinion on this, maybe it makes sense to upstream the issue to gloo? |
Its my pleasure to report my findings and I hope it will help the community. And at the same time hopefully I can learn more about bug reporting and how to contribute while following the resolution of this issue~ |
I am terribly sorry i just found out that in my |
Wow my honor is safe (kinda |
Ah I know why: in the reqwasm crate |
Can you try It might be better to use |
Yes that's what I did and explained in the original Issue post to fix the issue and it is working as intended as its not using the Javascript JSON Eval~ However I dont know how should I do to fix it in gloo-net |
I will look into it tomorrow I see where: /// Reads the response to completion, parsing it as JSON.
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub async fn json<T: DeserializeOwned>(&self) -> Result<T, Error> {
let promise = self.response.json().map_err(js_to_error)?; // --> Here
let json = JsFuture::from(promise).await.map_err(js_to_error)?;
Ok(JsValueSerdeExt::into_serde(&json)?)
} Maybe with this /// Reads the response to completion, parsing it as JSON.
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub async fn json<T: DeserializeOwned>(&self) -> Result<T, Error> {
serde_json::from_str::<T>(&self.text().await?).map_err(Error::from)
} |
Closing this as this has been resolved |
This is about:
Problem
When building JSON APIs using Rust on both the Backend and Frontend, one would expect that the Serialization and Deserialization of any Serde Derived Struct would be without issues. However, Javascript JSON evaluation used by
gloo_net::http::Response
with the.json()
call, is limited by Javascript capabilities. See 64bit number storage issue in JS over JSON.When I built my APIs following this tutorial (which is great BTW, just I think this specific issue could hit other people than me), I had a Rust Struct containing a
i64
value that was ati64::MAX
. And to my big surprise the JSON RAW value was correct:9223372036854775807
, but the.json()
value from the Response was9223372036854776000
hence triggering a Parsing error in the Serde library.The solution i found (might not be the best, I would be pleased to get feedbacks, I am very new to Web Development), is to get the raw body content using the
.text()
function on the Response, and then useserde_json::from_str()
on that String, hence bypassing any Evaluation of the content by Javascript, and avoiding the flaw (thats why we use Rust in the WebBrowser right? XD ).Details about the solution you'd like
Maybe precise in the tutorial that to avoid issues related to Javascript JSON evaluation, one could use
.text()
instead of.json()
and use theserde_json::from_str()
call instead to make sure that if Serde can Serialize on the Backend, it can also Deserialize it in the Frontend.Questionaire (Optional)
Others
Thank you very much for the Yew Framework~~
And if you think i am wrong or there is any details i missed, please let me know, i would be happy to learn more!
Wish you a nice day!
The text was updated successfully, but these errors were encountered: