use crate::{Decoder, Encoder};
use serde::{Deserialize, Serialize};
pub struct JsonSerdeWasmCodec;
impl<T: Serialize> Encoder<T> for JsonSerdeWasmCodec {
type Error = serde_json::Error;
type Encoded = String;
fn encode(val: &T) -> Result<Self::Encoded, Self::Error> {
serde_json::to_string(val)
}
}
impl<T> Decoder<T> for JsonSerdeWasmCodec
where
for<'de> T: Deserialize<'de>,
{
type Error = wasm_bindgen::JsValue;
type Encoded = str;
fn decode(val: &Self::Encoded) -> Result<T, Self::Error> {
let json = js_sys::JSON::parse(val)?;
Ok(serde_wasm_bindgen::from_value(json)?)
}
}