MessagePack Java 0.6.X 多種類型變量的序列化和反序列化(serialization/deserialization)

類 Packer/Unpacker 容許序列化和反序列化多種類型的變量,如後續程序所示。這個類啓用序列化和反序列化多種類型的變量和序列化主要類型變量以及包裝類,String 對象,byte[] 對象, ByteBuffer 對象等的方法類似。java

如上面提示的,你能夠序列化和反序列化你本身的對象,前提是你本身的對象須要使用 @Message 註解。app

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);
        }
    }
}

方法 Packer#write() 容許序列化多種類型的數據。ui

類 Unpacker 針對反序列化二進制數據爲主要變量,提供了一個反序列化方法。例如,你但願將二進制數據反序列化爲 boolean (或者 int) 數據類型,你可使用 Unpacker 中的 readBoolean (或者 readInt) 方法。.net

Unpacker 同時也爲參考變量提供了一個讀取的方法。這個方法容許爲一個參考變量從二進制數據中進行反序列化。參考變量的定義爲你將類型指定爲一個參數。例如,你但願反序列化二進制數據到 String (或者 byte[]) 對象,你必須在調用 read(String.class) (或者 read(byte[].class)) 方法的時候定義描述。debug

 

https://www.cwiki.us/display/Serialization/QuickStart+For+MessagePack+Java+0.6.Xcode

相關文章
相關標籤/搜索