公开:如何将 JSON 解析为实体 class

Exposed: How to parse JSON into an Entity class

我有以下用户 table 对象和实体 class:

object UserTable : IntIdTable() {
    val name = varchar("name", 256)
}

class User(id: EntityID<Int>): IntEntity(id) {
    companion object : IntEntityClass<User>(UserTable)
    val name by UserTable.name
}

有没有办法使用 Gson(或其他库)来解析 JSON 进入一个 User 实例,然后插入它?据我所知, 看来我必须创建一个中间 UserData 数据 class 然后手动复制这些字段。

data class UserData {
  var id: Int?
  var name: String?
}

fun main() {
  val data = Gson().fromJson<UserData>("...", UserData::class.java)
  val user = User.new {
    name = data.name
  }
}

在这个人为的示例中,它并没有那么糟糕,但我想知道是否有更简单的方法。

Exposed 不允许自己创建 DAO 对象,因为您总是需要将 EntityID 传递给构造函数。然而,Jackson supports reading an existing object。所以,你可以这样写:

transaction {
    User.new {
        mapper.readerForUpdating(this).readValue(json)
    }
}

为确保 Jackson 和 Exposed 不会干扰,您必须像这样创建 mapper

val mapper by lazy {
    val mapper = jacksonObjectMapper()
    mapper.setAnnotationIntrospector(object : JacksonAnnotationIntrospector() {
        override fun hasIgnoreMarker(m : AnnotatedMember)
            =  (m.getDeclaringClass() == IntEntity::class.java)
            || (m.getDeclaringClass() == Entity::class.java)
            || super.hasIgnoreMarker(m)
    })
    mapper
}

另请注意,您不能在委托属性上放置 @JsonProperty 注释,但您必须使用 @get:JsonProperty.

为了使用 Jackson,将以下内容添加到您的 build.gradle 文件中(如果您不使用 gradle,则必须使该代码适应您的构建系统):​​

compile "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.0"

这是一个完整的示例:https://gist.github.com/msrd0/1d8d3d76de4010cc72868d8a36f0560a