protobuf-javascript
protobuf-javascript copied to clipboard
How to serialize object make the output the same as SerializeAsString() method of C++
In C++, I can call protobuf_object.SerializeAsString().
Which method is the same as above in nodejs?
The following code is not correct.
let out = pbObj.serializeBinary();
let serializeStr = Buffer.from(out).toString();
What are you trying to do with the output? Fundamentally, serializeBinary is giving you a Uint8Array, which Buffer.from seems ok with. However, Buffer.toString assumes utf8 encoding, which you can't guarantee.
I'm not particularly experience with node, but at a glance I think Buffer/Uint8Array in this circumstance is roughly the same as SerializeAsString's produced std::string (i.e. bag of bits).
If you're trying to pass this off to some API that truly needs a string, then I think you need to Base64 encode.
I change proto definition bytes xxx to string xxx then everything is ok. I think there is some bug while use bytes for nodejs.
The problem is utf8 encoding as dibenede said. When you change your type to string that will get utf8 encoded when you call serializeBinary(), so when you wrap it in a buffer and call toString that will be reversed.
See https://nodejs.org/api/buffer.html#buftostringencoding-start-end
What are you actually trying to do? Why do you need a string?
On Thu, Mar 23, 2023 at 9:44 PM DinoStray @.***> wrote:
I change proto definition bytes xxx to string xxx then everything is ok. I think there is some bug while use bytes for nodejs.
— Reply to this email directly, view it on GitHub https://github.com/protocolbuffers/protobuf-javascript/issues/167#issuecomment-1482242001, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABTJXHMHLV3RWS6QODDXC3W5URENANCNFSM6AAAAAAWD3CKAQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>