Hi, is there some way we can define a variable wi...
# getting-started
x
Hi, is there some way we can define a variable with "-" ? sometimes json data from request may has some field name with "-", how to defined a correspond variable in data class ? the json data could be
{ "refresh-token" : "some thing" }
I got error like this:
l
For JSON, you can use the JsonNames annotation, such as
Copy code
@JsonNames("refresh-token") val token: String
In general, I believe there’s a SerialName annotation that works for all serializers, but only supports one name.
x
it works! I've been waste a lot time searching how to define a varible with "-"😀
l
I would imagine you could write `val `refresh-token`: String` , but that would be terrifying. I know backticks work for methods, and I think they work for variables (but I wouldn’t recommend them)
1
e
use
@SerialName
instead of
@JsonNames
so that it works with other formats and with serialization as well.
@JsonNames
is more useful for coalescing a number of alternative names to one during deserialization.