Skip to content

Add JsValue::{from_serde, into_serde} #171

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

Merged
merged 1 commit into from
Apr 27, 2018
Merged

Add JsValue::{from_serde, into_serde} #171

merged 1 commit into from
Apr 27, 2018

Conversation

alexcrichton
Copy link
Contributor

@alexcrichton alexcrichton commented Apr 27, 2018

These functions are activated with the serde-serialization serde-serialize feature of the
wasm-bindgen crate. When activated they will allow passing any arbitrary value
into JS that implements the Serialize trait and receiving any value from JS
using the Deserialize trait. The interchange between JS and Rust is JSON.

Closes #96

These functions are activated with the `serde-serialization` feature of the
`wasm-bindgen` crate. When activated they will allow passing any arbitrary value
into JS that implements the `Serialize` trait and receiving any value from JS
using the `Deserialize` trait. The interchange between JS and Rust is JSON.

Closes #96
@alexcrichton alexcrichton merged commit 11cd661 into master Apr 27, 2018
@alexcrichton alexcrichton deleted the serde branch April 27, 2018 17:39
@udoprog
Copy link

udoprog commented Apr 27, 2018

Oh. This looks really good. So we serialize incoming objects and pass the string in, vice-versa for passing them out?

I think this effectively minimises the number of context switches and is the most pragmatic solution right now.

A potential future improvement could be to encode objects in a way which has a more efficient representation and supports skipping properties when decoding. But it will be hard to compete with the efficiency of the built-in JSON support.

@alexcrichton
Copy link
Contributor Author

@udoprog precisely! You basically got all the intentions there :)

@David-OConnor
Copy link
Member

David-OConnor commented May 23, 2018

Can someone provide an example of how to enable this?

wasm-bindgen = {version = "^0.2", features = [“serde-serialization”,]}

in Cargo.toml, and

#[cfg(feature = "serde-serialization")]
 extern crate serde;

both lead to this:

error[E0433]: failed to resolve. Maybe a missing extern crate wasm_bindgen;?

@alexcrichton
Copy link
Contributor Author

@David-OConnor oh so after you enable the features in Cargo.toml you don't need the #[cfg] in your own code for serde, you should just be able to use wasm-bindgen as usual. Do you have an example I could poke around though?

@jsheard
Copy link
Contributor

jsheard commented May 23, 2018

The feature flag is actually serde-serialize, not serde-serialization.

@alexcrichton made a typo in the OP here 😛

@alexcrichton
Copy link
Contributor Author

Oops, thanks @jsheard!

@David-OConnor
Copy link
Member

David-OConnor commented May 23, 2018

Thank you for the help. I think I'm just not understanding the syntax of how to enable the feature; I've attempted the above two with the correct spelling, plus two additional attempts:

[dependencies.wasm-bindgen]
version = "^0.2"
features = ["serde-serialize"],

in Cargo.toml: invalid TOML error.
and
#![feature(proc_macro, wasm_custom_section, wasm_import_module, wasm_serde_serialize)]
at the top of my lib.rs; doesn't break anything like the other attempts, but doesn't expose from_serde.

@alexcrichton
Copy link
Contributor Author

@David-OConnor oh the trailing comma there is invalid syntax, but if you remove that you should be good!

@David-OConnor
Copy link
Member

Thank you! That worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants