protobuf.marshal 用于进行 pb 序列化。
marshal(message: string, value: any): ArrayBuffer
参数
参数 | 类型 | 描述 |
message | string | 结构体名 |
value | any | JSON 化的请求体 |
返回
类型 | 描述 |
ArrayBuffer | 序列化得到的二进制请求体 |
样例
调用方法进行 pb 序列化:
import protobuf from 'pts/protobuf';// 加载协议文件根目录中的 demo.protoprotobuf.load([], 'demo.proto');// 加载中协议文件 dirName 目录中的 demo.proto// protobuf.load(['dirName'], 'demo.proto');export default function () {// 调用 marshal 进行序列化const data = protobuf.marshal('xxxx.xxx.demo.stSayHelloReq', { msg: 'pts' });console.log(data); // [object ArrayBuffer]// 调用 unmarshal 进行反序列化const value = protobuf.unmarshal('xxxx.xxx.demo.stSayHelloReq', data);console.log(JSON.stringify(value)); // {"msg":"pts"}}