Tech
01/01/2023, 7:00 PMerror
to an API response I send out so I thought in the serializer to just add the error
field and the only way I got it working was to just make a Surrogate of my error, was wondering if there was a better solution to this than that.Adam S
01/01/2023, 7:34 PMerror
be?
If you’re using JSON you could try a custom JsonTransformingSerializer
, but I suspect that being explicit and creating a new @Serializable class
with a specific field is more clear and understandableTech
01/01/2023, 9:24 PMerror
will always be true which is why I would prefer to not make another classTech
01/01/2023, 9:25 PMinternal object RouteErrorSerializer : KSerializer<RouteError> {
@Serializable
data class Surrogate(
val error: Boolean,
@SerialName("status_code")
val statusCode: Int,
val message: String?
)
private val surrogateSerializer = Surrogate.serializer()
override val descriptor = surrogateSerializer.descriptor
override fun deserialize(decoder: Decoder): RouteError {
val surrogate = surrogateSerializer.deserialize(decoder)
return RouteError(
surrogate.statusCode,
surrogate.message
)
}
override fun serialize(encoder: Encoder, value: RouteError) {
surrogateSerializer.serialize(encoder, Surrogate(
true,
value.statusCode,
value.message
))
}
}
Tech
01/01/2023, 9:26 PMerror
onto the JSON object so in the future I can wrap responses in Kotlin's Result
Adam S
01/01/2023, 10:10 PM@EncodeDefault val error: Boolean = true
into the body (not constructor), and maybe annotate it with @Deprecated
with the level set to ‘hidden’Tech
01/01/2023, 10:11 PMTech
01/01/2023, 10:13 PMAdam S
01/01/2023, 10:14 PM