Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proto messages doesn't always produce canonical JSONs

See original GitHub issue

The 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:closed
  • Created 10 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

smaye81commented, Nov 22, 2022

Yep. I am planning to publish a release tomorrow (11/23). Will post here when it’s available.

doron-cohencommented, Nov 24, 2022

Already using it. Thanks @smaye81 !

Read more comments on GitHub >

github_iconTop 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; /** * Convert gRPC message to Json string. * * @param messageOrBuilder the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found