【问题标题】:How to create a custom deserializer in Jackson for a List containing generic type?如何在 Jackson 中为包含泛型类型的 List 创建自定义反序列化器?
【发布时间】:2021-02-24 10:59:48
【问题描述】:

您好,关注这个问题

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

我想知道如何通过这个来解析

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

这是我目前所拥有的

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

当我试图反序列化这个时,我得到了一个 NPE

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

【问题讨论】:

    标签: java android kotlin jackson-dataformat-xml


    【解决方案1】:

    这里的关键是

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

    解决方案:

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

    【讨论】:

      猜你喜欢
      • 2016-07-09
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 2017-12-23
      • 1970-01-01
      • 2014-04-05
      相关资源
      最近更新 更多