溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MessagePack Java 0.6.X List, Map 對(duì)象的序列化和反序列化

發(fā)布時(shí)間:2020-03-31 08:00:47 來源:網(wǎng)絡(luò) 閱讀:316 作者:HoneyMoose 欄目:大數(shù)據(jù)

為了序列化原生的容器對(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


向AI問一下細(xì)節(jié)

免責(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)容。

AI