溫馨提示×

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

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

Protostuff怎么使用

發(fā)布時(shí)間:2022-03-21 15:05:54 來源:億速云 閱讀:271 作者:iii 欄目:web開發(fā)

今天小編給大家分享一下Protostuff怎么使用的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

  首先,引入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這塊代碼,通過翻看源碼可以知道,里面已經(jīng)放置了一個(gè)緩存map幫我們緩存生成的內(nèi)容,所以不需要自己再加緩存了。

  由于protostuff目前不支持序列化list等對(duì)象,所以需要使用普通的POJO包裝一下。

  最后,來寫一個(gè)測(cè)試吧:

  public static void main(String&hellip;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("數(shù)學(xué)老師");

  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;

  }

  }

  最終的運(yùn)行結(jié)果如下:

  {"address":"I am a good boy","note":"this is test","records":["record1","record2","record3"],"teachers":[{"name":"語文老師"},{"name":"數(shù)學(xué)老師"}],"userId":123456}

以上就是“Protostuff怎么使用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

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

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

AI