protocolbuffers / protobuf-javascript

BSD 3-Clause "New" or "Revised" License
330 stars 66 forks source link

How to serialize object make the output the same as SerializeAsString() method of C++ #167

Open DinoStray opened 1 year ago

DinoStray commented 1 year ago

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();
dibenede commented 1 year ago

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.

DinoStray commented 1 year ago

I change proto definition bytes xxx to string xxx then everything is ok. I think there is some bug while use bytes for nodejs.

lukesandberg commented 1 year ago

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: @.***>