-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Consider taking advantage of serde's + serde_json's infrastructure #170
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
Comments
Thanks for the report! I think this is basically #96 though so I'm going to close in favor of that |
Thanks for the pointer to #96. I agree it's similar, but the suggestion in this issue goes a bit further than that, in two ways.
#[wasm_bindgen]
pub fn get_json() -> serde_json::value::Value {
json!({
"tuple": [23, "string"],
"someNumber": 17,
"aListOfStrings": [
"string 1",
"string 2",
],
})
}
The solution proposed in #96 would mean that the Supporting #[wasm_bindgen]
pub fn get_optional_number(val: bool) -> serde_json::value::Value {
let option: Option<i32> = if val { Some(5) } else { None };
json!(option)
} // JS:
get_optional_number(true) == 5
get_optional_number(false) == null |
@mstange oh I think both of those will actually be enabled by #96! I was hoping to make it more explicit so you'd instead write: #[wasm_bindgen]
pub fn get_json() -> JsValue {
json!({
...
}).into()
} (or something like that) I think in #96 we'd always serialize in and out through JSON rather than having any sort of internal representation. |
Oh, I see! That's perfect. As for serializing through a JSON string, the only concern I have is that, if your data structure contains long strings, escaping and unescaping those strings might introduce unnecessary overhead. And floating point numbers similarly don't really benefit from being passed as strings. But I agree that standing up something quickly is more important for now, and if performance becomes a problem, it can still be dealt with later. |
Oh nah yeah I'd definitely agree. In general I see that most APIs around |
Well you could think up some binary serialization for JSON-shaped data that preserves the binary form of those types. In fact, it looks like BSON is such a binary serialization... but I'm not sure if it does exactly what we need here. |
Ah good point! Thankfully using |
Sounds good! Just to close the loop on this one, it looks like the binary serialization we actually want is UBJSON. It shares JSON's exact data model and just defines a binary serialization for it. There is a JS implementation of it on npm, but the only rust implementation I could find looked rather incomplete. |
Let's say I have some structured data that I want to pass from rust to JS, for example some nested structs with
Vec
s andOption
s inside them, and this structured data is representable as JSON.Today I can do this by serializing my data to a JSON string using
serde_json
and then deserializing it into a JS object by callingJSON.parse()
on the JS side. Example:This is great because it allows large freedom in the types of data that I can pass and in how I define the shape of this data. However, it has two disadvantages:
JSON.parse()
on the JS side, which makes for an awkward API.It would be really nice if wasm-bindgen was able to do just that last step on its own, i.e. the transfer of an object of type
serde_json::value::Value
. It wouldn't need to worry about how to represent rust values as JavaScript values (the JSON structure already provides that), it would just need to implement the transfer.Something similar can probably also be done for passing data in the other direction.
The text was updated successfully, but these errors were encountered: