Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Serialization and reverse order of MessagePack Java Jackson Dataformat-POJO

Shulou Source: shulou.com Published: 2022-06-03 07:06:29 10月04日 Update

In this test code, we define a POJO class called MessageData, which you can find at the link below.

https://github.com/cwiki-us-demo/serialize-deserialize-demo-java/blob/master/src/main/java/com/insight/demo/serialize/model/msgpack/MessageData.java

Serialization and deserialization of POJO

You simply instantiate MessagePackFactory and pass parameters to the constructor of com.fasterxml.jackson.databind.ObjectMapper.

You can then format the data using Message Pack in the same way as jackson-databind.

This test method can be found at https://github.com/cwiki-us-demo/serialize-deserialize-demo-java/blob/master/src/test/java/com/insight/demo/serialize/MessagePackSerializer.java

/** * SerializationPOJO */@Testpublic void testMessagePackSerializationPOJO() { byte[] bytes = new byte[0]; String uuid = UUID.randomUUID().toString(); // Instantiate ObjectMapper for MessagePack ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); MessageData pojo = new MessageData(); pojo.setUuid(uuid); pojo.setName("CWIKI.US"); try { // Serialize a Java object to byte array bytes = objectMapper.writeValueAsBytes(pojo); logger.debug("Length of Bytes: [{}]", bytes.length); // Deserialize the byte array to a Java object MessageData deserialized = objectMapper.readValue(bytes, MessageData.class); logger.debug("Deserialized Name: [{}]", deserialized.name); assertEquals("CWIKI.US", deserialized.name); } catch (JsonProcessingException ex) { logger.error("Serialize Error", ex); } catch (IOException e) { e.printStackTrace(); } }

https://www.cwiki.us/display/Serialization/MessagePack+Jackson+Dataformat

Tags: Methods sequences tests identical code parameters names instances data formats links facets related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple MySQL Shulou Technology Microsoft