

syntax = "proto3";message Duty {string time = 1;string work = 2;}
syntax = "proto3";import "duty.proto";package student;message Student {string name = 1;Gender gender = 2;message GradeInfo {enum Grade {DEFAULT = 0;FIRST = 1;SECOND = 2;THIRD = 3;}Grade grade = 1;}GradeInfo gradeInfo = 3;map<string, int32> scores = 4;repeated Duty duties = 5;}enum Gender {DEFAULT = 0;FEMALE = 1;MALE = 2;}message SearchRequest {string id = 1;}message SearchResponse {message Result {Student student = 1;}}service SearchService {rpc SearchScores (SearchRequest) returns (SearchResponse);}
import protobuf from 'pts/protobuf';// Load the student.proto file in the root directory of the protocol files. The duty.proto file will be loaded at the same time.protobuf.load([], 'student.proto');// Load the student.proto file in the dirName directory of the protocol files.// protobuf.load(['dirName'], 'student.proto');export default function () {let bodyBuffer = protobuf.marshal('student.Student', {'name': 'Alice','gender': 1, // Alternatively, set it to FEMALE. You can directly set the specific value for enum.'gradeInfo': {'grade': 'THIRD'},'scores': {'Chinese': 116,'Math': 120,'English': 106},'duties': [{'time': 'time1','work': 'work1'},{'time': 'time2','work': 'work2'}]});const value = protobuf.unmarshal('student.Student', bodyBuffer);// {"name":"Alice","gender":"FEMALE","gradeInfo":{"grade":"THIRD"},"scores":{"Math":120,"Chinese":116,"English":106},"duties":[{"time":"time","work":"work"}]}console.log(JSON.stringify(value));}
Feedback