JacksonDecoder.java
1011 字节
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.waylau.netty.demo.codec.jackcon;
import java.util.List;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
/**
* 说明:Jackson json 解码器
*
* @author <a href="http://www.waylau.com">waylau.com</a> 2015年11月9日
*/
public class JacksonDecoder<T> extends ByteToMessageDecoder {
private final Class<T> clazz;
/**
*
*/
public JacksonDecoder(Class<T> clazz) {
this.clazz = clazz;
}
/*
* (non-Javadoc)
*
* @see io.netty.handler.codec.ByteToMessageDecoder#decode(io.netty.channel.
* ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)
*/
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in,
List<Object> out) throws Exception {
ByteBufInputStream byteBufInputStream = new ByteBufInputStream(in);
out.add(JacksonMapper.getInstance().readValue(byteBufInputStream, clazz));
}
}