Proto messages doesn't always produce canonical JSONs
See original GitHub issueThe generated code for some types (e.g. Timestamp or enum) contain toJson
and toJsonString
methods. These seems to work when passing a Protobuf message object to JSON.stringify
. But, when passing an object which contain such object as a property, this doesn’t work as intended.
In our case Timestamp
is serialized as an object of seconds
and nanos
and enums will serialize into their numbers.
We solved it by passing a replacer
to JSON.stringify
like:
body: JSON.stringify(
body, (_, value) => value?.toJson ? value.toJson() : value
),
According to MDN’s docs JSON.stringify
looks for the method toJSON
and not toJson
or toJsonString
. Am I missing something?
Issue Analytics
- State:
- Created 10 months ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Language Guide (proto3) | Protocol Buffers - Google Developers
One way to make sure this doesn't happen is to specify that the numeric values (and/or names, which can also cause issues for...
Read more >provide canonical output format · Issue #1121 · golang/protobuf
Users often want to rely on some outputs having the same format, for example: Some users store JSON versions of Protobuf messages on...
Read more >Embedding arbitrary JSON in a message - Google Groups
We are looking at migrating an existing, JSON-based protocol in which hand-coded C++ is written to perform serdes between objects and JSON.
Read more >Create Protobuf messages for .NET apps - Microsoft Learn
Learn how to create Protobuf messages for .NET apps. ... Scalar values always have a default value and can't be set to null...
Read more >java - How do I know what the json for a protobuff should be ...
MessageOrBuilder; import com.google.protobuf.util.JsonFormat; /** * Convert gRPC message to Json string. * * @param messageOrBuilder the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yep. I am planning to publish a release tomorrow (11/23). Will post here when it’s available.
Already using it. Thanks @smaye81 !