From: Felix Date: Sat, 18 Jan 2020 16:25:45 +0000 (+0100) Subject: simplify json serialization code X-Git-Url: http://these/git/%7BpictrsAvatarThumbnail%28this.props.site.site.icon%29%7D?a=commitdiff_plain;h=05ee5a8b600fdd7a64ad66bb52978befed3a7614;p=lemmy.git simplify json serialization code --- diff --git a/server/src/websocket/server.rs b/server/src/websocket/server.rs index 2d9ddcd4..9afbff25 100644 --- a/server/src/websocket/server.rs +++ b/server/src/websocket/server.rs @@ -295,18 +295,21 @@ impl Handler for ChatServer { } } +#[derive(Serialize)] +struct WebsocketResponse { + op: String, + data: T, +} + fn to_json_string(op: &UserOperation, data: T) -> Result where T: Serialize, { - let mut json = serde_json::to_value(&data)?; - match json.as_object_mut() { - Some(j) => j.insert("op".to_string(), serde_json::to_value(op.to_string())?), - None => return Err(format_err!("")), + let response = WebsocketResponse { + op: op.to_string(), + data, }; - // TODO: it seems like this is never called? - let x = serde_json::to_string(&json)?; - Ok(x) + Ok(serde_json::to_string(&response)?) } fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result {