【后台开发】序列化与反序列化,protoBuffer与flatBuffer

it2023-09-08  78

protoBuffer:序列化用时短,压缩后文件体积小,反序列化用时较长。

定义在XXX.proto里:

package XXX message value_struct{ optional int32 price = 1; optional float discount = 2; } message AttrFilter { optional int32 id = 1; optional int32 oper = 2; optional string value = 3; }

使用定义的proto

import "XXX.pb.h" XXX(package的名字)::AttrFilter filter; int id = 3; filter.set_id(id); //扁平类型 auto head = filter.set_value_struct(); //复合类型,需要先得到头部指针 int price = 50; head->set_price(price); float discount = 0.8; head->set_discount(discount);

flatBuffer:序列化用时较长,压缩后文件体积大,反序列化用时很短。

定义:

table FlatTagVector{ name: string; score: float; }

读取:

最新回复(0)