package com.docus.server.common;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.io.Serializable;

public class JsonEncoder extends MessageToByteEncoder<CommMsg<Serializable>> {

    @Override
    protected void encode(ChannelHandlerContext ctx, CommMsg<Serializable> user, ByteBuf buf) throws Exception {
        if (null != user) {
            String json = JSON.toJSONString(user);
            ctx.writeAndFlush(Unpooled.wrappedBuffer(json.getBytes()));
        }
    }
}
