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

Custom JSON output formatting of properties during serialization with the JsonFormat serializer #17777

Open
leksmano opened this issue Aug 9, 2024 · 1 comment

Comments

@leksmano
Copy link

leksmano commented Aug 9, 2024

What language does this apply to?
proto3

Describe the problem you are trying to solve.
Trying to introduce custom json output formatting for specific types that are autogenerated from protobuf definitions using the JsonFormat class

Currently only default serialization is possible, not allowing any custom printers to be added.

Describe the solution you'd like
ability to add additional custom printers for types that would allow custom Json output value formatting

Example of printers being added in the code currently:
https://github.com/protocolbuffers/protobuf/blob/main/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java#L750

And the wellKnownTypePrinters is private which does not allow extensions:
https://github.com/protocolbuffers/protobuf/blob/main/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java#L747

It would be helpful to allow serialization of enums according to custom rules. Currently it appears this is fixed and no override is possible:
https://github.com/protocolbuffers/protobuf/blob/main/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java#L1234

Describe alternatives you've considered
We have used similar functionality with the scalpb-json4s library that allows us to override the printer for certain types using the DefaultRegistry

@leksmano leksmano added the untriaged auto added to all issues by default when created. label Aug 9, 2024
@JasonLunn
Copy link
Contributor

It is not clear from your question how customization of JSON serialization would work given the need for the JSON to be parseable back into a Proto by a client in another language, or even within Java if they have a different set of customizations. Whereas text formatting is fairly customizable, it is not intended for exchange. JSON has to be able to roundtrip.

Maybe you could provide an example of the kind of customization you need?

@JasonLunn JasonLunn removed the untriaged auto added to all issues by default when created. label Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants