Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DinoStray opened this issue Mar 22, 2023 · 3 comments
Labels
question Further information is requested

Comments

@DinoStray
Copy link

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
Copy link
Contributor

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.

@dibenede dibenede added the question Further information is requested label Mar 22, 2023
@DinoStray
Copy link
Author

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
Copy link
Contributor

lukesandberg commented Mar 25, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants