Protobuf的序列化与反序列化
proto.proto文件内容
package proto;
message TestMsg{
optional string id = 1;
optional string name = 2;
}
Java 实现
序列化
public byte[] build(){
Proto.TestMsg.Builder builder = Proto.TestMsg.newBuilder();
builder.setId("ID的值");
builder.setName("Name的值");
Proto.TestMsg msg = builder.build();
return msg.toByteArray();
}
反序列化
Proto.TestMsg msg = Proto.TestMsg.parseFrom(message.returnByte());
System.out.Println(msg);
GO 实现
序列化
ison := &protobuf.Bullet{
id: proto.string("ID的值")
name: proto.string("Name的值")
}
data,_=proto.Marshal(ison)
return data
反序列化
mainInterFaceParse := &protobuf.TestMsg{}
err := proto.Unmarshal(reply, mainInterFaceParse)
if err != nil {
fmt.Println("unmarshal msg error:", err)
return
}