diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index 4e72e1d..22afcec 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -1,3 +1,5 @@ +use serde::Serialize; +use serde_wasm_bindgen::Serializer; use wasm_bindgen::prelude::wasm_bindgen; use wasm_bindgen::JsValue; use word_grid::api::APIGame; @@ -6,6 +8,8 @@ use word_grid::player_interaction::ai::Difficulty; const PLAYER_NAME: &str = "Player"; +const SERIALIZER: Serializer = Serializer::json_compatible(); + #[wasm_bindgen] pub struct WasmAPI(APIGame); @@ -30,18 +34,18 @@ impl WasmAPI { let result = self.0.exchange(PLAYER_NAME, selection); - serde_wasm_bindgen::to_value(&result).unwrap() + result.serialize(&SERIALIZER).unwrap() } pub fn pass(&mut self) -> JsValue { let result = self.0.pass(PLAYER_NAME); - serde_wasm_bindgen::to_value(&result).unwrap() + result.serialize(&SERIALIZER).unwrap() } pub fn load(&mut self) -> JsValue { let result = self.0.load(PLAYER_NAME); - serde_wasm_bindgen::to_value(&result).unwrap() + result.serialize(&SERIALIZER).unwrap() } pub fn play(&mut self, tray_tile_locations: JsValue, commit_move: bool) -> JsValue { @@ -49,12 +53,12 @@ impl WasmAPI { serde_wasm_bindgen::from_value(tray_tile_locations).unwrap(); let result = self.0.play(PLAYER_NAME, tray_tile_locations, commit_move); - serde_wasm_bindgen::to_value(&result).unwrap() + result.serialize(&SERIALIZER).unwrap() } pub fn add_to_dictionary(&mut self, word: &str) -> JsValue { let result = self.0.add_to_dictionary(PLAYER_NAME, word); - serde_wasm_bindgen::to_value(&result).unwrap() + result.serialize(&SERIALIZER).unwrap() } }