Serialization Protocol In Action

This article introduces simple practices for several common serialization protocols

ProtoBuf

ProtoBuf is open source with Google and can be compiled across languages.It can be called an IDL, Interface description language.

  1. Download the compiler, protoc protoc-3.11.4-osx-x86_64.zip; download versions of different operating systems. https://github.com/google/protobuf/releases
  2. Setting environment variables
 vi ~/.bash_profile
 export PATH=$PATH:/path/to/protoc-3.4.0/bin
  1. Introducing dependencies in maven projects
<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>3.11.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.googlecode.protobuf-java-format/protobuf-java-format -->
    <dependency>
      <groupId>com.googlecode.protobuf-java-format</groupId>
      <artifactId>protobuf-java-format</artifactId>
      <version>1.4</version>
    </dependency>
  1. Write protocol file with extension proto
syntax = "proto2";

package com.seandde.test;

option optimize_for = SPEED;
option java_package = "com.seandde.test.protobuf";
option java_outer_classname = "DataInfo";


message Student {
    required string name =1;
    required int32 age = 2;
    optional string address = 3;

}
  1. Generate corresponding java classes using protoc
protoc --java_out=src/main/java src/protobuf/Student.proto
  1. Under Simple Test
public class ProtoBufTest {
    public static void main(String ...arg) throws Exception {

        DataInfo.Student student = DataInfo.Student.newBuilder().setName("Zhang San").setAge(28).setAddress("Beijing").build();

        System.out.println(student);

        //Converting to bytes can be transferred over the network
        byte[] stdent2ByteArray = student.toByteArray();

        //Convert to a java object
        DataInfo.Student student2 = DataInfo.Student.parseFrom(stdent2ByteArray);

        System.out.println(student2);

    }

}

Related links: https://developers.google.com/protocol-buffers/docs/overview https://developers.google.com/protocol-buffers/docs/javatutorial

JSON, where alibaba's fastJson is used

  1. Introducing dependencies
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.62</version>
    </dependency>
  1. Writing test classes
public class Student {

    private String name;

    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class FastJsonTest {
    public static void main(String[] args) {
        Student student = new Student();
        student.setAge(11);
        student.setName("Number of sheets");

        String result = JSON.toJSONString(student);

        System.out.println(result);

        Student student1 = JSON.parseObject(result, Student.class);

        System.out.println(student1);
    }
}

Related links: https://github.com/alibaba/fastjson

MsgPack

Note: instead of recording field names, msgpack serializes using arrays that are recorded in field order, so adding fields to the beans requires loading the last of the beans. When used, various msgpack templates can be compiled in advance to improve serialization and deserialization performance

  1. Introducing dependencies
 <!-- https://mvnrepository.com/artifact/org.msgpack/msgpack -->
    <dependency>
      <groupId>org.msgpack</groupId>
      <artifactId>msgpack</artifactId>
      <version>0.6.12</version>
    </dependency>
  1. Write related tests
public class MsgPackTest {

    public static void main(String[] args) throws IOException {
        MessagePack messagePack = new MessagePack();
        Student student = new Student();
        student.setName("zhangsan");
        student.setAge(11);
        
        byte[] result = messagePack.write(student);

        System.out.println(result);
        
        Student student1 = messagePack.read(result, Student.class);

        System.out.println(student1);
		
		// Create serialize objects.
List<String> src = new ArrayList<String>();
src.add("msgpack");
src.add("kumofs");
src.add("viver");
		
		
		// Serialize
byte[] raw = msgpack.write(src);

// Deserialize directly using a template
List<String> dst1 = msgpack.read(raw, Templates.tList(Templates.TString));
System.out.println(dst1.get(0));
System.out.println(dst1.get(1));
System.out.println(dst1.get(2));

// Or, Deserialze to Value then convert type.
Value dynamic = msgpack.read(raw);
List<String> dst2 = new Converter(dynamic)
    .read(Templates.tList(Templates.TString));
System.out.println(dst2.get(0));
System.out.println(dst2.get(1));
System.out.println(dst2.get(2));
    }
}

Related links: https://msgpack.org/

hessian

hessian is different from msgpack in that it records field names, so performance may be slightly worse

  1. Introducing dependencies
  <!-- https://mvnrepository.com/artifact/com.caucho/hessian -->
    <dependency>
      <groupId>com.caucho</groupId>
      <artifactId>hessian</artifactId>
      <version>4.0.63</version>
    </dependency>
  1. Writing test classes
public class HessianTest {

    public static void main(String[] args) throws IOException {

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Hessian2Output output = new Hessian2Output(os);
        output.writeObject("23456789");
        output.close();

        ByteArrayInputStream in = new ByteArrayInputStream(os.toByteArray());
        Hessian2Input input = new Hessian2Input(in);
        System.out.println(input.readObject());
    }
}

Related links: http://hessian.caucho.com/doc/hessian-serialization.html

summary

When choosing a serialization framework, consider performance, supported scopes (such as pan-hua, parent-child classes), whether collection types will be lost, and so on.

Welcome to my public number: Zhang Hengqiang's study notes

Keywords: Programming Java Google JSON github

Added by neal.pressley on Wed, 25 Mar 2020 05:03:03 +0200