forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonRequestHandler.java
More file actions
28 lines (22 loc) · 835 Bytes
/
Copy pathJsonRequestHandler.java
File metadata and controls
28 lines (22 loc) · 835 Bytes
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
package com.github.dockerjava.netty.handler;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Handler that encodes an outgoing object to JSON.
*
* @author Marcus Linke
*/
public class JsonRequestHandler extends MessageToByteEncoder<Object> {
private ObjectMapper mapper = new ObjectMapper();
public JsonRequestHandler() {
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
}
@Override
protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
byte[] serialized = mapper.writeValueAsBytes(msg);
out.writeBytes(serialized);
}
}