Serialization and deserialization of multiple types of variables in MessagePack Java 0.6.X

Keywords: Java Junit

Class Packer/Unpacker allows serialization and deserialization of multiple types of variables, as shown in subsequent programs. This class enables serialization and deserialization of multiple types of variables and serialization of major types of variables as well as wrapping classes, String objects, byte [] objects, ByteBuffer objects, and so on.

As mentioned above, you can serialize and deserialize your own objects, provided that your own objects need to be annotated with @Message.

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.math.BigInteger;
import java.nio.ByteBuffer;

 * MessagePack6Types
 * @author yhu
public class MessagePack6Types {
    final Logger logger = LoggerFactory.getLogger(MessagePack6Types.class);

     * Test MessagePack6Types
    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(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 =;
            Integer wi =;
            Double wd =;

            // to arrays
            int[] ia =[].class);
            Double[] da =[].class);
            String[] sa =[].class);
            byte[] ba =[].class);

            // to String object, ByteBuffer object, BigInteger object, List object and Map object
            String ws =;
            ByteBuffer buf =;
            BigInteger bi =;

        } catch (Exception ex) {
            logger.error("MessagePack Serialization And Deserialization error", ex);

Method Packer write () allows serialization of multiple types of data.

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

Unpacker also provides a way to read reference variables. This method allows deserialization of a reference variable from binary data. Reference variables are defined as you specify a type as a parameter. For example, if you want to deserialize binary data to a String (or byte []) object, you must define a description when calling a read(String.class) method (or a read (byte []. class).

Posted by spaggle on Wed, 09 Oct 2019 04:40:34 -0700