Protostuff序列化使用说明
1.实战
1.maven依赖:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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; // 关于@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 + '\'' + '}'; } } |
3.test类ProtoBufUtilTest.java:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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.测试输出结果:
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'}
浙公网安备 33010602011771号