使用Jackson序列化yml文件的数据对象

时间:2018-06-13 18:25:55

标签: java android kotlin

我正在尝试序列化.yml文件,但我收到了消息:

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "text" (class com.core.storage.Content), not marked as ignorable (2 known properties: "check", "label"])

如消息所示,我不希望属性“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

1 个答案:

答案 0 :(得分:1)

确保使用“val”作为属性:

class CheckList(
    val index: Int,
    @JsonProperty("list") val content: MutableList<Content> = arrayListOf())