feat(gloo-utils): Lift serde-serialization from wasm-bindgen#242
feat(gloo-utils): Lift serde-serialization from wasm-bindgen#242ranile merged 11 commits intoranile:masterfrom
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? |
ranile
left a comment
There was a problem hiding this comment.
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? |
ranile
left a comment
There was a problem hiding this comment.
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.
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.
@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.
ranile
left a comment
There was a problem hiding this comment.
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_serdeandinto_serdetogloo-utils.I couldn't replicate functionality 1:1 because
wasm-bindgenuses private API methods (__wbindgen_json_serializeand__wbindgen_json_parse), so I've reimplemented it with publicly available APIs.from_serdestayed nearly the same, but throws an error in caseserde_jsongives an invalid JSON. It's probably safe to dounwrap_unchecked, but I wasn't sure which MSRV should I target.into_jsonis more complicated. If you passundefinedtoJSON.stringifyit throws an error somewhere inside. Current behavior was to return an error, to keep current behavior, I've added a check ifJsValueisundefined.While at it, made changes to crates that were using
serde_json(#239 PR) to use functions fromgloo-utils. Test suite is lifted fromwasm-bindgennearly verbatim.How
JSON.stringifyfunction behaves in a browser: