Msgpack0.6.12 java版简单使用总结

隐身守侯 提交于 2019-12-03 07:58:37

Msgpack0.6.12 java版简单使用总结        

          关于Msgpack官网上是这么说的,It's like JSON.but fast and small.

Msgpack0.6.12 java版使用总结:

依赖包:javassist-3.18.1-GA.jarmsgpack-0.6.12.jar

1.单个对象使用Msgpack 

1.1 此对象必须要实现Serializable接口(java.io.Serializable) 

1.2 此对象必须要使用@Message注解(org.msgpack.annotation.Message) 

1.3 此对象属性中不能有用transient修饰的字段

2.序列化ListMap(List接口)直接这么做msgpack不支持

2.1 构建含有List,Map属性的字段对象 

2.2 该有的步骤和构建单个对象使用Msgpack一样。

/*
* System Abbrev :
* system Name  :
* Component No  :
* Component Name:
* File name     :MsgpackTest.java
* Author        :Peter.Qiu
* Date          :2015-12-16
* Description   :  <description>
*/

/* Updation record 1:
 * Updation date        :  2015-12-16
 * Updator          :  Peter.Qiu
 * Trace No:  <Trace No>
 * Updation No:  <Updation No>
 * Updation Content:  <List all contents of updation and all methods updated.>
 */
package com.qiuzhping.msgpack;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.msgpack.MessagePack;
import org.msgpack.annotation.Message;


/**
 * <Description functions in a word>
 * 关于Msgpack官网上是这么说的,It's like JSON.but fast and small.<BR />
 * Msgpack0.6.12 使用总结:<BR />
 * 依赖包:javassist-3.18.1-GA.jar,msgpack-0.6.12.jar<BR />
 * 1.单个对象使用Msgpack <BR />
 *   1.1 此对象必须要实现Serializable接口(java.io.Serializable) <BR />
 *   1.2 此对象必须要使用@Message注解(org.msgpack.annotation.Message) <BR />
 *   1.3 此对象属性中不能有用transient修饰的字段. <BR />
 * 2.序列化List,Map(List接口)直接这么做msgpack不支持. <BR />
 *   2.1 构建含有List,Map属性的字段对象 <BR />
 *   2.2 该有的步骤和构建单个对象使用Msgpack一样。<BR />
 * 
 * <Detail description>
 * 
 * @author  Peter.Qiu
 * @version  [Version NO, 2015-12-16]
 * @see  [Related classes/methods]
 * @since  [product/module version]
 */
public class MsgpackTest {

