在本测试代码中,我们定义了一个 POJO 类,名字为 MessageData,你可以访问下面的链接找到有关这个类的定义。
https://github.com/cwiki-us-demo/serialize-deserialize-demo-java/blob/master/src/main/java/com/insight/demo/serialize/model/msgpack/MessageData.java
本测试方法,可以在https://github.com/cwiki-us-demo/serialize-deserialize-demo-java/blob/master/src/test/java/com/insight/demo/serialize/MessagePackSerializer.java中找到。
我们需要先定义一个对象的 List 列表。
/**
* SerializationList
*/
@Test
public void testMessagePackSerializationList() {
byte[] bytes = new byte[0];
String uuid = UUID.randomUUID().toString();
// Instantiate ObjectMapper for MessagePack
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
List
try {
// Serialize a Java object to byte array
bytes = objectMapper.writeValueAsBytes(objList);
logger.debug("Length of Bytes: [{}]", bytes.length);
// Deserialize the byte array to a Java object
// Deserialize the byte array to a List
List>() {
});
logger.debug("Deserialized List Count: [{}]", deserialized.size());
logger.debug("List index 0: [{}]", deserialized.get(0).name);
} catch (JsonProcessingException ex) {
logger.error("Serialize Error", ex);
} catch (IOException e) {
e.printStackTrace();
}
}
https://www.cwiki.us/display/Serialization/MessagePack+Jackson+Dataformat