msgpack简介
MessagePack是一个基于二进制高效的对象序列化Library用于跨语言通信。
它可以像JSON那样,在许多种语言之间交换结构对象;但是它比JSON更快速也更轻巧。
支持Python、Ruby、Java、C/C++、Javascript等众多语言。
比Google Protocol Buffers还要快4倍
1. 下载 msgpack-0.5.1-devel.jar
建议从Maven仓库下 可能依赖的 jar
slf4j-api-1.4.3.jar
slf4j-log4j12-1.4.3.jar
log4j-1.2.13.jar
javassist-3.12.1.GA.jar
2. 对JavaBean操作
3. 对Map,List操作
MessagePack是一个基于二进制高效的对象序列化Library用于跨语言通信。
它可以像JSON那样,在许多种语言之间交换结构对象;但是它比JSON更快速也更轻巧。
支持Python、Ruby、Java、C/C++、Javascript等众多语言。
比Google Protocol Buffers还要快4倍
1. 下载 msgpack-0.5.1-devel.jar
建议从Maven仓库下 可能依赖的 jar
slf4j-api-1.4.3.jar
slf4j-log4j12-1.4.3.jar
log4j-1.2.13.jar
javassist-3.12.1.GA.jar
2. 对JavaBean操作
- package com.mytest;
- /**
- *
- * 功能描述:
- *
- * @author lw
- * @created 2011-6-9 下午01:55:29
- * @version 1.0.0
- * @date 2011-6-9 下午01:55:29
- */
- import java.io.Serializable;
- import org.msgpack.annotation.MessagePackMessage;
- @MessagePackMessage
- // Annotation
- public class BeanClass implements Serializable{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- // public fields are serialized.
- public String str;
- public double num;
- public String info;
- }
- package com.mytest;
- /**
- *
- * 功能描述:
- *
- * @author lw
- * @created 2011-6-9 下午01:55:29
- * @version 1.0.0
- * @date 2011-6-9 下午01:55:29
- */
- import java.util.HashMap;
- import org.msgpack.MessagePack;
- public class BeanMain {
- public static void main(String[] args) {
- BeanClass src = new BeanClass();
- src.str = "msgpack";
- src.num = 0.5;
- src.info = "This is msgpack";
- HashMap
map = new HashMap (); - map.put("src", "src----");
- // Serialize
- byte[] raw = MessagePack.pack(src);
- // Deserialize
- BeanClass dst = MessagePack.unpack(raw, BeanClass.class);
- System.out.println(dst.str + ":" + dst.num);
- }
- }
3. 对Map,List操作
- package com.mytest;
- import static org.msgpack.Templates.TAny;
- import static org.msgpack.Templates.TString;
- import static org.msgpack.Templates.tList;
- import static org.msgpack.Templates.tMap;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import org.msgpack.MessagePack;
- import org.msgpack.Template;
- import org.msgpack.object.ArrayType;
- import org.msgpack.object.BooleanType;
- import org.msgpack.object.FloatType;
- import org.msgpack.object.IntegerType;
- import org.msgpack.object.RawType;
- /**
- *
- * 功能描述:
- *
- * @author lw
- * @created 2011-6-13 上午09:33:11
- * @version 1.0.0
- * @date 2011-6-13 上午09:33:11
- */
- public class RawTypeUtil {
- /**
- *
- * Object
- *
- * @param arg0
- * @return
- */
- public static Object unpackToObject(byte[] arg0) {
- return unpackToObject(arg0, TAny);
- }
- /**
- *
- * Object
- *
- * @param arg0
- * @param arg1
- * @return
- */
- public static Object unpackToObject(byte[] arg0, Template arg1) {
- Object obj = (Object) MessagePack.unpack(arg0, arg1);
- return rawObjToObj(obj);
- }
- /**
- *
- * Map
- *
- * @param arg0
- * @return
- */
- public static Map
unpackToMap(byte[] arg0) { - return unpackToMap(arg0, tMap(TString, TAny));
- }
- /**
- *
- * Map
- *
- * @param arg0
- * @param arg1
- * @return
- */
- public static Map
unpackToMap(byte[] arg0, Template arg1) { - @SuppressWarnings("unchecked")
- Map
dst = (Map ) MessagePack.unpack(arg0, arg1); - return rawtypeMapConvert(dst);
- }
- /**
- *
- * List
- *
- * @param arg0
- * @return
- */
- public static List
- return unpackToList(arg0, tList(tMap(TString, TAny)));
- }
- /**
- *
- * List
- *
- * @param arg0
- * @param arg1
- * @return
- */
- public static List
- @SuppressWarnings("unchecked")
- List
- return rawtypeListConvert(dst);
- }
- private static List
> rawtypeListConvert(final List > list) { - List
> rList = new ArrayList >(); - Map
map = new HashMap (); - for(Map
s : list) { - map = rawtypeMapConvert(s);
- rList.add(map);
- }
- return rList;
- }
- private static Map
rawtypeMapConvert ( - final Map
params) { - Map
rParams = new HashMap (0); - Iterator
iterator = params.keySet().iterator(); - while (iterator.hasNext()) {
- String key = (String) iterator.next();
- Object objResu = rawObjToObj(params.get(key));
- rParams.put(key, objResu);
- }
- return rParams;
- }
- private static Object rawObjToObj(Object obj) {
- Object objResu = null;
- if (obj instanceof IntegerType) {
- objResu = ((IntegerType) obj).asInt();
- } else if (obj instanceof ArrayType) {
- objResu = ((ArrayType) obj).asArray();
- } else if (obj instanceof BooleanType) {
- objResu = ((BooleanType) obj).asBoolean();
- } else if (obj instanceof FloatType) {
- objResu = ((FloatType) obj).asFloat();
- } else {
- objResu = ((RawType) obj).asString();
- }
- return objResu;
- }
- }
- package com.mytest;
- import java.util.HashMap;
- import java.util.Map;
- import org.msgpack.MessagePack;
- /**
- *
- * 功能描述:
- *
- * @author lw
- * @created 2011-6-13 上午09:12:07
- * @version 1.0.0
- * @date 2011-6-13 上午09:12:07
- */
- public class MapMain {
- public static void main(String[] args) {
- Map
map = new HashMap (); - map.put("t", "aa1");
- map.put("f", 111);
- byte[] raw = MessagePack.pack(map);
- map = RawTypeUtil.unpackToMap(raw);
- System.out.println(map.get("t").toString() + " : "
- + map.get("f").toString());
- }
- }
- package com.mytest;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.msgpack.MessagePack;
- /**
- *
- *
- * 功能描述:
- *
- * @author lw
- * @created 2011-6-13 上午09:12:30
- * @version 1.0.0
- * @date 2011-6-13 上午09:12:30
- */
- public class ListMain {
- public static void main(String[] args) {
- List
> list = new ArrayList >(); - Map
map1 = new HashMap (); - map1.put("t", "aa1");
- map1.put("f", 111);
- Map
map2 = new HashMap (); - map2.put("t", "aa2");
- map2.put("f", 333);
- list.add(map1);
- list.add(map2);
- byte[] raw = MessagePack.pack(list);
- List
> rList = new ArrayList >(); - rList = RawTypeUtil.unpackToList(raw);
- for (Map
s : rList) { - System.out.println(s.get("t") + " : " + s.get("f"));
- }
- }
- }