http://blog.csdn.net/sunning9001/article/details/50420290
MessagePack介绍:
MessagePack is an efficient binary serialization format.It lets you exchange data among multiple languages like JSON. But it's faster and smaller.
Small integers are encoded into a single byte, and typical short strings require only one extra byte in addition to the strings themselves.
支持语言:
MessagePack is supported by over 50 programming languages and environments.
Smalltalk ActionScript3 PHP Lua Rust Elixir Scheme C++11 Go mruby Elixir Rails Python Julia Clojure Clojure SML C Dart HHVM F# Jackson-dataformaSwift Objective-C Node Haskell Pascal Delphi Qt Go Shell C# Elixir Scala Python/Twisted J Swift Nim C Rust Nim Crystal JavaScript UNIX PHP Swift C#
1-使用@Message 可以标记被序列化的类。类中所有成员都会被序列化。
@Message
public class SerializeObject
{
private int i;
private String str;
public short s;
public long l;
public SerializeObject(int i, String str, short s, long l) {
super();
this.i = i;
this.str = str;
this.s = s;
this.l = l;
}
}
MessagePack msgpack = new MessagePack();
SerializeObject src = new SerializeObject(1, "str", (short) 2, 3);
// Serialize
byte[] bytes = msgpack.write(src);
// Deserialize
SerializeObject dst = msgpack.read(bytes, SerializeObject.class);
System.out.println(dst.toString());
@Message
public class NewSerializeObject extends SerializeObject {
@Optional
private String nstr = "";
public String getNstr() {
return nstr;
}
public void setNstr(String nstr) {
this.nstr = nstr;
}
@Override
public String toString() {
return "NewSerializeObject [nstr=" + nstr + "]";
}
}
MessagePack msgpack = new MessagePack();
SerializeObject src = new SerializeObject(1, "str", (short) 2, 3);
// Serialize
byte[] bytes = msgpack.write(src);
// Deserialize
SerializeObject dst = msgpack.read(bytes, SerializeObject.class);
System.out.println(dst.toString());
//使用@optional注解,来升级序列化对象
NewSerializeObject newDst = msgpack.read(bytes, NewSerializeObject.class);
System.out.println(newDst.toString());
3- 序列化list数组
MessagePack msgpack = new MessagePack();
// Create templates for serializing/deserializing List and Map objects
//使用template来序列化List数组。
Template<List<String>> listTmpl = Templates.tList(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
//
// Deserialization
//
byte[] bytes = out.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
Unpacker unpacker = msgpack.createUnpacker(in);
// to List object
List<String> dstList = unpacker.read(listTmpl);
System.out.println(dstList);
4- 序列化map对象
MessagePack msgpack = new MessagePack();
// Create templates for serializing/deserializing List and Map objects
//使用template来序列化map对象
Template<Map<String, String>> mapTmpl = Templates.tMap(Templates.tString(), Templates.tString());
//
// Serialization
//
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = msgpack.createPacker(out);
// 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
// to Map object
Map<String, String> dstMap = unpacker.read(mapTmpl);
System.out.println(dstMap);
5 -MessagePack 不支持对Object对象的序列化,但是jackson-dataformat-msgpack 提供对messagepack格式的序列化和反序列化。
This Jackson extension library handles reading and writing of data encoded in MessagePack data format.
public class ExamplePojo {
private String name;
public ExamplePojo(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ExamplePojo() {
}
}
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
ExamplePojo orig = new ExamplePojo("komamitsu");
byte[] bytes = objectMapper.writeValueAsBytes(orig);
ExamplePojo value = objectMapper.readValue(bytes, ExamplePojo.class);
System.out.println(value.getName()); // => komamitsu