我想限制类属性允许的整数值,在序列化为Json(gson)后,该值将表示为数字,而不是字符串.
class Car(
@Expose val color: Color = Color.RED,
@Expose val seats: Seats = Seats.FIVE // Using an enum class so only predefined values are accepted.
)
enum class Color{
@SerializedName("red") RED,
@SerializedName("blue") BLUE,
}
enum class Seats{
@SerializedName("4") FOUR,
@SerializedName("5") FIVE,
}
实际json输出:
{
"color": "red",
"seats": "5" // This is a string. It's not what I want.
}
我需要的json输出:
{
"color": "red",
"seats": 5 // This is a number ✓
}