Macro for constructing https://rustwasm.github.io/wasm-bindgen/api/wasm_bindgen/struct.JsValue.html?

Is there a macro for constructing JsValue in wasm_bindgen - Rust ?

I am looking for something that looks like json but with quasiquoting / unquoting to throw in Rust values.

I feel like I've seen exactly that — a macro that works like serde_json::json! but produces JsValues — but I can't find it now. (It's not in wasm-bindgen, js-sys, or even serde-wasm-bindgen.)

Ah, serde_json::json! might have been what I was trying to recall.

Is there an easy way to convert serde_json::Value <-> JsValue ? Given this is for JS/rust interop, the conversion is probably not expensive compared to the JS /rust-wasm boundary call.

Is there an easy way to convert serde_json::Value <-> JsValue ?

Yes, JsValue::from_serde() and into_serde(): Arbitrary Data with Serde - The `wasm-bindgen` Guide

The explanation supposes that you're using custom structs but serde_json::Value is just another serde-compatible type as far as wasm-bindgen knows.

1 Like

Good enough for me. To quote Butler Lampson: [1]

"We can solve any problem by introducing an extra level of indirection."

:slight_smile:

[1] Fundamental theorem of software engineering

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.