杰克逊支持没有注释和专用bean字段的多态性 [英] Jackson support for polymorphism without annotations and dedicated bean fields
问题描述
有没有办法在Jackson中使用注释或专用bean字段执行多态类的序列化/反序列化?我必须支持我无法修改且不希望使用注释的类层次结构。
Is there a way to perform serialization/deserialization in Jackson of polymorphic classes w/out using annotations or specialized bean fields? I have to support class hierarchies that I cannot modify and don't wish to use annotations.
我希望能够指定一个合成名称,这不会在我正在序列化/反序列化的类中,它将被插入到JSON表示中并用于标识类型。
I'd like to be able to designate a synthetic name, which would not be in the classes that I am serializing/deserializing, that would be inserted into the JSON representation and used to identify the type.
推荐答案
如果混合不符合您的喜好,则没有任何预定义可以通过,但您可以相对通过子类化 JacksonAnnotationIntrospector
轻松实现这一点,并用它配置mapper。
If mix-ins are not to your liking, there isn't anything pre-defined to pass, but you can relatively easily achieve this by sub-classing JacksonAnnotationIntrospector
and configure mapper with it.
在您的实现中,您可以覆盖所有方面注释访问:在你的情况下,它可能足以覆盖 findTypeResolver()
(如果你想要每个属性覆盖,'findPropertyTypeResolver()')。
然后,该方法可以使用您想要构造的任何机制 TypeResolverBuilder
(很可能是 StdTypeResolverBuilder
)信息通常来自 @JsonTypeInfo
注释。
In your implementation you can override all aspects of annotation access: in your case it's probably enough to override findTypeResolver()
(and if you want per-property overrides, 'findPropertyTypeResolver()').
The method can then use whatever mechanism you want to construct TypeResolverBuilder
(most likely StdTypeResolverBuilder
) that contains same information as what would usually come from @JsonTypeInfo
annotation.
这篇关于杰克逊支持没有注释和专用bean字段的多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!