先看官方的定義:MessagePack是一種高效的二進制序列化格式。它容許您像JSON同樣在多個語言之間交換數據。可是,它更快而且更小。html
從官方定義中,能夠有以下的結論:java
MessagePack是一個二進制序列化格式,於是它序列化的結果能夠在多個語言間進行數據的交換。git
從性能上講,它要比json的序列化格式要好。github
從結果大小上講,它要比json的序列化結果要小。json
可是官方並無提MessagePack和google pb的對比,實際上從空間和時間兩個方面對比,pb均要優於MessagePack,但pb相對MessagePack 的缺點是支持的語言種類比較少,須要編寫專門的 .proto文件,使用上沒有MessagePack方便。數組
format name
|
first byte (in binary)
|
first byte (in hex)
|
nil
|
11000000
|
0xc0
|
false
|
11000010
|
0xc2
|
true
|
11000011
|
0xc3
|
uint 8 stores a 8-bit unsigned integer +--------+--------+ | 0xcc |ZZZZZZZZ| +--------+--------+ uint 16 stores a 16-bit big-endian unsigned integer +--------+--------+--------+ | 0xcd |ZZZZZZZZ|ZZZZZZZZ| +--------+--------+--------+ int 8 stores a 8-bit signed integer +--------+--------+ | 0xd0 |ZZZZZZZZ| +--------+--------+ int 16 stores a 16-bit big-endian signed integer +--------+--------+--------+ | 0xd1 |ZZZZZZZZ|ZZZZZZZZ| +--------+--------+--------+
str 8 stores a byte array whose length is upto (2^8)-1 bytes: +--------+--------+========+ | 0xd9 |YYYYYYYY| data | +--------+--------+========+ str 16 stores a byte array whose length is upto (2^16)-1 bytes: +--------+--------+--------+========+ | 0xda |ZZZZZZZZ|ZZZZZZZZ| data | +--------+--------+--------+========+
array 16 stores an array whose length is upto (2^16)-1 elements: +--------+--------+--------+~~~~~~~~~~~~~~~~~+ | 0xdc |YYYYYYYY|YYYYYYYY| N objects | +--------+--------+--------+~~~~~~~~~~~~~~~~~+ array 32 stores an array whose length is upto (2^32)-1 elements: +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+ | 0xdd |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ| N objects | +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+
source types
|
output format
|
Integer
|
int format family (positive fixint, negative fixint, int 8/16/32/64 or uint 8/16/32/64)
|
Nil
|
nil
|
Boolean
|
bool format family (false or true)
|
Float
|
float format family (float 32/64)
|
String
|
str format family (fixstr or str 8/16/32)
|
Binary
|
bin format family (bin 8/16/32)
|
Array
|
array format family (fixarray or array 16/32)
|
Map
|
map format family (fixmap or map 16/32)
|
Extension
|
ext format family (fixext or ext 8/16/32)
|
source formats
|
output type
|
positive fixint, negative fixint, int 8/16/32/64 and uint 8/16/32/64
|
Integer
|
nil
|
Nil
|
false and true
|
Boolean
|
float 32/64
|
Float
|
fixstr and str 8/16/32
|
String
|
bin 8/16/32
|
Binary
|
fixarray and array 16/32
|
Array
|
fixmap map 16/32
|
Map
|
fixext and ext 8/16/32
|
Extension
|
public class MessagePackSerializationCompareJson { @Message // Annotation public static class MyMessage { // public fields are serialized. public boolean compact; public int schema; public String toString() { return "compact:"+compact+";schema:"+schema; } } /** * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //初始化一個對象 MyMessage src = new MyMessage(); src.compact = true; src.schema=0; //利用MessagePack進行序列化 MessagePack msgpack = new MessagePack(); // Serialize byte[] bytes = msgpack.write(src); System.out.println("msgpack result length:"+bytes.length); //利用json進行序列化 String jsonResult = JSONObject.toJSON(src).toString(); System.out.println("json result length:"+jsonResult.getBytes().length); } }
json result length:27 msgpack result length:3
public class MessagePackSerializationCompareJson { @Message // Annotation public static class MyMessage { // public fields are serialized. public boolean compact; public int schema; public boolean link; public String toString() { return "compact:"+compact+";schema:"+schema+";link:"+link; } } /** * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //初始化一個對象 MyMessage src = new MyMessage(); src.compact = true; src.schema=0; //利用json進行序列化 String jsonResult = JSONObject.toJSON(src).toString(); System.out.println("json result length:"+jsonResult.getBytes().length); //利用MessagePack進行序列化 MessagePack msgpack = new MessagePack(); // Serialize byte[] bytes = msgpack.write(src); System.out.println("msgpack result length:"+bytes.length); } }
json result length:40 msgpack result length:4
public class SimpleMessagePackPractice { @Message // Annotation public static class MyMessage { // public fields are serialized. public boolean compact; public boolean link; public String toString() { return "link:" + link + ";compact:" + compact; } } @Message // Annotation public static class MyMessage2 { // public fields are serialized. public boolean link; public boolean compact; public String toString() { return "link:" + link + ";compact:" + compact; } } /** * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //初始化一個對象 MyMessage src = new MyMessage(); src.compact = true; src.link = false; //利用MessagePack進行序列化 MessagePack msgpack = new MessagePack(); // Serialize byte[] bytes = msgpack.write(src); //利用MessagePack進行反序列化 MyMessage2 dst = msgpack.read(bytes, MyMessage2.class); System.out.println("msgpack 原始數據:" + src); System.out.println("msgpack 反序列化:" + dst); } }
msgpack 原始數據:link:false;compact:true msgpack 反序列化:link:true;compact:false
public class SimpleMessagePackPractice { @Message // Annotation public static class MyMessage { // public fields are serialized. public boolean compact; public String link; public String toString() { return "link:" + link + ";compact:" + compact; } } @Message // Annotation public static class MyMessage2 { // public fields are serialized. public String link; public boolean compact; public String toString() { return "link:" + link + ";compact:" + compact; } } /** * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //初始化一個對象 MyMessage src = new MyMessage(); src.compact = true; src.link = "www.baidu.com"; //利用MessagePack進行序列化 MessagePack msgpack = new MessagePack(); // Serialize byte[] bytes = msgpack.write(src); //利用MessagePack進行反序列化 MyMessage2 dst = msgpack.read(bytes, MyMessage2.class); System.out.println("msgpack 原始數據:" + src); System.out.println("msgpack 反序列化:" + dst); } }
Exception in thread "main" org.msgpack.MessageTypeException: Expected raw value, but got boolean at org.msgpack.unpacker.Accept.acceptBoolean(Accept.java:33) at org.msgpack.unpacker.MessagePackUnpacker.readOneWithoutStackLarge(MessagePackUnpacker.java:154) at org.msgpack.unpacker.MessagePackUnpacker.readOneWithoutStack(MessagePackUnpacker.java:139) at org.msgpack.unpacker.MessagePackUnpacker.readOne(MessagePackUnpacker.java:73) at org.msgpack.unpacker.MessagePackUnpacker.readString(MessagePackUnpacker.java:502) at org.msgpack.template.StringTemplate.read(StringTemplate.java:46) at org.msgpack.template.StringTemplate.read(StringTemplate.java:25) at org.msgpack.template.AbstractTemplate.read(AbstractTemplate.java:31) at com.my.msgpack.SimpleMessagePackPractice$MyMessage2_$$_Template_1305193908_1.read(SimpleMessagePackPractice$MyMessage2_$$_Template_1305193908_1.java) at org.msgpack.template.AbstractTemplate.read(AbstractTemplate.java:31) at org.msgpack.MessagePack.read(MessagePack.java:388) at org.msgpack.MessagePack.read(MessagePack.java:371) at com.my.msgpack.SimpleMessagePackPractice.main(SimpleMessagePackPractice.java:61)