
com.github.jchanghong.gson.Json2kotlinclass.kt Maven / Gradle / Ivy
package com.github.jchanghong.gson
import cn.hutool.core.util.ClassUtil
import cn.hutool.core.util.ReflectUtil
import cn.hutool.json.JSONUtil
import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.full.allSuperclasses
import kotlin.reflect.full.allSupertypes
import kotlin.reflect.full.createType
import kotlin.reflect.full.isSubtypeOf
internal object Json2kotlinclass {
fun toType(list: List>): String {
for (clazz in list) {
if (clazz.typeParameters.size>0) return "List?"
}
try {
val map = list.map {
it.kotlin.allSuperclasses
.filter { !it.java.isInterface }
.map { it.createType(nullable = true) }
.toSet() + setOf(it.kotlin.createType(nullable = true))
}
val reduce = map.reduce { acc, set -> acc.intersect(set) }
if (reduce.size==1) return reduce.first().toString().removePrefix("kotlin.")
val set = reduce - Any::class.createType(nullable = true)
if (set.size==1) return set.first().toString().removePrefix("kotlin.")
val first = set.last()
return first.toString().removePrefix("kotlin.")
} catch (e: Exception) {
throw e
}
}
fun jsonToKotlinClass(json: List): String {
val map = json.map { it.jsonToObject
© 2015 - 2025 Weber Informatics LLC | Privacy Policy