ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java序列化和Protobuf

2020-05-11 18:55:00  阅读:251  来源: 互联网

标签:userName Java Protobuf java proto userID MyProto hobby 序列化


序列化

原理无非将对象压缩成字符流,需要的时候再取出来;

这种情况在分布式用的比较多,类似的Xml和Json也可以实现这种效果;


下面放一个Java序列化的例子

1.创建实例化对象
package com.bean;


import java.io.Serializable;
import java.util.List;

/**
 * @author: jane
 * @CreateTime: 2020/5/11
 * @Description:
 */
public class MyProto implements Serializable {
    int userID;
    String userName;
    List<String> hobby;
    public MyProto(int userID, String userName, List<String> hobby){
        this.userID = userID;
        this.userName = userName;
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        return "MyProto{" +
                "userID=" + userID +
                ", userName='" + userName + '\'' +
                ", hobby=" + hobby +
                '}';
    }
}

如果有不希望被序列化的属性,可以加上transient;

2.编写测试
package com.serializable;

import com.bean.MyProto;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: jane
 * @CreateTime: 2020/5/11
 * @Description:
 */
public class SerializableTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        List<String> hobby = new ArrayList<>();
        hobby.add("Programming");
        hobby.add("Reading");
        hobby.add("Playing Games");
        MyProto proto = new MyProto(666, "Jane", hobby);
        System.out.println("Before encode:"+proto.toString());

        FileOutputStream fos = new FileOutputStream(new File("src/main/resources/serial.out"));
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(proto);

        FileInputStream fis = new FileInputStream(new File("src/main/resources/serial.out"));
        ObjectInputStream ois = new ObjectInputStream(fis);
        MyProto proto2 = (MyProto)ois.readObject();

        System.out.println("After decode:"+proto2.toString());
    }
}
3.输出结果,符合预期
Before encode:MyProto{userID=666, userName='Jane', hobby=[Programming, Reading, Playing Games]}
After decode:MyProto{userID=666, userName='Jane', hobby=[Programming, Reading, Playing Games]}

Protobuf

Protobuf也是用于序列化的,而且Protobuf的效率比较高,适合常见语言;

Java自带的Serializable就比较专一了。。。

下面演示Windows版的例子

1.下载

https://github.com/protocolbuffers/protobuf/releases/tag/v3.11.4

里面的protoc.exe是关键

2.编写MyProto.proto
option java_package = "com.protobuf";
option java_outer_classname = "MyProtoName";

message MyProto{
    required int32 userID = 1;
    required string userName = 2;
    repeated string hobby = 3;
}
3.Cmd下,通过Protobuf生成Java文件
protoc.exe --java_out=.\ .\MyProto.proto
4.把生成的Java文件放到项目对应位置,编写测试
package com.protobuf;

import com.google.protobuf.InvalidProtocolBufferException;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: jane
 * @CreateTime: 2020/5/11
 * @Description:
 */
public class ProtoTest {
    public static void main(String[] args) throws InvalidProtocolBufferException {
        MyProtoName.MyProto proto = create();
        System.out.println("Before encode:");
        System.out.println(proto.toString());
        MyProtoName.MyProto proto2 = decode(encode(proto));
        System.out.println("After decode:");
        System.out.println(proto2.toString());
        System.out.println(proto.equals(proto2));
    }
    private static MyProtoName.MyProto create(){
        MyProtoName.MyProto.Builder builder = MyProtoName.MyProto.newBuilder();
        builder.setUserID(777);
        builder.setUserName("Jayying");
        List<String> hobby = new ArrayList<>();
        hobby.add("Programming");
        hobby.add("Reading");
        hobby.add("Playing Games");
        builder.addAllHobby(hobby);
        return builder.build();
    }
    private static byte[] encode(MyProtoName.MyProto proto){
        return proto.toByteArray();
    }
    private static MyProtoName.MyProto decode(byte[] code) throws InvalidProtocolBufferException {
        return MyProtoName.MyProto.parseFrom(code);
    }
}
5.结果输出,符合预期
Before encode:
userID: 777
userName: "Jayying"
hobby: "Programming"
hobby: "Reading"
hobby: "Playing Games"

After decode:
userID: 777
userName: "Jayying"
hobby: "Programming"
hobby: "Reading"
hobby: "Playing Games"

true

标签:userName,Java,Protobuf,java,proto,userID,MyProto,hobby,序列化
来源: https://www.cnblogs.com/Jayyi/p/12871068.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有