溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Protostuff序列化和反序列化的使用說明

發(fā)布時(shí)間:2020-08-19 14:14:11 來源:腳本之家 閱讀:225 作者:zhglance 欄目:編程語言

大家都知道protobuf好用,可是在網(wǎng)上找到的netty整合protobuf的文章都是千篇一律,自己編寫proto文件然后使用工具轉(zhuǎn)java文件用起來復(fù)雜麻煩,經(jīng)過不懈努力終于找到了一個(gè)簡單的方法希望大家喜歡。

google原生的protobuffer使用起來相當(dāng)麻煩,首先要寫.proto文件,然后編譯.proto文件,生成對(duì)應(yīng)的.java文件,鄙人試了一次,發(fā)現(xiàn)真的很麻煩。而protostuff的官方網(wǎng)站(http://www.protostuff.io/documentation/runtime-schema/),對(duì)于智商比較低的小編來說也略顯生澀,于是鄙人就根據(jù)項(xiàng)目中用到的protostuff,撰寫此文,以方便自己和他人加深印象和學(xué)習(xí)。

1.實(shí)戰(zhàn)

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;
  // 關(guān)于@Tag,要么所有屬性都有@Tag注解,要么都沒有,不能一個(gè)類中只有部分屬性有@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.輸出結(jié)果:  

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'}

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)億速云的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI