Javascript library: Serialization/ deserialization into bytes type gives wrong data

230 views
Skip to first unread message

Ben B

unread,
May 7, 2021, 2:59:10 PM5/7/21
to Protocol Buffers
Dear Protobuf team,

I was looking to serialize Float32's or Int16 (or other arbitrary data type) so I've decided to use the bytes type. Unfortunately, In Javascript, I've had to convert this data into UInt8 to serialize it. This wasn't too ergonomic because it involves using DataViews, because UInt8 is required to `setBytes` and Int8Array(floatArray) will cast the numbers individually. In the end, that's not a problem though.

However, deserializing this data back out results in incorrect values:
const { BytesMessage } = require('./protobuf/BytesMessage_pb');

const message = new BytesMessage();
const int16Array = new Int16Array([0, 1000, 2000]);
const dataView = new DataView(int16Array.buffer);
const uint8 = new Uint8Array(dataView.buffer);
message.setSomeBytes(uint8);
const bytes = message.serializeBinary(); // <--- This seems to negatively affect the output

const uint8before = message.getSomeBytes() as Uint8Array;
const int16Before = new Int16Array(new DataView(uint8before.buffer).buffer);

const deserializedMessage = BytesMessage.deserializeBinary(bytes);
const uint8after = deserializedMessage.getSomeBytes() as Uint8Array;
const int16After = new Int16Array(new DataView(uint8after.buffer).buffer);
console.log({ uint8before, uint8after });
console.log({ int16Before, int16After });

The output showing the inconsistency between int16Before and int16After:
{
  uint8before: Uint8Array(6) [ 0, 0, 232, 3, 208, 7 ],
  uint8after: Uint8Array(6) [ 0, 0, 232, 3, 208, 7 ]
}
{
  int16Before: Int16Array(3) [ 0, 1000, 2000 ],
  int16After: Int16Array(4) [ 1546, 0, 1000, 2000 ] <-- Notice this one is different to the original.
}

I am not sure why this is the case. As you can see, the exact same code is run, the difference between before and after is whether is call serializeBinary. I have created a minimum example here in this repo: https://github.com/ben-xD/google-protobuf-js-issue-1

Please let me know what you think, is it my programming mistake or is it a bug?

Thanks!

Ben B

unread,
May 10, 2021, 1:53:23 PM5/10/21
to Protocol Buffers
Does anyone who uses or contributes to javascript + protobuf have any idea regarding my previous question?

Thanks :)

Reply all
Reply to author
Forward
0 new messages