Ryan Brink
05/18/2023, 10:23 PM"schemas": {
"ComputeUnit": {
"type": "number"
},
// ...
}
and leave it up to the consumer to figure out what kind of number it actually is. Is my best bet to write a custom serializer for Number
(the kotlin stdlib class) that will grok the type at serialization time?Adam S
05/19/2023, 7:17 AMnumber
for floats (so in Kotlin use Float
or Double
), and integer
(so use Int
or Long
)
And in principle if minimum >= 0
you could consider unsigned integers (but I find they tend to be a bit annoying to use in code so regular signed integers are fine)
https://swagger.io/docs/specification/data-models/data-types/#numbersRyan Brink
05/19/2023, 11:57 AMAdam S
05/19/2023, 2:17 PMRyan Brink
05/19/2023, 2:18 PMAdam S
05/19/2023, 2:19 PM0
then that can be parsed to a Float - or am I missing something?Ryan Brink
05/19/2023, 2:20 PMAdam S
05/19/2023, 3:17 PMval computeUnit: JsonElement
, and then in code use computeUnit.toIntOrNull
You could define and use a custom serializer for that, if you wanted to encapsulate the logic. Probably what I’d do is create a wrapper class
@Serializable
value class JsonNumber(private val value: JsonElement)
and then add some util functions/properties to convert it to an int/floatAdam S
05/19/2023, 3:19 PMobject JsonNumberSerializer: KSerializer<Number> { ... }
and define a typealias so it’s easier to re-use it
typealias JsonNumber = @Serializable(with = JsonNumberSerializer::class) Number
but from memory it’s quite tricky to use the raw Number type in code, and it ends up being more trouble than it’s worth