MessagePack Java Jackson Dataformat - 列表(List)的序列化和反序列化

阅读更多

在本测试代码中,我们定义了一个 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 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 deserialized = objectMapper.readValue(bytes, new TypeReference>() {
        });
        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

你可能感兴趣的:(MessagePack Java Jackson Dataformat - 列表(List)的序列化和反序列化)