ITeye博客

msgpack

djkin · 2015-01-03 19:01 · 学习记录 · Android · 移动开发 · Markdown

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操作 

Java代码  收藏代码

  1. package com.mytest;
  2. /**
  3. *
  4. * 功能描述:
  5. *
  6. * @author lw
  7. * @created 2011-6-9 下午01:55:29
  8. * @version 1.0.0
  9. * @date 2011-6-9 下午01:55:29
  10. */
  11. import java.io.Serializable;
  12. import org.msgpack.annotation.MessagePackMessage;
  13. @MessagePackMessage
  14. // Annotation
  15. public class BeanClass implements Serializable{
  16. /**
  17. *
  18. */
  19. private static final long serialVersionUID = 1L;
  20. // public fields are serialized.
  21. public String str;
  22. public double num;
  23. public String info;
  24. }

Java代码  收藏代码

  1. package com.mytest;
  2. /**
  3. *
  4. * 功能描述:
  5. *
  6. * @author lw
  7. * @created 2011-6-9 下午01:55:29
  8. * @version 1.0.0
  9. * @date 2011-6-9 下午01:55:29
  10. */
  11. import java.util.HashMap;
  12. import org.msgpack.MessagePack;
  13. public class BeanMain {
  14. public static void main(String[] args) {
  15. BeanClass src = new BeanClass();
  16. src.str = "msgpack";
  17. src.num = 0.5;
  18. src.info = "This is msgpack";
  19. HashMap<String, String> map = new HashMap<String, String>();
  20. map.put("src", "src----");
  21. // Serialize
  22. byte[] raw = MessagePack.pack(src);
  23. // Deserialize
  24. BeanClass dst = MessagePack.unpack(raw, BeanClass.class);
  25. System.out.println(dst.str + ":" + dst.num);
  26. }
  27. }

3.  对Map,List操作 

Java代码  收藏代码

  1. package com.mytest;
  2. import static org.msgpack.Templates.TAny;
  3. import static org.msgpack.Templates.TString;
  4. import static org.msgpack.Templates.tList;
  5. import static org.msgpack.Templates.tMap;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.Map;
  11. import org.msgpack.MessagePack;
  12. import org.msgpack.Template;
  13. import org.msgpack.object.ArrayType;
  14. import org.msgpack.object.BooleanType;
  15. import org.msgpack.object.FloatType;
  16. import org.msgpack.object.IntegerType;
  17. import org.msgpack.object.RawType;
  18. /**
  19. *
  20. * 功能描述:
  21. *
  22. * @author lw
  23. * @created 2011-6-13 上午09:33:11
  24. * @version 1.0.0
  25. * @date 2011-6-13 上午09:33:11
  26. */
  27. public class RawTypeUtil {
  28. /**
  29. *
  30. * Object
  31. *
  32. * @param arg0
  33. * @return
  34. */
  35. public static Object unpackToObject(byte[] arg0) {
  36. return unpackToObject(arg0, TAny);
  37. }
  38. /**
  39. *
  40. * Object
  41. *
  42. * @param arg0
  43. * @param arg1
  44. * @return
  45. */
  46. public static Object unpackToObject(byte[] arg0, Template arg1) {
  47. Object obj = (Object) MessagePack.unpack(arg0, arg1);
  48. return rawObjToObj(obj);
  49. }
  50. /**
  51. *
  52. * Map
  53. *
  54. * @param arg0
  55. * @return
  56. */
  57. public static Map<String, Object> unpackToMap(byte[] arg0) {
  58. return unpackToMap(arg0, tMap(TString, TAny));
  59. }
  60. /**
  61. *
  62. * Map
  63. *
  64. * @param arg0
  65. * @param arg1
  66. * @return
  67. */
  68. public static Map<String, Object> unpackToMap(byte[] arg0, Template arg1) {
  69. @SuppressWarnings("unchecked")
  70. Map<String, Object> dst = (Map<String, Object>) MessagePack.unpack(arg0, arg1);
  71. return rawtypeMapConvert(dst);
  72. }
  73. /**
  74. *
  75. * List
  76. *
  77. * @param arg0
  78. * @return
  79. */
  80. public static List<Map<String, Object>> unpackToList(byte[] arg0) {
  81. return unpackToList(arg0, tList(tMap(TString, TAny)));
  82. }
  83. /**
  84. *
  85. * List
  86. *
  87. * @param arg0
  88. * @param arg1
  89. * @return
  90. */
  91. public static List<Map<String, Object>> unpackToList(byte[] arg0, Template arg1) {
  92. @SuppressWarnings("unchecked")
  93. List<Map<String, Object>> dst = (List<Map<String, Object>>) MessagePack.unpack(arg0, arg1);
  94. return rawtypeListConvert(dst);
  95. }
  96. private static List<Map<String, Object>> rawtypeListConvert(final List<Map<String, Object>> list) {
  97. List<Map<String,Object>> rList = new ArrayList<Map<String,Object>>();
  98. Map<String, Object> map = new HashMap<String, Object>();
  99. for(Map<String, Object> s : list) {
  100. map = rawtypeMapConvert(s);
  101. rList.add(map);
  102. }
  103. return rList;
  104. }
  105. private static Map<String, Object> rawtypeMapConvert (
  106. final Map<String, Object> params) {
  107. Map<String, Object> rParams = new HashMap<String, Object>(0);
  108. Iterator<String> iterator = params.keySet().iterator();
  109. while (iterator.hasNext()) {
  110. String key = (String) iterator.next();
  111. Object objResu = rawObjToObj(params.get(key));
  112. rParams.put(key, objResu);
  113. }
  114. return rParams;
  115. }
  116. private static Object rawObjToObj(Object obj) {
  117. Object objResu = null;
  118. if (obj instanceof IntegerType) {
  119. objResu = ((IntegerType) obj).asInt();
  120. } else if (obj instanceof ArrayType) {
  121. objResu = ((ArrayType) obj).asArray();
  122. } else if (obj instanceof BooleanType) {
  123. objResu = ((BooleanType) obj).asBoolean();
  124. } else if (obj instanceof FloatType) {
  125. objResu = ((FloatType) obj).asFloat();
  126. } else {
  127. objResu = ((RawType) obj).asString();
  128. }
  129. return objResu;
  130. }
  131. }

