评论

收藏

[Java] Protostuff序列化和反序列化的使用说明

编程语言 编程语言 发布于:2021-08-17 12:28 | 阅读数:476 | 评论:0

大家都知道protobuf好用,可是在网上找到的netty整合protobuf的文章都是千篇一律,自己编写proto文件然后使用工具转java文件用起来复杂麻烦,经过不懈努力终于找到了一个简单的方法希望大家喜欢。
google原生的protobuffer使用起来相当麻烦,首先要写.proto文件,然后编译.proto文件,生成对应的.java文件,鄙人试了一次,发现真的很麻烦。而protostuff的官方网站(http://www.protostuff.io/documentation/runtime-schema/),对于智商比较低的小编来说也略显生涩,于是鄙人就根据项目中用到的protostuff,撰写此文,以方便自己和他人加深印象和学习。
1.实战
1.maven依赖:
<dependency>
  <groupid>io.protostuff</groupid>
  <artifactid>protostuff-core</artifactid>
  <version>1.4.0</version>
</dependency>
<dependency>
  <groupid>io.protostuff</groupid>
  <artifactid>protostuff-runtime</artifactid>
  <version>1.4.0</version>
</dependency>
2.protobufutil工具类:protobufutil.java
import io.protostuff.linkedbuffer;
import io.protostuff.protobufioutil;
import io.protostuff.protostuffioutil;
import io.protostuff.schema;
import io.protostuff.runtime.runtimeschema;
/**
 * created by zhangzh on 2017/2/20.
 */
public class protobufutil {
  public protobufutil() {
  }
  public static <t> byte[] serializer(t o) {
  schema schema = runtimeschema.getschema(o.getclass());
  return protobufioutil.tobytearray(o, schema, linkedbuffer.allocate(256));
  }
  public static <t> t deserializer(byte[] bytes, class<t> clazz) {
  t obj = null;
  try {
    obj = clazz.newinstance();
    schema schema = runtimeschema.getschema(obj.getclass());
    protostuffioutil.mergefrom(bytes, obj, schema);
  } catch (instantiationexception e) {
    e.printstacktrace();
  } catch (illegalaccessexception e) {
    e.printstacktrace();
  }
  return obj;
  }
}
3. bean类student.java:  
import io.protostuff.tag;
/**
 * created by zhangzh on 2017/2/20.
 */
public class student {
  @tag(1)
  private string name;
  @tag(2)
  private string studentno;
  @tag(3)
  private int age;
  @tag(4)
  private string schoolname;
  // 关于@tag,要么所有属性都有@tag注解,要么都没有,不能一个类中只有部分属性有@tag注解
  public string getname() {
  return name;
  }
  public void setname(string name) {
  this.name = name;
  }
  public string getstudentno() {
  return studentno;
  }
  public void setstudentno(string studentno) {
  this.studentno = studentno;
  }
  public int getage() {
  return age;
  }
  public void setage(int age) {
  this.age = age;
  }
  public string getschoolname() {
  return schoolname;
  }
  public void setschoolname(string schoolname) {
  this.schoolname = schoolname;
  }
  @override
  public string tostring() {
  return "student{" +
    "name='" + name + '\'' +
    ", studentno='" + studentno + '\'' +
    ", age=" + age +
    ", schoolname='" + schoolname + '\'' +
    '}';
  }
}
3.test类protobufutiltest.java:  
import java.util.arrays;
/**
 * created by zhangzh on 2017/2/20.
 */
public class protobufutiltest {
  public static void main(string[] args) {
  student student = new student();
  student.setname("lance");
  student.setage(28);
  student.setstudentno("2011070122");
  student.setschoolname("bjut");
  byte[] serializerresult = protobufutil.serializer(student);
  system.out.println("serializer result:" + arrays.tostring(serializerresult));
  student deserializerresult = protobufutil.deserializer(serializerresult,student.class);
  system.out.println("deserializerresult:" + deserializerresult.tostring());
  }
}
4.输出结果:  
serializer result:[10, 5, 108, 97, 110, 99, 101, 18, 10, 50, 48, 49, 49, 48, 55, 48, 49, 50, 50, 24, 28, 34, 4, 66, 74, 85, 84]
deserializerresult:student{name='lance', studentno='2011070122', age=28, schoolname='bjut'}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/zhglance/article/details/56017926

关注下面的标签,发现更多相似文章