您好,登錄后才能下訂單哦!
為了序列化原生的容器對象例如? List 和?Map
?對象,你必須使用?Template
。
Template 對象是?
serializer 和 deserializer 的配對。例如,為了序列化一個?List
?對象,在?List
?對象中?Integer
?對象為元素,你可以使用下面的方法來創建一個模板對象(Template object)。
Template?listTmpl?=?Templates.tList(Templates.TInteger);
類?tList,
?TInteger
?是靜態方法,字段為?Templates。
一個?List
?和?Map
?對象的用例如下顯示:
本代碼可以在?https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Template.java?中查看。
package?com.insight.demo.msgpack; import?org.junit.Test; import?org.msgpack.MessagePack; import?org.msgpack.packer.Packer; import?org.msgpack.template.Template; import?org.msgpack.unpacker.Unpacker; import?org.slf4j.Logger; import?org.slf4j.LoggerFactory; import?java.io.ByteArrayInputStream; import?java.io.ByteArrayOutputStream; import?java.math.BigInteger; import?java.nio.ByteBuffer; import?java.util.ArrayList; import?java.util.HashMap; import?java.util.List; import?java.util.Map; import?static?org.msgpack.template.Templates.*; /** ?*?MessagePack6Template ?* ?*?@author?yhu ?*/ public?class?MessagePack6Template?{ ????final?Logger?logger?=?LoggerFactory.getLogger(MessagePack6Template.class); ????/** ?????*?Test?MessagePack6Template ?????*/ ????@Test ????public?void?testMessagePack6Template()?{ ????????logger.debug("MessagePack6Template?for?Template"); ????????MessagePack?msgpack?=?new?MessagePack(); ????????try?{ ????????????//?Create?templates?for?serializing/deserializing?List?and?Map?objects ????????????Template<List<String>>?listTmpl?=?tList(TString); ????????????Template<Map<String,?String>>?mapTmpl?=?tMap(TString,?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); ????????}?catch?(Exception?ex)?{ ????????????logger.error("MessagePack?Serialization?And?Deserialization?error",?ex); ????????} ????} }
https://www.cwiki.us/display/Serialization/QuickStart+For+MessagePack+Java+0.6.X
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。