Java代码  收藏代码

  1. package com.mytest;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.msgpack.MessagePack;
  5. /**
  6. *
  7. * 功能描述:
  8. *
  9. * @author lw
  10. * @created 2011-6-13 上午09:12:07
  11. * @version 1.0.0
  12. * @date 2011-6-13 上午09:12:07
  13. */
  14. public class MapMain {
  15. public static void main(String[] args) {
  16. Map<String, Object> map = new HashMap<String, Object>();
  17. map.put("t", "aa1");
  18. map.put("f", 111);
  19. byte[] raw = MessagePack.pack(map);
  20. map = RawTypeUtil.unpackToMap(raw);
  21. System.out.println(map.get("t").toString() + " : "
  22. + map.get("f").toString());
  23. }
  24. }

Java代码  收藏代码

  1. package com.mytest;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.msgpack.MessagePack;
  7. /**
  8. *
  9. *
  10. * 功能描述:
  11. *
  12. * @author lw
  13. * @created 2011-6-13 上午09:12:30
  14. * @version 1.0.0
  15. * @date 2011-6-13 上午09:12:30
  16. */
  17. public class ListMain {
  18. public static void main(String[] args) {
  19. List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  20. Map<String, Object> map1 = new HashMap<String, Object>();
  21. map1.put("t", "aa1");
  22. map1.put("f", 111);
  23. Map<String, Object> map2 = new HashMap<String, Object>();
  24. map2.put("t", "aa2");
  25. map2.put("f", 333);
  26. list.add(map1);
  27. list.add(map2);
  28. byte[] raw = MessagePack.pack(list);
  29. List<Map<String, Object>> rList = new ArrayList<Map<String, Object>>();
  30. rList = RawTypeUtil.unpackToList(raw);
  31. for (Map<String, Object> s : rList) {
  32. System.out.println(s.get("t") + " : " + s.get("f"));
  33. }
  34. }
  35. }

评论(1)

  1. #1 楼 · lvzhou_31 · 2015-04-22 16:58

    能不能把msgpack-0.5.1-devel.jar给下。maven中没找到

评论功能维护中,当前仅展示历史评论。