protobuf简介

本文介绍了Google开源的数据序列化工具protobuf的基本概念与优势,并通过一个简单的实战案例展示了如何定义对象结构、生成源码及在Java中进行数据的序列化与反序列化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

protobuf是google开源的一个数据序列化与反序列化工具,由于其支持多种语言、各种平台,多被用于对象的存储,远程调用等方向。
用户首先定义对象的结构,根据结构生成对应语言的源码,然后就可以在各种语言中使用protobuf将数据进行序列化和反序列化。

使用

下面是一个简单的使用的例子:
首先需要定义一个.proto文件,其中需要定义你希望操作的对象的结构。

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phone = 4;
}

保存为person.proto文件,之后下载protoc编译工具,并解压,使用protoc将proto文件生成java类。

protoc.exe --java_out=. person.proto

在指定的java_out目录下就可以生成java对应的类,这里生成了PersonOuterClass.java。将文件放入项目,并引入protobuf的jar包。

compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.1.0'

接下来就可以使用protobuf进行对象的操作了。

    public static void main(String[] args) throws Exception{
        //创建对象
        PersonOuterClass.Person p= PersonOuterClass.Person.newBuilder().setName("my_name").setId(2).build();
        System.out.print(p.toString());
        //序列化对象
        FileOutputStream fos=new FileOutputStream("D://person");
        p.writeTo(fos);
        fos.close();
        //反序列化对象
        FileInputStream fis=new FileInputStream("D://person");
        PersonOuterClass.Person pread=PersonOuterClass.Person.parseFrom(fis);
        System.out.println(pread.toString());
        fis.close();
    }

优势

与java原生的serializable接口进行比较。

    public class JavaPerson implements Serializable{
        public String name;
        public Integer id;
        public String email;
        
        public enum PhoneType{
            MOBILE,HOME,WORK
        }
        public class PhoneNumber{
            public String number;
            public PhoneType type;
        }
        List<PhoneNumber> phone;
        
        public static void main(String[] args) throws Exception{
            JavaPerson jp=new JavaPerson();
            jp.name="my_name";
            jp.id=2;

            FileOutputStream fileOut = new FileOutputStream("d://person.ser");
            ObjectOutputStream outStream = new ObjectOutputStream(fileOut);
            outStream.writeObject(jp);
            outStream.close();
            fileOut.close();


            FileInputStream fileIn =new FileInputStream("d://person.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            jp = (JavaPerson) in.readObject();
            in.close();
            fileIn.close();
        }
    }

运行比较下来:

  • protobuf较java默认形式代码更简洁。
  • 循环运行10000次序列化和反序列化后protobuf比java快约30%。
  • 生成的文件protobuf有11字节而java有215字节。
  • protobuf提供额外的字段校验支持。

目录

proto2语法
proto3语法
protobuf编码
protobuf自解释message
java操作protobuf
使用gradle生成protobuf

转载于:https://www.cnblogs.com/resentment/p/6505044.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值