Protobuf

2024-07-03  本文已影响0人  34sir

用于结构化数据的序列化 高效

高效

可拓展性

跨平台

代码生成

使用.proto定义数据结构

// varlist.proto begin
syntax = "proto3";

enum VarType {
 None    = 0;
 Bool    = 1;
 Int32   = 2;
 Int64   = 3;
 Float   = 4;
 Double  = 5;
 Str     = 6;
 Bytes   = 7;
}

/*
  VarList:
    A flexible data structure to store data with different types and arbitrary
    amount. When the protocol changes, the VarList.proto is no need to change;
    instead, the parsing algorithm should be changed to meet the new protocol.
    The "types" and "indexes" are helper member to map data and it's type and
    index in a VarList message; therefore, the information should be taking
    care on our own.
*/
message VarList {
 repeated bool bools = 1;
 repeated double doubles = 2;
 repeated float floats = 3;
 repeated sint64 int64s = 4;
 repeated int32 ints = 5;
 repeated string strs = 6;
 repeated VarType types = 7;
 repeated bytes bytez = 8;
 repeated int32 indexes = 9; // TODO: confusing, rename to "subindexes" might be better
 int32 count = 10;
}
// varlist.proto end

repeated 是一个关键字 用于定义一个字段可以包含多个值的消息类型 可以高效地存储多个值 使数据格式更紧凑

为什么二进制编码比文本性能高?

flutter 如何集成?
https://pub.dev/packages/protoc_plugin

上一篇 下一篇

猜你喜欢

热点阅读