gpt4 book ai didi

json - 在 Kotlin 中使用 Jackson 进行多态(反)序列化时缺少身份字段

转载 作者:行者123 更新时间:2023-12-02 12:38:58 25 4
gpt4 key购买 nike

我有以下注释的类层次结构:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
JsonSubTypes.Type(value = NetCommand.AddEntity::class, name = "AddEntity"),
JsonSubTypes.Type(value = NetCommand.RemoveEntity::class, name = "RemoveEntity"),
JsonSubTypes.Type(value = NetCommand.MoveEntity::class, name = "MoveEntity"),
JsonSubTypes.Type(value = NetCommand.SpeakEntity::class, name = "SpeakEntity"),
JsonSubTypes.Type(value = NetCommand.AddItem::class, name = "AddItem")
)
sealed class NetCommand {
class AddEntity(val id: Long, val position: TilePosition, val table: Character) : NetCommand()
class RemoveEntity(val id: Long) : NetCommand()
class MoveEntity(val id: Long, val position: TilePosition) : NetCommand()
class SpeakEntity(val id: Long, val username: String, val message: String) : NetCommand()
class AddItem(val id: Long, val item: Item) : NetCommand()
}

这个想法是我可以将 NetCommand 的集合 (ArrayList) 传递给第二个应用程序,并将它们正确地反序列化到适当的子类中。

我还编写了一个简单的测试来帮助我迭代注释/ jackson 映射器的不同配置:

val command = NetCommand.AddEntity(1, TilePosition(0, 0), Character.KNIGHT)
val commandList: ArrayList<NetCommand> = ArrayList()
commandList.add(command)

val mapper = jacksonObjectMapper()

val commandListString = mapper.writeValueAsString(commandList)
val resultList = mapper.readValue<ArrayList<NetCommand>>(commandListString)

assert(resultList[0] as? NetCommand.AddEntity != null)
assert((resultList[0] as NetCommand.AddEntity).id == command.id)

这行失败:

val resultList = mapper.readValue<ArrayList<NetCommand>>(commandListString)

出现此错误:

Missing type id when trying to resolve subtype of [simple type, class shared.NetCommand]: missing type id property 'type'
at [Source: (String)"[{"id":1,"position":{"x":0,"y":0},"table":"KNIGHT"}]"; line: 1, column: 51] (through reference chain: java.util.ArrayList[0])
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class shared.NetCommand]: missing type id property 'type'
at [Source: (String)"[{"id":1,"position":{"x":0,"y":0},"table":"KNIGHT"}]"; line: 1, column: 51] (through reference chain: java.util.ArrayList[0])

知道为什么我的类型字段没有被序列化吗?


(不太理想)解决方案

我找到了一个解决方案,即使用子类的名称手动将已初始化的字段添加到子类的主体中。例如。

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
JsonSubTypes.Type(value = AddEntity::class, name = "AddEntity"),
JsonSubTypes.Type(value = RemoveEntity::class, name = "RemoveEntity"),
JsonSubTypes.Type(value = MoveEntity::class, name = "MoveEntity"),
JsonSubTypes.Type(value = SpeakEntity::class, name = "SpeakEntity"),
JsonSubTypes.Type(value = AddItem::class, name = "AddItem")
)
sealed class NetCommand { val type: String = javaClass.simpleName }
class AddEntity(val id: Long, val position: TilePosition, val table: Character) : NetCommand()
class RemoveEntity(val id: Long) : NetCommand()
class MoveEntity(val id: Long, val position: TilePosition) : NetCommand()
class SpeakEntity(val id: Long, val username: String, val message: String) : NetCommand()
class AddItem(val id: Long, val item: Item) : NetCommand()

理想情况下,我想自动使用简单的类名,而不是在每个 JsonSubTypes.Type 调用中使用 name = "AddEntity" 等。

最佳答案

我想我已经找到了最好的解决方案。使用JsonTypeInfo.Id.CLASS对于映射,我不再需要为每个子类型提供名称 - 它只依赖于完全限定的类名。这会自动使用字段名称 @class我可以自动填充父类(super class) NetCommand使用@JsonProperty注释以正确命名字段。另外值得注意的是,我们不需要提供 @JsonSubTypes完全没有注释。

宁愿使用 SimpleName (例如 AddItem 而不是 my.fully.qualified.path.AddItem ),但还没有弄清楚。

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
sealed class NetCommand { @JsonProperty("@class") val type = javaClass.canonicalName }
class AddEntity(val id: Long, val position: TilePosition, val table: Character) : NetCommand()
class RemoveEntity(val id: Long) : NetCommand()
class MoveEntity(val id: Long, val position: TilePosition) : NetCommand()
class SpeakEntity(val id: Long, val username: String, val message: String) : NetCommand()
class AddItem(val id: Long, val item: Item) : NetCommand()

关于json - 在 Kotlin 中使用 Jackson 进行多态(反)序列化时缺少身份字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53136853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com