ProtoBuff of java serialization

1. Demand

Serialize       with ProtoBuff

2. Detailed explanation

       1. ProtocolBuffer is a lightweight and efficient structured data storage format that can be used for structured data serialization.

        2. It is very suitable for data storage or data exchange format.

       3. A language-independent, platform-independent and extensible serialized structured data format that can be used in communication protocols, data storage and other fields. Currently provides APIs in C++, Java, Python three languages

The protobuf protocol is based on a file with a  .proto  suffix, which describes what data exists and what the data type is.

 

modifier

  • required : Fields that cannot be added or deleted must be initialized;
  • optional : optional field, which can be deleted or not initialized;
  • repeated : Repeatable fields, corresponding to java files, generate List.

3. Take a chestnut

 

pom file

<dependency>
    <groupId>com.dyuproject.protostuff</groupId>
    <artifactId>protostuff-core</artifactId>
    <version>1.0.8</version>
</dependency>

<dependency>
    <groupId>com.dyuproject.protostuff</groupId>
    <artifactId>protostuff-runtime</artifactId>
    <version>1.0.8</version>
</dependency>

 

Student class

package com.serializable;

public class Student {


    private String id;
    private String username;
    private String password;

    public Student() {
    }
    public Student(String id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password="
                + password + "]";
    }

}

Serializer class

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtobufIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;

public class SerializeUtils {

    public static <T> byte[] serialize(T t,Class<T> clazz) {
        return ProtobufIOUtil.toByteArray(t, RuntimeSchema.createFrom(clazz),
                LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
    }
    public static <T> T deSerialize(byte[] data,Class<T> clazz) {
        RuntimeSchema<T> runtimeSchema = RuntimeSchema.createFrom(clazz);
        T t = runtimeSchema.newMessage();
        ProtobufIOUtil.mergeFrom(data, t, runtimeSchema);
        return t;
    }
}

 

test code

@Test
public  void Protobuf() {
    Student student=new Student("1","村长","123456");
    System.out.println("Serialization");
    byte[] data = SerializeUtils.serialize(student,Student.class);
    for (byte b : data) {
        System.out.print(b);
    }
    System.out.println();
    System.out.println("Deserialize");
    Student student2 = SerializeUtils.deSerialize(data,Student.class);
    System.out.println(student2);
}

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325894573&siteId=291194637