我正在尝试序列化一个 .yml 文件,但我收到消息:
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: 无法识别的字段“text”(类 com.core.storage.Content),未标记为可忽略(2 个已知属性:“check”, “标签”])
如消息所示,我不想忽略属性“check”和“label”。这是我的数据对象:
class CheckList(
index: Int,
@JsonProperty("list") content: MutableList<Content> = arrayListOf())
@JsonPropertyOrder("check", "label")
class Content(@JsonProperty("check") val check: String = "",
@JsonProperty("label") val label: String = "")
序列化函数:
fun <T : Any> parseYmlFile(file: File, c: KClass<T>): T {
val mapper = ObjectMapper(YAMLFactory())
mapper.registerModule(KotlinModule())
return file.bufferedReader().use { mapper.readValue(it.readText(), c.java) }
}
比parseYmlFile(file, CheckList::class)
我在这里错过了什么?我尝试删除 @JsonPropertyOrder
但没有成功
YML:
index: 100
list:
- check: Avoid regular phone calls for sensitive conversations
- check: Use Signal for secure calls
- check: Use JitsiMeet for video conferencing
- label: If you must use less secure options
- check: Download from official website
- check: Change password regularly
- check: Adjust settings so you don't keep chat history
- check: Verify who you're speaking to
- check: Consider using anonymous username
- check: Use codes for sensitive topics
最佳答案
确保对属性使用“val”:
class CheckList(
val index: Int,
@JsonProperty("list") val content: MutableList<Content> = arrayListOf())
关于java - 使用 Jackson 序列化 yml 文件的数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50843882/