Maybe a bit of a stupid question, but being from a...
# serialization
n
Maybe a bit of a stupid question, but being from a Swift background: Can anyone help me to get this code using a generic argument
T
instead of
Data
?
Copy code
fun deserialize(string: String): Data {
        return Json.nonstrict.parse(Data.serializer(), string)
    }
j
Easiest way out currently would be:
Copy code
fun <T> deserialize(deserializationStrategy: DeserializationStrategy<T>, string: String): T {
    return Json.nonstrict.parse(deserializationStrategy, string)
}
The experimental way out would be:
Copy code
@UseExperimental(ImplicitReflectionSerializer::class)
inline fun <reified T : Any> deserialize(string: String): T {
    return Json.nonstrict.parse(string)
}
Usage for experimental:
val obj = deserialize<Data>(myJson)
Usage for non experimental.. well I think you can figure that out
n
Well, I don't think either will work in this case... since I need to have just one parameter as input and lack of reflection on Kotlin Native (which I am using 😞 )
j
I think you would be able to resolve it by using two parameters. Because I was once myself in your position and it is usually just a question about changing up the structure
But yeah, Currently I do not think there are any easier ways