MessagePack是一个基于二进制高效的对象序列化类库,可用于跨语言通信。它可以像JSON那样,在许多种语言之间交换结构对象;但是它比JSON更快速也更轻巧。支持Python、Ruby、Java、C/C++等众多语言。比Google Protocol Buffers还要快4倍。
代码地址:https://github.com/msgpack/msgpack
官网地址:http://msgpack.org/
MessagePack的优势是速度比较快,支持众多语言,便于不同语言开发的系统之间交换数据。我在把对象序列化都转换成了字符串形式,是为了把对象存储到缓存中。我们日常项目中使用的数据形式包括对象、List、Set和Map,因此主要把这几种类型的数据进行了序列化及反序列化,支持对象中包含List、Set和Map。
首先在项目的pom文件中引入:
<dependency> <groupId>org.msgpack</groupId> <artifactId>msgpack</artifactId> <version>0.6.12</version> </dependency>
代码MsgpackTest:
package com.nettm.serializable; import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.codec.binary.Base64; import org.msgpack.MessagePack; import org.msgpack.template.SetTemplate; import org.msgpack.template.Template; import org.msgpack.template.Templates; import org.testng.Assert; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class MsgpackTest { private long time; private MessagePack msgpack; @BeforeTest public void beforeTest() { time = System.currentTimeMillis(); msgpack = new MessagePack(); msgpack.register(CustomItemDto.class); msgpack.register(CustomCategoryDto.class); } @AfterTest public void afterTest() { msgpack.unregister(); System.out.println(System.currentTimeMillis() - time); } @Test(invocationCount = 1, threadPoolSize = 1) public void testObject() { CustomItemDto val = new CustomItemDto(); val.setId(10L); val.setItemCode(""); val.setItemDespositPrice(32.45); val.setItemMemo(null); val.setItemName("张金"); val.setItemPrice(89.02); val.setSort(10); String a = serializationObject(val); CustomItemDto newValue = deserializationObject(a, CustomItemDto.class); Assert.assertEquals(val.getId(), newValue.getId()); } @Test(invocationCount = 1, threadPoolSize = 1) public void testList() { List<CustomItemDto> lst = new ArrayList<CustomItemDto>(); for (int i = 0; i < 10; i++) { CustomItemDto val = new CustomItemDto(); val.setId(10L); val.setItemCode(""); val.setItemDespositPrice(32.45); val.setItemMemo(null); val.setItemName("张金"); val.setItemPrice(89.02); val.setSort(10); lst.add(val); } String a = serializationList(lst, CustomItemDto.class); List<CustomItemDto> newValue = deserializationList(a, CustomItemDto.class); Assert.assertEquals(lst.size(), newValue.size()); } @Test(invocationCount = 1, threadPoolSize = 1) public void testBean() { List<CustomCategoryDto> lst = new ArrayList<CustomCategoryDto>(); for (int j = 0; j < 10; j++) { CustomCategoryDto dto = new CustomCategoryDto(); dto.setCategoryCode("ABCD_001"); dto.setCategoryName("呼吸系统"); for (int i = 0; i < 10; i++) { CustomItemDto val = new CustomItemDto(); val.setId(10L); val.setItemCode(""); val.setItemDespositPrice(32.45); val.setItemMemo(null); val.setItemName("张金"); val.setItemPrice(89.02); val.setSort(10); dto.getCustomItemList().add(val); } for (int i = 0; i < 10; i++) { CustomItemDto val = new CustomItemDto(); val.setId(Long.parseLong(String.valueOf(i))); val.setItemCode(""); val.setItemDespositPrice(32.45); val.setItemMemo(null); val.setItemName("张金"); val.setItemPrice(89.02); val.setSort(10); dto.getCustomItemSet().add(val); } for (int i = 0; i < 10; i++) { CustomItemDto val = new CustomItemDto(); val.setId(Long.parseLong(String.valueOf(i))); val.setItemCode(""); val.setItemDespositPrice(32.45); val.setItemMemo(null); val.setItemName("张金"); val.setItemPrice(89.02); val.setSort(10); dto.getCustomItemMap().put(String.valueOf(i), val); } lst.add(dto); } String a = serializationList(lst, CustomCategoryDto.class); List<CustomCategoryDto> newValue = deserializationList(a, CustomCategoryDto.class); Assert.assertEquals(lst.size(), newValue.size()); } @Test(invocationCount = 1, threadPoolSize = 1) public void testMap() { Map<String, CustomItemDto> map = new HashMap<String, CustomItemDto>(); for (int i = 0; i < 10; i++) { CustomItemDto val = new CustomItemDto(); val.setId(10L); val.setItemCode(""); val.setItemDespositPrice(32.45); val.setItemMemo(null); val.setItemName("张金"); val.setItemPrice(89.02); val.setSort(10); map.put(new ObjectId().toString(), val); } String a = serializationMap(map, CustomItemDto.class); Map<String, CustomItemDto> newValue = deserializationMap(a, CustomItemDto.class); Assert.assertEquals(map.size(), newValue.size()); } @Test(invocationCount = 1, threadPoolSize = 1) public void testSet() { Set<CustomItemDto> set = new HashSet<CustomItemDto>(); for (int i = 0; i < 10; i++) { CustomItemDto val = new CustomItemDto(); val.setId(Long.parseLong(String.valueOf(i))); val.setItemCode(""); val.setItemDespositPrice(32.45); val.setItemMemo(null); val.setItemName("金星"); val.setItemPrice(89.02); val.setSort(10); set.add(val); } String a = serializationSet(set, CustomItemDto.class); Set<CustomItemDto> newValue = deserializationSet(a, CustomItemDto.class); Assert.assertEquals(set.size(), newValue.size()); } private <T extends Serializable> String serializationObject(T obj) { byte[] b = null; try { b = msgpack.write(obj); } catch (IOException e) { e.printStackTrace(); } return new String(new Base64().encode(b)); } private <T extends Serializable> T deserializationObject(String obj, Class<T> clazz) { T t = null; byte[] bytes = new Base64().decode(obj.getBytes()); try { t = msgpack.read(bytes, clazz); } catch (IOException e) { e.printStackTrace(); } return t; } private <T extends Serializable> String serializationList(List<T> obj, Class<T> clazz) { byte[] b = null; try { b = msgpack.write(obj); } catch (IOException e) { e.printStackTrace(); } return new String(new Base64().encode(b)); } private <T extends Serializable> List<T> deserializationList(String obj, Class<T> clazz) { Template<T> elementTemplate = msgpack.lookup(clazz); Template<List<T>> listTmpl = Templates.tList(elementTemplate); List<T> t = null; byte[] bytes = new Base64().decode(obj.getBytes()); try { t = msgpack.read(bytes, listTmpl); } catch (IOException e) { e.printStackTrace(); } return t; } private <T extends Serializable> String serializationMap( Map<String, T> obj, Class<T> clazz) { byte[] b = null; try { b = msgpack.write(obj); } catch (IOException e) { e.printStackTrace(); } return new String(new Base64().encode(b)); } private <T extends Serializable> Map<String, T> deserializationMap( String obj, Class<T> clazz) { Template<T> elementTemplate = msgpack.lookup(clazz); Template<Map<String, T>> listTmpl = Templates.tMap(Templates.TString, elementTemplate); Map<String, T> t = null; byte[] bytes = new Base64().decode(obj.getBytes()); try { t = msgpack.read(bytes, listTmpl); } catch (IOException e) { e.printStackTrace(); } return t; } private <T extends Serializable> String serializationSet(Set<T> obj, Class<T> clazz) { Template<T> elementTemplate = msgpack.lookup(clazz); byte[] b = null; try { b = msgpack.write(obj, new SetTemplate<T>(elementTemplate)); } catch (IOException e) { e.printStackTrace(); } return new String(new Base64().encode(b)); } private <T extends Serializable> Set<T> deserializationSet(String obj, Class<T> clazz) { Template<T> elementTemplate = msgpack.lookup(clazz); Set<T> t = null; byte[] bytes = new Base64().decode(obj.getBytes()); try { t = msgpack.read(bytes, new SetTemplate<T>(elementTemplate)); } catch (IOException e) { e.printStackTrace(); } return t; } }
时间: 2024-11-03 21:09:25