@Test public void testEncode1() throws Exception { SwallowMessage msg = new SwallowMessage(); msg.setGeneratedTime(new Date()); msg.setMessageId(123L); HashMap<String, String> map = new HashMap<String, String>(); map.put("property-key", "property-value"); msg.setContent("content"); JsonEncoder jsonEncoder = new JsonEncoder(SwallowMessage.class); ChannelBuffer channelBuffer = (ChannelBuffer) jsonEncoder.encode(null, null, msg); JsonBinder jsonBinder = JsonBinder.getNonEmptyBinder(); SwallowMessage actualMsg = jsonBinder.fromJson(channelBuffer.toString(Charset.forName("UTF-8")), SwallowMessage.class); Assert.assertEquals(msg, actualMsg); }
@Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { if (msg.getClass() == clazz) { JsonBinder jsonBinder = JsonBinder.getNonEmptyBinder(); String json = jsonBinder.toJson(msg); byte[] jsonBytes = json.getBytes(Charset.forName("UTF-8")); ChannelBuffer channelBuffer = ChannelBuffers.buffer(jsonBytes.length); channelBuffer.writeBytes(jsonBytes); return channelBuffer; } return msg; }
JsonEncoder extends OneToOneEncoder { @Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { if (msg.getClass() == clazz) { JsonBinder jsonBinder = JsonBinder.getNonEmptyBinder(); String json = jsonBinder.toJson(msg); byte[] jsonBytes = json.getBytes(Charset.forName("UTF-8")); ChannelBuffer channelBuffer = ChannelBuffers.buffer(jsonBytes.length); channelBuffer.writeBytes(jsonBytes); return channelBuffer; } return msg; } }
JsonEncoder extends OneToOneEncoder { @Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { if (msg.getClass() == clazz) { JsonBinder jsonBinder = JsonBinder.getNonEmptyBinder(); String json = jsonBinder.toJson(msg); byte[] jsonBytes = json.getBytes(Charset.forName("UTF-8")); ChannelBuffer channelBuffer = ChannelBuffers.buffer(jsonBytes.length); channelBuffer.writeBytes(jsonBytes); return channelBuffer; } return msg; } JsonEncoder(Class clazz); }
JsonEncoder extends OneToOneEncoder { @Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { if (msg.getClass() == clazz) { JsonBinder jsonBinder = JsonBinder.getNonEmptyBinder(); String json = jsonBinder.toJson(msg); byte[] jsonBytes = json.getBytes(Charset.forName("UTF-8")); ChannelBuffer channelBuffer = ChannelBuffers.buffer(jsonBytes.length); channelBuffer.writeBytes(jsonBytes); return channelBuffer; } return msg; } JsonEncoder(Class clazz);  }
JsonEncoder extends OneToOneEncoder { @Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { if (msg.getClass() == clazz) { JsonBinder jsonBinder = JsonBinder.getNonEmptyBinder(); String json = jsonBinder.toJson(msg); byte[] jsonBytes = json.getBytes(Charset.forName("UTF-8")); ChannelBuffer channelBuffer = ChannelBuffers.buffer(jsonBytes.length); channelBuffer.writeBytes(jsonBytes); return channelBuffer; } return msg; } JsonEncoder(Class clazz);   }