protobuf
protobuf copied to clipboard
[C#] Support snake_case keys in JsonFormatter
What language does this apply to? proto3, C#.
Describe the problem you are trying to solve.
I'd like to serialize a Protobuf message to JSON using JsonFormatter, where keys named using snake_case are kept, instead of being converted into camelCase.
Describe the solution you'd like
I would like the keys to be kept as snake_case.
Describe alternatives you've considered
The alternative I have is taking the output string, parse the JSON, and recursively iterate through all the keys to convert camelCase back to snake_case.
Additional context
In Protobuf Python, for example, this is already possible using the preserving_proto_field_name field.
We could add an option is json fotmat setting and a method public Settings WithPreserveFieldName(bool perserveFieldName) => new Settings(formatDefaultValues, TypeRegistry, FormatEnumsAsIntegers, perserveFieldName)
https://github.com/protocolbuffers/protobuf/blob/master/csharp/src/Google.Protobuf/JsonFormatter.cs#L781
Are you happy to create a PR and assign to me for review?
@anandolee It seems like it's not trivial to add this option because ToJsonName is static and cannot access the JsonFormatter.Settings object.
From what I understand, ToJsonName gets called in the FieldDescriptor constructor and adding this option would require changing multiple classes' constructors, outside of JsonFormatter.
Do you see some other, easier solution?
ToJsonName() is called in some helper APIs or generated code.
Users can create their own JsonFormatter() with settings and then call formatter.Format(message). For example: https://github.com/protocolbuffers/protobuf/blob/master/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs#L90
We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.
This issue is labeled inactive because the last activity was over 90 days ago.
We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.
This issue was closed and archived because there has been no new activity in the 14 days since the inactive label was added.