Skip to content

Commit

Permalink
1.protoStuff和fastjson和serverlization序列化对比
Browse files Browse the repository at this point in the history
  • Loading branch information
lwk123 committed Aug 2, 2017
0 parents commit dbe939e
Show file tree
Hide file tree
Showing 8 changed files with 460 additions and 0 deletions.
40 changes: 40 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ProtostuffTest1</groupId>
<artifactId>ProtostuffTest1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>ProtostuffTest1</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<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>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.29</version>
</dependency>
</dependencies>
</project>
58 changes: 58 additions & 0 deletions src/main/java/ProtostuffTest1/ProtostuffTest1/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ProtostuffTest1.ProtostuffTest1;

import io.protostuff.Tag;

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 + '\'' +
'}';
}
}
59 changes: 59 additions & 0 deletions src/main/java/ProtostuffTest1/ProtostuffTest1/Student1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ProtostuffTest1.ProtostuffTest1;

import java.io.Serializable;

public class Student1 implements Serializable{

private static final long serialVersionUID = -36435658415657806L;

private String name;

private String studentNo;

private int age;

private String schoolName;


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 "Student1{" +
"name='" + name + '\'' +
", studentNo='" + studentNo + '\'' +
", age=" + age +
", schoolName='" + schoolName + '\'' +
'}';
}
}
57 changes: 57 additions & 0 deletions src/main/java/ProtostuffTest1/ProtostuffTest1/Student2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ProtostuffTest1.ProtostuffTest1;

import java.io.Serializable;

public class Student2 implements Serializable{

private String name;

private String studentNo;

private int age;

private String schoolName;


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 "Student1{" +
"name='" + name + '\'' +
", studentNo='" + studentNo + '\'' +
", age=" + age +
", schoolName='" + schoolName + '\'' +
'}';
}
}
55 changes: 55 additions & 0 deletions src/main/java/ProtostuffTest1/ProtostuffTest1/Student3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ProtostuffTest1.ProtostuffTest1;

public class Student3{

private String name;

private String studentNo;

private int age;

private String schoolName;


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 "Student3{" +
"name='" + name + '\'' +
", studentNo='" + studentNo + '\'' +
", age=" + age +
", schoolName='" + schoolName + '\'' +
'}';
}
}
86 changes: 86 additions & 0 deletions src/main/java/ProtostuffTest1/ProtostuffTest1/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package ProtostuffTest1.ProtostuffTest1;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class Test {

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

Student student = new Student();
student.setName("lance");
student.setAge(28);
student.setStudentNo("2011070122");
student.setSchoolName("BJUT");

Student1 student1 = new Student1();

student1.setName("lance");
student1.setAge(28);
student1.setStudentNo("2011070122");
student1.setSchoolName("BJUT");

Student2 student2 = new Student2();

student2.setName("lance");
student2.setAge(28);
student2.setStudentNo("2011070122");
student2.setSchoolName("BJUT");

Student3 student3 = new Student3();

student3.setName("lance");
student3.setAge(28);
student3.setStudentNo("2011070122");
student3.setSchoolName("BJUT");


//使用开源的Protostuff序列化和反序列化
byte[] serializerResult = Util.serializer(student);

System.out.println("使用开源的Protostuff序列化和反序列化 size:" + serializerResult.length);

Student deSerializerResult = Util.deserializer(serializerResult,Student.class);
System.out.println("使用开源的Protostuff序列化Result:" + Arrays.toString(serializerResult));
System.out.println("使用开源的Protostuff反序列化Result:" + deSerializerResult.toString());

System.out.println("----------------------------------------------------");
//实现Serializable接口序列化和反序列化
int size = Util.calcSize(student1);
System.out.println("实现Serializable接口序列化和反序列化 size:" + size);

System.out.println("实现Serializable接口序列化和反序列化Result:" + student1.toString());
System.out.println("----------------------------------------------------");
//fastjson序列化
String studentJson = JSON.toJSONString(student3);
int size2 = studentJson.length();
//反序列化
JSONObject ins1 = JSON.parseObject(studentJson, JSONObject.class);
System.out.println("fastjson序列化 size:"+size2);
System.out.println("fastjson序列化Result:" + student2);
System.out.println("fastjson反序列化Result:" + ins1.toJSONString());

//序列化到本地
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("E:/Student2.txt"));
out.writeObject(student2);
out.close();

//反序列化
ObjectInputStream in=new ObjectInputStream(new FileInputStream("E:/Student2.txt"));
Student2 student4=(Student2)in.readObject();
in.close();
int size3 = Util.calcSize(student2);
System.out.println("实现Serializable接口序列化和反序列化 size:" + size3);

System.out.println("实现Serializable接口序列化和反序列化Result:" + student4.toString());
System.out.println("----------------------------------------------------");
}

}
Loading

0 comments on commit dbe939e

Please sign in to comment.