Home >Backend Development >Golang >How Can I Customize JSON Field Names for Protobuf Extension Messages?
Problem:
When extending a message and marshaling it as JSON, the field name for the extension message defaults to "[message.extension_message_name]". This can be undesirable, as it introduces unnecessary complexity and confusion.
Solution:
To customize the JSON name for a protobuf extension message, utilize the json_name field option. By specifying the desired name within the square brackets, you can override the default behavior. For instance:
message TestMessage { string myField = 1 [json_name="my_special_field_name"]; }
Upon marshaling TestMessage to JSON, the field myField will now appear with the name my_special_field_name. This aligns with the language guide's recommendation to use json_name for customizing JSON keys for message fields.
The above is the detailed content of How Can I Customize JSON Field Names for Protobuf Extension Messages?. For more information, please follow other related articles on the PHP Chinese website!