Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MessagePack Java 0.6.X List, serialization and deserialization of Map objects

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

To serialize native container objects such as List and Map objects, you must use Template.

Template objects are pairs of serializer and deserializer. For example, to serialize a List object where the Integer object is an element, you can create a Template object using the following method:

Template listTmpl = Templates.tList(Templates.TInteger);

Class tList, TInteger is a static method, the field is Templates.

A use case for List and Map objects is shown below:

This code can be viewed at 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 listTmpl = tList(TString); Template mapTmpl = tMap(TString, TString); // // Serialization // ByteArrayOutputStream out = new ByteArrayOutputStream(); Packer packer = msgpack.createPacker(out); // Serialize List object List list = new ArrayList(); list.add("msgpack"); list.add("for"); list.add("java"); packer.write(list); // List object // Serialize Map object Map map = new HashMap(); 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 dstList = unpacker.read(listTmpl); // to Map object Map 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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report