Protostuff怎么使用

今天小编给大家分享一下Protostuff怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于做网站、成都网站建设、南宁网络推广、重庆小程序开发、南宁网络营销、南宁企业策划、南宁品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供南宁建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

首先,引入maven依赖如下:

io.protostuff

protostuff-core

1.4.4

io.protostuff

protostuff-runtime

1.4.4

之后,编写序列化工具Util:

public class SerializeUtil {

private static class SerializeData{

private Object target;

}

@SuppressWarnings("unchecked")

public static byte[] serialize(Object object) {

SerializeData serializeData = new SerializeData();

serializeData.target = object;

Class serializeDataClass = (Class) serializeData.getClass();

LinkedBuffer linkedBuffer = LinkedBuffer.allocate(1024 * 4);

try {

Schema schema = RuntimeSchema.getSchema(serializeDataClass);

return ProtostuffIOUtil.toByteArray(serializeData, schema, linkedBuffer);

} catch (Exception e) {

throw new IllegalStateException(e.getMessage(), e);

} finally {

linkedBuffer.clear();

}

}

@SuppressWarnings("unchecked")

public static T deserialize(byte[] data, Class clazz) {

try {

Schema schema = RuntimeSchema.getSchema(SerializeData.class);

SerializeData serializeData = schema.newMessage();

ProtostuffIOUtil.mergeFrom(data, serializeData, schema);

return (T) serializeData.target;

} catch (Exception e) {

throw new IllegalStateException(e.getMessage(), e);

}

}

}

需要注意的是RuntimeSchema.getSchema这块代码,通过翻看源码可以知道,里面已经放置了一个缓存map帮我们缓存生成的内容,所以不需要自己再加缓存了。

由于protostuff目前不支持序列化list等对象,所以需要使用普通的POJO包装一下。

最后,来写一个测试吧:

public static void main(String…args) throws Exception {

User user = new User();

user.setUserId(123456);

user.setAddress("I am a good boy");

user.setNote("this is test");

List list = new ArrayList<>();

list.add("record1");

list.add("record2");

list.add("record3");

user.setRecords(list);

Teacher teacher1 = new Teacher();

teacher1.setName("语文老师");

Teacher teacher2 = new Teacher();

teacher2.setName("数学老师");

List teachers = new ArrayList<>();

teachers.add(teacher1);

teachers.add(teacher2);

user.setTeachers(teachers);

byte[] b = serialize(user);

User rst = deserialize(b, User.class);

System.out.println(JSON.toJSONString(rst));

}

class User {

private Integer userId;

private String address;

private String note;

private List records;

private List teachers;

public Integer getUserId() {

return userId;

}

public void setUserId(Integer userId) {

this.userId = userId;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

public String getNote() {

return note;

}

public void setNote(String note) {

this.note = note;

}

public List getRecords() {

return records;

}

public void setRecords(List records) {

this.records = records;

}

public List getTeachers() {

return teachers;

}

public void setTeachers(List teachers) {

this.teachers = teachers;

}

}

class Teacher{

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

最终的运行结果如下:

{"address":"I am a good boy","note":"this is test","records":["record1","record2","record3"],"teachers":[{"name":"语文老师"},{"name":"数学老师"}],"userId":123456}

以上就是“Protostuff怎么使用”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注创新互联行业资讯频道。


本文题目:Protostuff怎么使用
网站路径:http://azwzsj.com/article/ihsphh.html