-
Notifications
You must be signed in to change notification settings - Fork 147
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
feat(gloo-utils): Lift serde-serialization from wasm-bindgen #242
feat(gloo-utils): Lift serde-serialization from wasm-bindgen #242
Conversation
18e2d4a
to
a49af99
Compare
Fixed the formatting error. I thought I ran it, but I guess I didn't. Clippy error & browser test failure looks like unrelated issue? |
Can you please merge master so CI is green? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to document the feature on docs.rs as well. This can done by:
lib.rs
#![cfg_attr(docsrs, feature(doc_cfg))
on functions
#[cfg_attr(docsrs, doc(cfg(feature = "_")))]
Cargo.toml
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
87a006f
to
ff13de2
Compare
I think that covers all requested changes. I had to use UFCS in a few places because otherwise it confuses with Tests - the way I have written it, tests must be run in I'm not sure why unrelated test failed. |
@hamza1311 do you want to keep: let s = if self.is_undefined() {
String::new()
} else {
js_sys::JSON::stringify(self)
.map(String::from)
.unwrap_throw()
}; or do what There is also an option to use https://docs.rs/js-sys/latest/js_sys/JSON/fn.stringify_with_replacer.html, but I think it will slow down serialization too much? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why unrelated test failed.
The tests are all green on master now (as of #244 thanks to @futursolo). If you merge master here, the only failing tests should be from this PR
Co-authored-by: Muhammad Hamza <muhammadhamza1311@gmail.com>
Co-authored-by: Muhammad Hamza <muhammadhamza1311@gmail.com>
Co-authored-by: Muhammad Hamza <muhammadhamza1311@gmail.com>
7aa660a
to
f34da81
Compare
crates/utils/src/format/json.rs
Outdated
// we're passed `undefined` reinterpret it as `null` for JSON | ||
// purposes. | ||
#[wasm_bindgen( | ||
inline_js = "export function serialize(obj) { return JSON.stringify(obj === undefined ? null : obj) }" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inline JS snippets could cause issues for non-ES module targets.
no-modules
target is still the only target supported by gloo-worker
.
Instead of going through JSON::stringify
and serde_json
, I would suggest using serde-wasm-bindgen
. It supports non-serializable types (e.g.: ES2015 Map
) and has a smaller bundle footprint than serde_json
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@futursolo oh well, back to what I had before.
So if you use serde_wasm_bindgen
, then you don't really need this. The goal is to provide a drop-in replacement for JsValue::{from_serde/to_serde}
and an easier way to use js_sys::JSON::
.
Plus, in the deprecation thread, someone said they had a bad time with serde-wasm-bindgen
. I think it makes sense: it calls to JS for every field which is slow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Thanks a lot for your work!
These changes have been released in: gloo-utils: v0.1.5 |
Per discussion in #239 adding support for
from_serde
andinto_serde
togloo-utils
.I couldn't replicate functionality 1:1 because
wasm-bindgen
uses private API methods (__wbindgen_json_serialize
and__wbindgen_json_parse
), so I've reimplemented it with publicly available APIs.from_serde
stayed nearly the same, but throws an error in caseserde_json
gives an invalid JSON. It's probably safe to dounwrap_unchecked
, but I wasn't sure which MSRV should I target.into_json
is more complicated. If you passundefined
toJSON.stringify
it throws an error somewhere inside. Current behavior was to return an error, to keep current behavior, I've added a check ifJsValue
isundefined
.While at it, made changes to crates that were using
serde_json
(#239 PR) to use functions fromgloo-utils
. Test suite is lifted fromwasm-bindgen
nearly verbatim.How
JSON.stringify
function behaves in a browser: