-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Binary event #6
Comments
As for MessagePack Java Implementation, msgpack-java looks the best. It also provides Jackson extension, msgpack-jackson. Map<String, Object> obj = new LinkedHashMap<>();
obj.put("text", "Donghwan Kim");
obj.put("binary", "Donghwan Kim".getBytes());
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
byte[] bytes = objectMapper.writeValueAsBytes(obj);
System.out.println(Arrays.toString(bytes));
System.out.println(objectMapper.readValue(bytes, Map.class));
|
The second task is about how a given object should be serialized into either text message (JSON) or binary message (MessagePack). First public static class TestBean {
// Getters and setters are skipped
public String string = "100";
public int number = 100;
public Map<String, Object> object = new LinkedHashMap();
public byte[] byteArray = new byte[]{-126, -92, 116, 101, 120, 116, -84, 68, 111, 110, 103, 104,
119, 97, 110, 32, 75, 105, 109, -90, 98, 105, 110, 97, 114, 121, -60, 12, 68, 111, 110,
103, 104, 119, 97, 110, 32, 75, 105, 109};
public ByteBuffer byteBuffer = ByteBuffer.wrap("small".getBytes());
{
{
object.put("integer", 300);
object.put("bytes", ByteBuffer.wrap("big".getBytes()));
}
}
}
ObjectMapper mapper = new ObjectMapper();
Map map = mapper.convertValue(new TestBean(), Map.class);
System.out.println(map);
Second to determine if a given object contains binary (if so, it should be encoded to binary), it's required to traverse the given object. Using ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
// Of course thread safety is not required here
final AtomicBoolean containsBinary = new AtomicBoolean();
module.addSerializer(byte[].class, new ByteArraySerializer() {
@Override
public void serialize(byte[] bytes, JsonGenerator gen, SerializerProvider provider) throws
IOException {
containsBinary.set(true);
super.serialize(bytes, gen, provider);
}
});
module.addSerializer(ByteBuffer.class, new ByteBufferSerializer() {
@Override
public void serialize(ByteBuffer bytes, JsonGenerator gen, SerializerProvider provider)
throws IOException {
containsBinary.set(true);
super.serialize(bytes, gen, provider);
}
});
mapper.registerModule(module);
Map map = mapper.convertValue(new TestBean(), Map.class);
System.out.println(map);
System.out.println(containsBinary);
If it turns out that it has binary, |
…n format msgpack-lite that is one of dependencies of cettia-javascript-client encodes typed arrays to ext format. But in Java they should be decoded as bin format. Refs #6
Derived from cettia/cettia-protocol#9
The text was updated successfully, but these errors were encountered: