如何在 Jackson 中为包含泛型类型的列表创建自定义反序列化器?
How to create a custom deserializer in Jackson for a List containing 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>>
)
这里的关键是
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
)
}
}
你好,请问这个问题
我想知道如何采用这个才能解析
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>>
)
这里的关键是
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
)
}
}