测试序列化类型

This commit is contained in:
YuCheng Hu 2019-08-06 23:29:36 -04:00
parent de4476e04d
commit 3df4f9dee2
1 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,93 @@
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);
}
}
}