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
Landry Norris
11/15/2022, 2:17 PM
For JSON, you can use the JsonNames annotation, such as
Copy code
@JsonNames("refresh-token") val token: String
Landry Norris
11/15/2022, 2:17 PM
In general, I believe there’s a SerialName annotation that works for all serializers, but only supports one name.
x
xun su
11/15/2022, 2:22 PM
it works! I've been waste a lot time searching how to define a varible with "-"😀
l
Landry Norris
11/15/2022, 2:23 PM
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
ephemient
11/15/2022, 11:06 PM
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.