Skip to content

Instantly share code, notes, and snippets.

@JamieAP
Created February 16, 2016 13:13
Show Gist options
  • Save JamieAP/3c3dd2ece49bef05b137 to your computer and use it in GitHub Desktop.
Save JamieAP/3c3dd2ece49bef05b137 to your computer and use it in GitHub Desktop.
public class JsonDecoder<T> extends MessageToMessageDecoder<String, T> {
private static final Gson GSON = new GsonBuilder().create();
private final Class<T> clazz;
public JsonDecoder(Class<T> clazz, Class<?>... acceptedMsgTypes) {
super(acceptedMsgTypes);
this.clazz = checkNotNull(clazz);
}
@Override
public T decode(ChannelHandlerContext ctx, String msg) throws Exception {
return GSON.fromJson(msg, clazz);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment