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!

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/protobuf/dddb9e35-4268-4cfd-bc7f-53a67d3f1d9fn%40googlegroups.com.

Reply via email to