simplify json serialization code

This commit is contained in:
Felix 2020-01-18 17:25:45 +01:00
parent cde4a8a977
commit 05ee5a8b60

View File

@ -295,18 +295,21 @@ impl Handler<StandardMessage> for ChatServer {
} }
} }
#[derive(Serialize)]
struct WebsocketResponse<T> {
op: String,
data: T,
}
fn to_json_string<T>(op: &UserOperation, data: T) -> Result<String, Error> fn to_json_string<T>(op: &UserOperation, data: T) -> Result<String, Error>
where where
T: Serialize, T: Serialize,
{ {
let mut json = serde_json::to_value(&data)?; let response = WebsocketResponse {
match json.as_object_mut() { op: op.to_string(),
Some(j) => j.insert("op".to_string(), serde_json::to_value(op.to_string())?), data,
None => return Err(format_err!("")),
}; };
// TODO: it seems like this is never called? Ok(serde_json::to_string(&response)?)
let x = serde_json::to_string(&json)?;
Ok(x)
} }
fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<String, Error> { fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<String, Error> {