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

Values missing if set to 0 and after converting toJson() #762

Closed
iamterryclark opened this issue Mar 25, 2024 · 2 comments
Closed

Values missing if set to 0 and after converting toJson() #762

iamterryclark opened this issue Mar 25, 2024 · 2 comments

Comments

@iamterryclark
Copy link

iamterryclark commented Mar 25, 2024

Hello,

I am running some tests to convert our google-protobuf setup to protobuf-es

we have a WebSocket receiving / requesting data between C++ and JavaScript

i have registered the types i am using and passing that to my unpack function.

I noticed that inside the message JSON after converting the incoming binary function that any value that was set to 0 goes missing from the json object

event.data is an ArrayBuffer so it needs to be converted to a UInt8Array it seems

 const data = new Uint8Array(event.data)
 const message = myMessagePacket.fromBinary(data);
 
 ...
 
//later i convert to json
 
const myMessage = message.anyTypedData.unpack(registry);

functionToPassBackToMyStateManmagement(myMessage.toJson())
 

I noticed printing myMessage.toJson() is missing any value which is set to 0.

@timostamm
Copy link
Member

Hi Terry, this is expected behavior for fields that do not track presence. The field is also omitted when serializing to binary.

You can pass the option emitDefaultValues: true as an argument to toJson. The option is documented here.

@iamterryclark
Copy link
Author

Thank you for the reply. I hadn't seen that.

Looking forward to have this library implemented on our app, in place of google-protobuf

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

No branches or pull requests

2 participants