MessagePack初探
MessagePack is an efficient binary serialization format.
MessagePack版本-0.6.11
实例一:把一个Java对象序列化
@Message // Annotation public static class MyMessage { // public fields are serialized. public String name; public double version; } @Test public void test977() throws IOException { MyMessage src = new MyMessage(); src.name = "msgpack"; src.version = 0.6; MessagePack msgpack = new MessagePack(); // Serialize byte[] bytes = msgpack.write(src); // Deserialize MyMessage dst = msgpack.read(bytes, MyMessage.class); System.out.println(dst.name); System.out.println(dst.version); }
在这里使用的是MessagePack类,注意被序列化的类要使用注解@Message
实例二:序列化多个Java对象
@Message // Annotation public static class MyMessage { // public fields are serialized. public String name; public double version; } @Test public void test8666() throws IOException { MyMessage src1 = new MyMessage(); src1.name = "msgpack"; src1.version = 0.6; MyMessage src2 = new MyMessage(); src2.name = "muga"; src2.version = 10.0; MyMessage src3 = new MyMessage(); src3.name = "frsyukik"; src3.version = 1.0; MessagePack msgpack = new MessagePack(); // // Serialize // ByteArrayOutputStream out = new ByteArrayOutputStream(); Packer packer = msgpack.createPacker(out); packer.write(src1); packer.write(src2); packer.write(src3); byte[] bytes = out.toByteArray(); // // Deserialize // ByteArrayInputStream in = new ByteArrayInputStream(bytes); Unpacker unpacker = msgpack.createUnpacker(in); MyMessage dst1 = unpacker.read(MyMessage.class); MyMessage dst2 = unpacker.read(MyMessage.class); MyMessage dst3 = unpacker.read(MyMessage.class); // print dst1 System.out.println(dst1.name); System.out.println(dst1.version); // print dst2 System.out.println(dst2.name); System.out.println(dst2.version); // print dst2 System.out.println(dst3.name); System.out.println(dst3.version); }
使用Packer 和Unpacker 序列化多个Java对象
实例三:多种数据类型(包括基本类型和基本类型的封装类型)的序列化和反序列化
@Test public void test444() throws IOException { MessagePack msgpack = new MessagePack(); // // 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("MesagePack"); // 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 System.out.println(b); int i = unpacker.readInt(); // int value System.out.println(i); double d = unpacker.readDouble(); // double value System.out.println(d); // to primitive wrapper value Boolean wb = unpacker.read(Boolean.class); System.out.println(wb); Integer wi = unpacker.read(Integer.class); System.out.println(wi); Double wd = unpacker.read(Double.class); System.out.println(wd); // to arrays int[] ia = unpacker.read(int[].class); System.out.println(ia); Double[] da = unpacker.read(Double[].class); System.out.println(da); String[] sa = unpacker.read(String[].class); System.out.println(sa); byte[] ba = unpacker.read(byte[].class); System.out.println(ba); // to String object, ByteBuffer object, BigInteger object, List object and Map object String ws = unpacker.read(String.class); System.out.println(ws); ByteBuffer buf = unpacker.read(ByteBuffer.class); BigInteger bi = unpacker.read(BigInteger.class); System.out.println(bi); }
实例四:List和Map类型的序列化和反序列化
@Test public void test8655() throws IOException { MessagePack msgpack = new MessagePack(); // Create templates for serializing/deserializing List and Map objects Template<List<String>> listTmpl = Templates.tList(Templates.TString); Template<Map<String, String>> mapTmpl = Templates.tMap(Templates.TString, Templates.TString); // // Serialization // ByteArrayOutputStream out = new ByteArrayOutputStream(); Packer packer = msgpack.createPacker(out); // Serialize List object List<String> list = new ArrayList<String>(); list.add("msgpack"); list.add("for"); list.add("java"); packer.write(list); // List object // Serialize Map object Map<String, String> map = new HashMap<String, String>(); map.put("sadayuki", "furuhashi"); map.put("muga", "nishizawa"); packer.write(map); // Map object // // Deserialization // byte[] bytes = out.toByteArray(); ByteArrayInputStream in = new ByteArrayInputStream(bytes); Unpacker unpacker = msgpack.createUnpacker(in); // to List object List<String> dstList = unpacker.read(listTmpl); // to Map object Map<String, String> dstMap = unpacker.read(mapTmpl); }
实例五:不使用注解@Message进行序列化对象
public static class MyMessage3 { public String name; public double version; } @Test public void test86666() throws IOException { MyMessage3 src = new MyMessage3(); src.name = "msgpack"; src.version = 0.6; MessagePack msgpack = new MessagePack(); msgpack.register(MyMessage3.class); //当没有使用注解@Message byte[] bytes = msgpack.write(src); MyMessage3 dest = msgpack.read(bytes, MyMessage3.class); System.out.println(dest.name); System.out.println(dest.version); }
实例六:使用@Optional注解标注新添加的字段
@Message // Annotation public static class MyMessage { // public fields are serialized. public String name; public double version; } @Message public static class MyMessage2 { public String name; public double version; // new field @Optional public int flag = 0; } @Test public void test90890() throws IOException { MyMessage2 src = new MyMessage2(); src.name = "msgpack"; src.version = 0.6; src.flag = 1; MessagePack msgpack = new MessagePack(); // Serialize byte[] bytes = msgpack.write(src); //使用原有的版本反序列化,会忽略optional注解的字段 MyMessage dst = msgpack.read(bytes, MyMessage.class); System.out.println(dst.name); System.out.println(dst.version); }
实例七:动态的确定序列化的类型
主要是使用类库提供的Value对象
@Test public void test765f4f() throws IOException { // Create serialize objects. List<String> src = new ArrayList<String>(); src.add("msgpack"); src.add("kumofs"); src.add("viver"); MessagePack msgpack = new MessagePack(); // Serialize byte[] raw = msgpack.write(src); // Deserialize directly using a template List<String> dst1 = msgpack.read(raw, Templates.tList(Templates.TString)); // Or, Deserialze to Value then convert type. Value dynamic = msgpack.read(raw); System.out.println(dynamic.isArrayValue()); List<String> dst2 = new Converter(dynamic).read(Templates.tList(Templates.TString)); }
在这里我还有些疑问,如何动态的确定序列化类型???
List<String> dst2 = new Converter(dynamic).read(Templates.tList(Templates.TString));
这行代码命名还是指定了序列化类型,怎么是动态确定的呢???
其实我看在应用中传递convert对象,再在具体的处理方法中序列化成相应的类型。(具体的方法可以确定要序列化的类型)
@Test public void test765f4f() throws IOException { // Create serialize objects. List<String> src = new ArrayList<String>(); src.add("msgpack"); src.add("kumofs"); src.add("viver"); MessagePack msgpack = new MessagePack(); // Serialize byte[] raw = msgpack.write(src); // Deserialize directly using a template List<String> dst1 = msgpack.read(raw, Templates.tList(Templates.TString)); // Or, Deserialze to Value then convert type. Value dynamic = msgpack.read(raw); System.out.println(dynamic.isArrayValue()); Converter converter = new Converter(dynamic); List<String> dst2 = converter.read(Templates.tList(Templates.TString)); }
====END====