'protobuf - field name with special characters
I'm trying to manually model a .proto
file representing a JSON which has one of the field names with a dot and an @ sign. I'm getting an error that doesn't explicitly say that a dot (.) or @
is disallowed. The error message received while building the Java proto library, when the field name is @odata.nextLink
is, Expected field name.
and when the field name is odata.nextLink
is, Missing field number.
Is there a way to work it around? Jackson has a JsonProperty
annotation where could specify the actual field name in the JSON representation, while you could pick up a sane field name in the Java class.
In case you're curious, this is related to Open Data Protocol where their naming conventions are like that.
Solution 1:[1]
There is always a way to work it around :) But i'm not sure if it's worth the effort. First thing to say, PB fields have strict naming conventions, in proto files as well as in Java generated code. No hack to get your special characters in. Annotations are not a simple way out either. Java proto code is generated, so you would have to modify the compiler to add the annotations. It's opensource so technically doable. Practically... it's up to you :)
As you said that you're manually modelling proto files, my recommendation is to make up some kind of naming convention (say, replace .
-> _
and @
-> _at_
). Then translate Odata names and call getters/setters through Java Reflection.
Solution 2:[2]
proto3 has a json_name
option now that you may be able to use for this.
https://developers.google.com/protocol-buffers/docs/proto3#json
See the "Notes" section for the message
proto3 type.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Pavel Zdenek |
Solution 2 | niltz |