您好,登錄后才能下訂單哦!
為了序列化原生的容器對(duì)象例如? List 和?Map
?對(duì)象,你必須使用?Template
。
Template 對(duì)象是?
serializer 和 deserializer 的配對(duì)。例如,為了序列化一個(gè)?List
?對(duì)象,在?List
?對(duì)象中?Integer
?對(duì)象為元素,你可以使用下面的方法來創(chuàng)建一個(gè)模板對(duì)象(Template object)。
Template?listTmpl?=?Templates.tList(Templates.TInteger);
類?tList,
?TInteger
?是靜態(tài)方法,字段為?Templates。
一個(gè)?List
?和?Map
?對(duì)象的用例如下顯示:
本代碼可以在?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
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。