在本测试代码中,我们定义了一个 POJO 类,名字为 MessageData,你可以访问下面的链接找到有关这个类的定义。
我们需要先定义一个对象的 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<MessageData> objList = MockDataUtils.getMessageDataList(9); 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<MessageData> deserialized = objectMapper.readValue(bytes, new TypeReference<List<MessageData>>() {
});
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