	public static void main(String[] args) {
		MessagePack pack = new MessagePack();
		User user = new User(1, "name", "password");
		try {
			System.out.println("单个对象使用Msgpack");
			System.out.println("序列化前: " + user.toString());
			// 序列化
			byte[] bytes = pack.write(user);
			// 反序列化
			User s = pack.read(bytes, User.class);
			System.out.println("反序列化: " + s.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println();
		//ArrayList
		try {
			System.out.println("ArrayList使用Msgpack");
			ArrayList<User> list = new ArrayList<User>();
			list.add(user);
			System.out.println("序列化前: " + list.get(0).getId());
			// 序列化
			byte[] bytesList = pack.write(list);
			// 反序列化
			@SuppressWarnings("unchecked")
			ArrayList<User> lists = pack.read(bytesList, ArrayList.class);
			System.out.println("反序列化: " + lists.get(0).getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println();
		//Vector
		try {
			System.out.println("Vector使用Msgpack");
			Vector<User> list = new Vector<User>();
			list.add(user);
			System.out.println("序列化前: " + list.get(0).getId());
			// 序列化
			byte[] bytesList = pack.write(list);
			// 反序列化
			@SuppressWarnings("unchecked")
			Vector<User> lists = pack.read(bytesList, Vector.class);
			System.out.println("反序列化: " + lists.get(0).getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println();
		//Map
		try {
			System.out.println("Map使用Msgpack");
			Map<String,User> map = new HashMap<String,User>();
			map.put("user", user);
			System.out.println("序列化前: " + map.get("user"));
			// 序列化
			byte[] bytesList = pack.write(map);
			// 反序列化
			@SuppressWarnings("unchecked")
			Map<String,User> maps = pack.read(bytesList, Map.class);
			System.out.println("反序列化: " + maps.get("user"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println();
		//ComplexEntity(自定义的Entity,只要包含一些集合的属性,List,Map)
		try {
			System.out.println("ComplexEntity使用Msgpack");
			ComplexEntity entity = new ComplexEntity();
			entity.maps.put("user", user);
			entity.lists.add(user);
			entity.lists.add(user);
			
			System.out.println("序列化前map: " + entity.maps.get("user"));
			System.out.println("序列化前list: " + entity.lists);
			// 序列化
			byte[] entityBytes = pack.write(entity);
			// 反序列化
			ComplexEntity entitys = pack.read(entityBytes, ComplexEntity.class);
			System.out.println("反序列化map: " + entitys.maps.get("user"));
			System.out.println("反序列化list: " + entitys.lists);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

@Message
class User implements Serializable {

	/**
	 *serialVersionUID
	 */
	private static final long serialVersionUID = -5848295770696335660L;
	private int id;
	private String name;
	private transient String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public User(int id, String name, String password) {
		this.id = id;
		this.name = name;
		this.password = password;
	}
	
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", password=" + password
				+ "]";
	}
	public User() {
	
	}

}

@Message
class ComplexEntity implements Serializable {

	/**
	 *serialVersionUID
	 */
	private static final long serialVersionUID = -458914520933183052L;
	//为了能直接使用,全部都改为public了
	public List<User> lists = new ArrayList<User>();
	public Map<String,User> maps = new HashMap<String,User>();

	public Map<String, User> getMaps() {
		return maps;
	}

	public void setMaps(Map<String, User> maps) {
		this.maps = maps;
	}

	public List<User> getLists() {
		return lists;
	}

	public void setLists(List<User> lists) {
		this.lists = lists;
	}
}

运行结果:

单个对象使用Msgpack
序列化前: User [id=1, name=name, password=password]
反序列化: User [id=1, name=name, password=null]

ArrayList使用Msgpack
序列化前: 1
org.msgpack.MessageTypeException: convert into unknown type is invalid
	at org.msgpack.template.AnyTemplate.read(AnyTemplate.java:52)
	at org.msgpack.template.AbstractTemplate.read(AbstractTemplate.java:31)
	at org.msgpack.template.ListTemplate.read(ListTemplate.java:66)
	at org.msgpack.template.ListTemplate.read(ListTemplate.java:27)
	at org.msgpack.template.AbstractTemplate.read(AbstractTemplate.java:31)
	at org.msgpack.MessagePack.read(MessagePack.java:388)

Vector使用Msgpack
序列化前: 1
	at org.msgpack.MessagePack.read(MessagePack.java:371)
	at com.qiuzhping.msgpack.MsgpackTest.main(MsgpackTest.java:79)
org.msgpack.MessageTypeException: convert into unknown type is invalid
	at org.msgpack.template.AnyTemplate.read(AnyTemplate.java:52)
	at org.msgpack.template.AbstractTemplate.read(AbstractTemplate.java:31)
	at org.msgpack.template.ListTemplate.read(ListTemplate.java:66)
	at org.msgpack.template.ListTemplate.read(ListTemplate.java:27)
	at org.msgpack.template.AbstractTemplate.read(AbstractTemplate.java:31)
	at org.msgpack.MessagePack.read(MessagePack.java:388)
	at org.msgpack.MessagePack.read(MessagePack.java:371)
	at com.qiuzhping.msgpack.MsgpackTest.main(MsgpackTest.java:95)

Map使用Msgpack
序列化前: User [id=1, name=name, password=password]
org.msgpack.MessageTypeException: convert into unknown type is invalid
	at org.msgpack.template.AnyTemplate.read(AnyTemplate.java:52)
	at org.msgpack.template.AbstractTemplate.read(AbstractTemplate.java:31)
	at org.msgpack.template.MapTemplate.read(MapTemplate.java:71)
	at org.msgpack.template.MapTemplate.read(MapTemplate.java:27)
	at org.msgpack.template.AbstractTemplate.read(AbstractTemplate.java:31)
	at org.msgpack.MessagePack.read(MessagePack.java:388)
	at org.msgpack.MessagePack.read(MessagePack.java:371)
	at com.qiuzhping.msgpack.MsgpackTest.main(MsgpackTest.java:111)

ComplexEntity使用Msgpack
序列化前map: User [id=1, name=name, password=password]
序列化前list: [User [id=1, name=name, password=password], User [id=1, name=name, password=password]]
反序列化map: User [id=1, name=name, password=null]
反序列化list: [User [id=1, name=name, password=null], User [id=1, name=name, password=null]]



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!