Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MessagePack Java 0.6.x uses a message packaging (message-packable) class

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Use the annotation @ Message to allow you to serialize the public fields of objects in your own class.

This code can be downloaded to the local https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Object.java for compilation and testing.

Package com.insight.demo.msgpack;import org.junit.Test;import org.msgpack.MessagePack;import org.msgpack.annotation.Message;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.UUID;import static org.junit.Assert.assertEquals;/** * MessagePack6Objects * * @ author yhu * / public class MessagePack6Object {final Logger logger = LoggerFactory.getLogger (MessagePack6Object.class) / * MessageData Message Object * / @ Message / / Annotation public static class MessageData {/ / public fields are serialized. Public String uuid; public String name; public double version;} / * Test MessagePack6Objects * / @ Test public void testMessagePack6Objects () {logger.debug ("MessagePack6Objects for Objects"); String uuid = UUID.randomUUID () .toString (); / / INIT OBJ MessageData src = new MessageData (); src.uuid = uuid; src.name = "MessagePack6" Src.version = 0.6; try {MessagePack msgPack = new MessagePack (); / / Serialization logger.debug ("- Serialization -"); byte [] bytes = msgPack.write (src); logger.debug ("Bytes Array Length: [{}]", bytes.length) / / Deserialization logger.debug ("- Deserialization -"); MessageData dst = msgPack.read (bytes, MessageData.class); logger.debug ("Check Object for UUID: [{}]", dst.uuid); assertEquals (uuid, dst.uuid);} catch (Exception ex) {logger.error ("MessagePack Serialization And Deserialization error", ex) }}}

If you want to serialize multiple objects sequentially, you can use Packer and Unpacker multiple objects.

This is because MessagePack.write (Object) and read (byte []) actually create Packer and Unpacker objects every time they call.

To use the Packer and Unpacker objects, call createPacker (OutputStream) and createUnpacker (InputStream).

This code can be viewed in https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Objects.java.

Package com.insight.demo.msgpack;import org.junit.Test;import org.msgpack.MessagePack;import org.msgpack.annotation.Message;import org.msgpack.packer.Packer;import org.msgpack.unpacker.Unpacker;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.util.UUID;import static org.junit.Assert.assertEquals;/** * MessagePack6Objects * * @ author yhu * / public class MessagePack6Objects {final Logger logger = LoggerFactory.getLogger (MessagePack6Objects.class) / * MessageData Message Objects * / @ Message / / Annotation public static class MessageData {/ / public fields are serialized. Public String uuid; public String name; public double version;} / * Test MessagePack6Objects * / @ Test public void testMessagePack6Objects () {logger.debug ("MessagePack6Objects for Objects"); String uuid = UUID.randomUUID () .toString (); / / INIT OBJ MessageData src1 = new MessageData (); src1.uuid = uuid; src1.name = "MessagePack6-src1" Src1.version = 0.6; MessageData src2 = new MessageData (); src2.uuid = uuid; src2.name = "MessagePack6-src2"; src2.version = 10.6; MessageData src3 = new MessageData (); src3.uuid = uuid; src3.name = "MessagePack6-src3"; src3.version = 1.6; try {MessagePack msgPack = new MessagePack () / / Serialization logger.debug ("- Serialization -"); ByteArrayOutputStream out = new ByteArrayOutputStream (); Packer packer = msgPack.createPacker (out); packer.write (src1); packer.write (src2); packer.write (src3); byte [] bytes = out.toByteArray () Logger.debug ("Bytes Array Length: [{}]", bytes.length); / / Deserialization logger.debug ("- Deserialization -"); ByteArrayInputStream in = new ByteArrayInputStream (bytes); Unpacker unpacker = msgPack.createUnpacker (in); MessageData dst1 = unpacker.read (MessageData.class); MessageData dst2 = unpacker.read (MessageData.class) MessageData dst3 = unpacker.read (MessageData.class); logger.debug ("Check Object for UUID: [{}]", dst1.uuid); assertEquals (uuid, dst1.uuid);} catch (Exception ex) {logger.error ("MessagePack Serialization And Deserialization error", ex);}

Https://www.cwiki.us/display/Serialization/QuickStart+For+MessagePack+Java+0.6.X

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report