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 serialization and deserialization of multiple types of variables (serializati

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

Share

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

The Packer/Unpacker class allows serialization and deserialization of many types of variables, as shown in the following program. This class enables serialization and deserialization of multiple types of variables in a similar way to serializing primary type variables as well as wrapper classes, String objects, byte[] objects, ByteBuffer objects, etc.

As indicated above, you can serialize and deserialize your own objects, provided that your own objects require the @Message annotation.

package com.insight.demo.msgpack;import org.junit.Test;import org.msgpack.MessagePack;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.math.BigInteger;import java.nio.ByteBuffer;/** * MessagePack6Types * * @author yhu */public class MessagePack6Types { final Logger logger = LoggerFactory.getLogger(MessagePack6Types.class); /** * Test MessagePack6Types */ @Test public void testMessagePack6Types() { logger.debug("testMessagePack6Types for Types"); MessagePack msgpack = new MessagePack(); try { // // Serialization // ByteArrayOutputStream out = new ByteArrayOutputStream(); Packer packer = msgpack.createPacker(out); // Serialize values of primitive types packer.write(true); // boolean value packer.write(10); // int value packer.write(10.5); // double value // Serialize objects of primitive wrapper types packer.write(Boolean.TRUE); packer.write(new Integer(10)); packer.write(new Double(10.5)); // Serialize various types of arrays packer.write(new int[]{1, 2, 3, 4}); packer.write(new Double[]{10.5, 20.5}); packer.write(new String[]{"msg", "pack", "for", "java"}); packer.write(new byte[]{0x30, 0x31, 0x32}); // byte array // Serialize various types of other reference values packer.write("MessagePack"); // String object packer.write(ByteBuffer.wrap(new byte[]{0x30, 0x31, 0x32})); // ByteBuffer object packer.write(BigInteger.ONE); // BigInteger object // // Deserialization // byte[] bytes = out.toByteArray(); ByteArrayInputStream in = new ByteArrayInputStream(bytes); Unpacker unpacker = msgpack.createUnpacker(in); // to primitive values boolean b = unpacker.readBoolean(); // boolean value int i = unpacker.readInt(); // int value double d = unpacker.readDouble(); // double value // to primitive wrapper value Boolean wb = unpacker.read(Boolean.class); Integer wi = unpacker.read(Integer.class); Double wd = unpacker.read(Double.class); // to arrays int[] ia = unpacker.read(int[].class); Double[] da = unpacker.read(Double[].class); String[] sa = unpacker.read(String[].class); byte[] ba = unpacker.read(byte[].class); // to String object, ByteBuffer object, BigInteger object, List object and Map object String ws = unpacker.read(String.class); ByteBuffer buf = unpacker.read(ByteBuffer.class); BigInteger bi = unpacker.read(BigInteger.class); } catch (Exception ex) { logger.error("MessagePack Serialization And Deserialization error", ex); } }}

The Packer#write() method allows serialization of multiple types of data.

The class Unpacker provides a deserialization method for deserializing binary data as the primary variable. For example, if you want to deserialize binary data to boolean (or int) data types, you can use the readBoolean (or readInt) method in Unpacker.

Unpacker also provides a method for reading reference variables. This method allows deserialization from binary data for a reference variable. The definition of a reference variable is that you specify the type as a parameter. For example, if you want to deserialize binary data into a String (or byte[]) object, you must define the description when calling the read(String.class) (or read(byte[].class)) method.

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