Search code examples
javaandroidkotlinjackson-dataformat-xml

How to create a custom deserializer in Jackson for a List containing generic type?


Hi following this question

How to create a custom deserializer in Jackson for a generic type?

I would like to know how to adopt this to be able parse

public static class Something {
    public static List<Wrapper<Person>> people;
}

This is what I have so far

internal class WithVisibilityDeserializer :
    JsonDeserializer<WithVisibility<*>>(), ContextualDeserializer {
    private var valueType: JavaType? = null

    @Throws(JsonMappingException::class)
    override fun createContextual(
        ctxt: DeserializationContext,
        property: BeanProperty
    ): JsonDeserializer<*> {
        val wrapperType = property.type
        val valueType = wrapperType.containedType(0)
        val deserializer = WithVisibilityDeserializer()
        deserializer.valueType = valueType
        return deserializer
    }

    @Throws(IOException::class)
    override fun deserialize(parser: JsonParser, ctxt: DeserializationContext): WithVisibility<*> {
        val value: Any? = ctxt.readValue<Any>(parser, valueType)
        return WithVisibility(
            value = value,
            visibility = false
        )
    }
}

and I am getting an NPE when trying to deserialize a lit of this

data class ViewSelectionFieldTypes(
    @JacksonXmlElementWrapper(localName = "Types", useWrapping = false)
    @JacksonXmlProperty(localName = "Type")
    val type: List<WithVisibility<String>>
)

Solution

  • The key thing here was

    al valueType = if (!wrapperType.isCollectionLikeType) {
                wrapperType.containedType(0)
            } else {
                // This is needed because there is a List that contains the WithVisibility (List<WithVisibility<String>>)
                wrapperType.containedType(0).containedType(0)
            }
    

    Solution:

    internal class WithVisibilityDeserializer :
        JsonDeserializer<WithVisibility<*>>(), ContextualDeserializer {
        private var valueType: JavaType? = null
    
        @Throws(JsonMappingException::class)
        override fun createContextual(
            ctxt: DeserializationContext,
            property: BeanProperty
        ): JsonDeserializer<*> {
            val wrapperType = property.type
            val valueType = if (!wrapperType.isCollectionLikeType) {
                wrapperType.containedType(0)
            } else {
                // This is needed because there is a List that contains the WithVisibility (List<WithVisibility<String>>)
                wrapperType.containedType(0).containedType(0)
            }
            val deserializer = WithVisibilityDeserializer()
            deserializer.valueType = valueType
            return deserializer
        }
    
        @Throws(IOException::class)
        override fun deserialize(parser: JsonParser, ctxt: DeserializationContext): WithVisibility<*> {
            val value: Any? = ctxt.readValue<Any>(parser, valueType)
            return WithVisibility(
                value = value,
                visibility = false
            )
        }
    }