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
Add support for serializing and deserializing JsValue? #32
Comments
Thinking about this a bit more, I suspect that such a feature would require additional functionality from both serde and wasm-bindgen sides to achieve this... |
Yeah unfortunately that wouldn't be possible. |
@RReverser do you have any thoughts regarding workarounds that could achieve something like this, i.e., moving a Rust struct containing JsValues to Javascript? |
I think instead of using Serde in this case you'd just have to implement conversion via manual methods, where you take a JsValue and step-by-step extract the properties you're interested in to a Rust struct or vice versa. Of course, if the JsValue you want to preserve is a primitive JSON-like value, you can instead use |
I think there is a limitation with that approach in that it will not work for types from web_sys such as |
If you mean the 2nd one, then yeah, as I said, it works only for primitive JSON-like values. For anything else you need to implement custom conversion methods. |
Hmm actually, while very hacky, it could be possible for serde-wasm-bindgen to provide some sort of
This way, it could preserve Unfortunately, I'm pretty busy right now and won't be able to dig into this more, but if this makes sense to you, you're welcome to try in a PR :) |
That does sounds pretty hacky! While at a high level I follow what you are saying, I don't think I have quite enough experience with serde or wasm-bindgen at this stage to be able to put a PR together in a reasonable time frame. So unfortunately I think this idea will have to stay on the back burner for the moment. |
In my experience, implementing this kind of hacks is the best and fun way to learn :) But, up to you, of course. |
I completely agree, however, since I am also trying to start a business, I have to be pragmatic and unfortunately just work around these sorts of issues from time to time :) |
Instead of implementing So
// can directly derive here because `JsTyped<Element>` is Serialize + Deserialize
#[derive(Serialize, Deserialize)]
struct SomeValueToPassToJs {
element: JsTyped<Element>,
} |
Problem is, you can't implement Instead, you need something like described in #32 (comment) (messing up with internal handles). |
The intuitive answer seems to be no, but actually there is a way to do it (if we are talking about hacks). There're 2 quirks with JsValue (and anything contains JsValue):
Hence, serializer and deserializer can be detected with thread-locals. |
Why not? You can have nested serializers or deserializers. Personally, I'd prefer to go with newtype + handles, those are safe in all scenarios. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Since This also takes advantage of the |
The biggest issue with my suggestion, is that there's no way to know what serializer will be used, (with specialization we could), so if the user serialized to, for example, JSON, then deserializing would necessarily not work, so some trickery would need to happen to make this work in that case. But perhaps all of this could be mitigated with an explicit unsafe naming, such as |
That was more or less my suggestion and see the open PR at #40 that basically does that. It needed a few minor final touches but fell off the radar I suspect. |
Closed by #40. |
Let say we have a
struct
in Rust as follows:It would be nice to be able to pass
t
back and forth between Rust and Javascript, where during "serialization"v1
andv2
are just replaced with the actual Javascript values they represent, and on deserialization, the Javascript values are pushed onto wasm-bindgen's heap and the struct members are initialized withJsValue
s that reference those Javascript objects on the heap.At the moment, this doesn't work out of the box since
Serialize
is not implemented forJsValue
, which makes sense since it's internal representation is just an index to wasm-bindgen's heap, but perhaps there is a way to hack this, possibly with#[serde(serialize_with = "path")]
on theJsValue
fields?The text was updated successfully, but these errors were encountered: