Ketan
Ketan

Reputation: 3282

Is there any alternative to Jackson @JsonTypeInfo annotation

Friends,

Jackson framework provides annotation based approach to emit the type information during serialization process.

I do not want to use @JsonTypeInfo annotation in any of my class.

Is there any alternative/s to above annotation.

If yes, please provide example of to do the same if possible.

Upvotes: 4

Views: 2535

Answers (2)

user3227576
user3227576

Reputation: 574

Override JacksonAnnotationIntrospector and use your Introspector in your ObjectMapper by using setAnnotationIntrospector. Here is a code snippet for your AnnotationIntrospector:

@Override
public TypeResolverBuilder<?> findTypeResolver(MapperConfig<?> config,
        AnnotatedClass ac, JavaType baseType) {
    if (Modifier.isAbstract(baseType.getRawClass().getModifiers())){
        StdTypeResolverBuilder typeResolverBuilder = new StdTypeResolverBuilder();
        typeResolverBuilder.typeProperty("@class");
        typeResolverBuilder.inclusion(As.PROPERTY);
        typeResolverBuilder.init(Id.CLASS, null);
        return typeResolverBuilder;
    }
    return super.findTypeResolver(config, ac, baseType);
}

Upvotes: 3

StaxMan
StaxMan

Reputation: 116472

Couple of alternatives:

  • Use mix-in annotations which do not require modifying of value classes
  • Override JacksonAnnotationIntrospector and implement your own logic for determining when and how equivalent type information should be used.

Upvotes: 2

Related Questions