gloo icon indicating copy to clipboard operation
gloo copied to clipboard

feat(gloo-utils): Lift serde-serialization from wasm-bindgen

Open andoriyu opened this issue 3 years ago • 0 comments

Per discussion in #239 adding support for from_serde and into_serde to gloo-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 case serde_json gives an invalid JSON. It's probably safe to do unwrap_unchecked, but I wasn't sure which MSRV should I target.

into_json is more complicated. If you pass undefined to JSON.stringify it throws an error somewhere inside. Current behavior was to return an error, to keep current behavior, I've added a check if JsValue is undefined.

While at it, made changes to crates that were using serde_json (#239 PR) to use functions from gloo-utils. Test suite is lifted from wasm-bindgen nearly verbatim.

How JSON.stringify function behaves in a browser:

> JSON.stringify({w: undefined});
"{}"
> JSON.stringify(undefined);
undefined << causes panic in rust

andoriyu avatar Aug 15 '22 01:08 andoriyu