MessagePack 是一个高效的二进制序列化框架,它像json一样支持不同语言间的数据交换,但是它的性能更快,序列化之后的码流也更小
介绍
MessagePack 特点如下:
- 编解码高效,性能高
- 序列化之后的码流小
- 支持跨语言
多语言支持
官方支持的语言如下:Java、Python、Ruby、Haskell、C#、OCaml、Lua、Go、C、C++等。
netty 对象编解码实例
MessagePack 解码器
public class MsgpackEncoder extends MessageToByteEncoder<Object>
{
@Override
protected void encode(ChannelHandlerContext ctx , Object o , ByteBuf byteBuf) throws Exception
{
MessagePack msgpack=new MessagePack();
byte[] raw=msgpack.write(o);
byteBuf.writeBytes(raw);
}
}
MessagePack 译码器
public class MsgpackDecoder extends MessageToMessageDecoder<ByteBuf>
{
@Override
protected void decode(ChannelHandlerContext ctx , ByteBuf byteBuf ,