Douglas Mesquita
Douglas Mesquita

Reputation: 920

Data object for serialize yml file with Jackson

I'm trying to serialize a .yml file but I got the message:

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"])

As the message show, I don't want the properties "check" and "label" ignorable. Here are my data objects:

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 = "")

The function that serializes:

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) }
}

and than parseYmlFile(file, CheckList::class)

What am I missing here? I've tried to remove @JsonPropertyOrder but haven't worked

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

Upvotes: 1

Views: 126

Answers (1)

Udo Borkowski
Udo Borkowski

Reputation: 311

Make sure to use "val" for the properties:

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

Upvotes: 1

Related Questions