Are there any common ways of converting a `KClass&...
# serialization
g
Are there any common ways of converting a
KClass<T>
+ `value: String`into an instance of
T
, given that you know
T
is a reasonable value type (
Double
,
Int
,
Enum
, or
String
)?
I'm looking for an expanded (and tested, and somebody else's problem)` version of:
Copy code
fun KType.fromString(valueString: String) = when(val typeClassifier = classifier){
    Double::class -> valueString.toDouble()
    Int::class -> valueString.toInt()
    String::class -> valueString
    is KClass<*> -> {
        if(Enum::class.isSuperclassOf(typeClassifier)){
            Enum.valueOf()
        }
        TODO()

    else -> TODO("$valueString as $typeClassifier")
}
n
so.. not using kotlinx-serialization or only for class types ?
g
does kotlinx-serialization expose a helper function to do this? Can you link me to it?
s
Json.parse(kclass.serializer(), string)
would probably do the trick due to https://github.com/Kotlin/kotlinx.serialization/blob/master/runtime/commonMain/src/kotlinx/serialization/internal/Primitives.kt#L106 (except enums)