Answer the question
In order to leave comments, you need to log in
How to conveniently read jspb.Message from grpc in JavaScript?
Can anyone explain the best way to convert the protobuff message to JavaScript?
Let's say I received a message with many different parameters.
export class AnyResponse extends jspb.Message {
getView(): View | undefined;
setView(value?: View): OperationResponse;
hasView(): boolean;
clearView(): OperationResponse;
getAnyParam(): string;
setAnyParam(value: string): OperationResponse;
........
"value": {
"fieldsMap": [
[
"id",
{
"nullValue": 0,
"numberValue": 1,
"stringValue": "",
"boolValue": false
}
],
[
"anyNname",
{
"nullValue": 0,
"numberValue": 0,
"stringValue": "Здесь строка",
"boolValue": false
}
],
"value": {
"id": 1,
"name": "Здесь строка",
.......
}
const rowResponse = response.toObject();
const itemsList = response.getSome()?.getItems()?.getValue()?.map(
value => ({
...value.toObject(),
value: value.getValue()?.toJavaScript(),
}),
);
const items = {
...rowResponse.view.items,
itemsList: itemsList,
};
rowResponse.view.items = items;
return rowResponse;
proto.quark.View.Items.toObject = function (includeInstance, msg) {
var f, obj = {
anyNameList: jspb.Message.toObjectList(msg.getAnyNameList(),
proto.quark.Field.toObject, includeInstance),
anyTwoNameList: jspb.Message.toObjectList(msg.getAnyTwoNameList(),
......
};
if (includeInstance) {
obj.$jspbMessageInstance = msg;
}
return obj;
};
jspb.Message.toObjectList = function (a, b, c) {
const rowResult = jspbMessageToObjectListProto.call(this, a, b, c);
// const svgs = a.findIndex(v=>v && v.getType && v.getType() === 'svg');
const svgIndex = rowResult.findIndex(v=>v.type === 'svg');
if(svgIndex >= 0) {
rowResult[svgIndex].value = a[svgIndex].getValue()?.toJavaScript();
}
return rowResult;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question