Is there something like serde, but for wasm_bindgen::JsValue?

I'm looking for something that does { struct, enum } <-> wasm_bindgen::JsValue.

Pre-emptive: Why not just user serde <-> json <-> wasm_bindgen::JsValue ?
I want to be able to handle things like js/SharedArrayBuffer

I'm not sure what you're asking for, something like the structured clone algorithm?

Fundamentally, serialization will destroy everything interesting about functions and shared array buffers, so you'd have to do some sort of table key management, and it's not really much of a serialization approach at that point

Sorry for the missing context. Let's try this. Suppose we start with:

pub struct Person {
  first_name: String,
  last_name: String
}

this is easy to build Rust_Person <-> json right ?

Now suppose we have another structure:

pub struct Person2 {
  first_name: String,
  last_name: String,
  data: js_sys::SharedArrayBuffer
}

in theory, there is a Rust_Person2/wasm32-unknown-unknown <-> In browser JS representation, we can definitely construct a js object of the form

 {
  'first_name': ...,
  'last_name': ...,
  'data': ... shared array buffer ...
}

but can we do this automatically, via something serde like (but not quite serde, since afaik, json can not represent js_sys::SharedArrayBuffer)

I don't think anything like this exists at the moment.

You would need to create your own FromJS trait which lets you deserialize some T from a js_sys::Object, plus implement it for various types in std and create a custom derive.

It sounds like a lot of work, but you're probably only looking at 1-2kloc and could smash it out in a weekend if you wanted to.

1 Like

Again I'm not sure exactly what you're looking for here: you say "in js we can construct (an object containing a shared array buffer)", but that's not serialization (you can somewhat treat source code as serialization of the program behavior, which include that object in it's state, but that's getting a bit meta!)

When you post that object to a worker, for example, the structured clone algorithm actually detects and side-channels shared array buffers such that they are actually shared. Functions are not postable, since they are tied to a JS runtime context, etc.. Otherwise that shared array buffer isn't going anywhere, so no serialization is occurring.

In particular, you should be aware that currently JsValue is currently simply an index into a table of objects in JS, making the question of serializing it rather more ambiguous than it might otherwise be: if you're trying to send it to JS, then you're too late: it's already there!

Edit: you might be looking for GitHub - cloudflare/serde-wasm-bindgen: Native integration of Serde with wasm-bindgen

1 Like

I think I am starting to see the confusion now. Note: although I say 'something like serde', I never use the word serialization.

In my mind, I have a Rust program compiled to wasm32-unknown-unknown running on Chrome.

Now, I want to be able to expose some Rust data types to JS land.

I have a live Rust value; I want to construct a live JS value. This is what I mean by "something like serde".

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.