-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
How to customize JSON tags for protoc generated code. #1388
Comments
The only way to change the serialized name of a message in JSON is the adjust the message declaration in the There is no Go-specific way to adjust this. |
You mean by doing something akin to this @dsnet?? |
Correct. |
For some reason that hasn't been working... |
Yes. Some things to consider:
|
I am asking because when I generate the protobuf using that in a proto file I still get the follow in my I would have expected the JSON tag to read Thank you very much for the help to this point also!! |
|
If I needed
I'm assuming this is in fact because the API I'm calling is returning an array of objects. |
In the protobuf type system, only a protobuf message is a first-class type. Cardinality is not first-class type (e.g., You can still handle this case manually by doing: import "encoding/json"
import "google.golang.org/protobuf/encoding/protojson"
func main() {
var raws []json.RawMessage
if err := json.Unmarshal(..., &raws); err != nil {
...
}
for _, raw := range raws {
if err := protojson.Unmarshal(raw, ...); err != nil {
...
}
}
} |
So it does require the loop and two unmarshal's... that was what I thought initially but it seemed like thew wrong route. I've also seen that when the first unmarshal is done the entire object comes out... but would obviously be missing the fields that are not directly coded (i.e. I have the proto field set to I'm going to give this a try and poke at it to see what happens... I'll comment back with what happens. |
If the array is particularly large, you can also use Decoding to a |
So I have another question I haven't been able to find much on, not that I'm understanding enough to use anyway. I have something like the following set up:
Am I forced again in this instance to use the method above shown by @dsnet if the incoming JSON is a:
|
https://developers.google.com/protocol-buffers/docs/proto3#json_options states "Proto3 JSON parsers are required to accept both the converted lowerCamelCase name and the proto field name." The parser only accepts those names and the In your given example, you'll also need to set |
Thanks everyone!!! |
@dsnet Can generate |
Generation of the We can't change the behavior today since it would break existing usages that are accidentally depending on whatever behavior currently happens (see Hyrum's Law. |
This is actually a pretty easy fix to make, if you're using We hope that you can seriously think about it. If this problem can be solved, in fact, projects similar to |
But right now, if someone is using |
We are not making any changes to the content of generated These tags are generated for backwards compatibility, but the only supported way to convert protobuf messages to and from JSON is the |
How about adding an option to disable |
will check for both JSON names. It will unmarshal if either of them matches. |
Not sure if this is the right place or not... apologies if so.
I am trying to get a field in my proto object to map to a JSON field of a different name.
For instance... I would like to have a field , say
Status
, in my gRPC object that would map to a field of a different name, saystatusCode
, in the incoming JSON. I'm not entirely certain this is even possible.I'm hoping to avoid needing to create a struct to unmarshal the incoming object into and then manually assigning each field to the desired counterpart in the gRPC object. Any help or direction with this would be much appreciated.
Again, sorry if I'm in the wrong place.
The text was updated successfully, but these errors were encountered: