一直在物色比较好用的网络传输数据序列化工具,看了诸如marshalling,protobuff等,但是均有一个共同特点,使用起来异常繁杂,有没有比较好用同时性能又不会太差的组件呢?答案当然是有的,那就是基于protobuff改造的protostuff,它拥有良好性能的同时,又免去了生成描述文件的烦恼,可谓是趁手利器。
来看看具体的使用方式吧。
首先,引入maven依赖如下:
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.4.4</version>
</dependency>
之后,编写序列化工具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<SerializeData> serializeDataClass = (Class<SerializeData>) serializeData.getClass();
LinkedBuffer linkedBuffer = LinkedBuffer.allocate(1024 * 4);
try {
Schema<SerializeData> 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> T deserialize(byte[] data, Class<T> clazz) {
try {
Schema<SerializeData> 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<String> 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<Teacher> 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<String> records;
private List<Teacher> 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<String> getRecords() {
return records;
}
public void setRecords(List<String> records) {
this.records = records;
}
public List<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(List<Teacher> 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}
可以看到,这种相对来说比较复杂的结构的序列化和反序列化,还是挺得心应手的。
欢迎工作一到五年的Java工程师朋友们加入Java程序员开发: 854393687
群内提供免费的Java架构学习资料(里面有高可用、高并发、高性能及分布式、Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多个知识点的架构资料)合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!
网友评论