protobuf.js
protobuf.js copied to clipboard
Incorrect Encoding Decoding Problem
protobuf.js version: 6.9.0
I am using protobufjs node package to work with protobuf. In my protobuf file structure, I have 3 inner protobuf message type. This is my protobuf file structure:
message OuterMessage {
int32 customer_id = 1;
...
entity.InnerMessage inner_message = 3;
repeated SecondInnerMessage second_message = 4;
}
message InnerMessage {
int32 customer_id = 1;
...
google.protobuf.Timestamp start_date_time = 3;
google.protobuf.Timestamp end_date_time = 4;
}
message SecondInnerMessage {
int32 customer_id = 1;
...
message ThirdInnerMessage {
int32 attr = 1;
}
ThirdInnerMessage third = 8;
}
message Envelope {
int32 envelope_version = 1;
...
google.protobuf.Any payload = 100;
}
Basically there is an outerMessage inside an Envelope. This is the way I read protobuf files:
protobuf.load(filePath, function (err, root) {
if (err)
throw err;
// Obtain a message type
messageType = root.lookupType("...Envelope");
// Verify the payload if necessary (i.e. when possibly incomplete or invalid)
// Here the payload is a json object in the same structure with Envelope
errMsg = messageType.verify(JSON.parse(JSON.stringify(payload)));
if (errMsg)
throw Error(errMsg);
// create message instance
let envelope = messageType.create(JSON.parse(JSON.stringify(payload)));
console.log(envelope);
let encoded = messageType.encode(envelope).finish();
console.log("Encoded ", encoded);
let decoded = messageType.decode(encoded);
console.log("Decoded: ", decoded);
resolve(encoded);
});
Output of above code(I removed attribute names and put only attr
):
Envelope {
attr: 123,
attr: 1594650602645,
payload:
{ attr2: 1,
attr3: 2,
outer_message:
{ attr: 1,
attr: 3,
attr: '2020-05-05 00:00:00',
attr: '2020-06-05 00:00:00' },
second_message: [ [Object] ] } }
Encoded <Buffer a2 06 00>
<Buffer a2 06 00>
Decoded: Envelope { payload: Any {} }
I got a couple of problem with this code. Firstly, the envelope is not printed properly. The SecondInnerMessage
part prints only [ [Object] ]
even though it is full of data.
Secondly, even though there is a considerably big amount of data, encoded version is just <Buffer a2 06 00> <Buffer a2 06 00>
. Is this correct? Why it is too short?
Thirdly, in the decoded version, I can't get the data back. It just prints an empty payload. Why is this?