0

In my current project I have this group of big and complicated annotations I have to put on a lots of fields around my class. They are Jackson json annotations.

@JsonSerialize(using = IViewableToReferenceSerializer.class, contentUsing = IViewableToReferenceSerializer.class)
private ComplexeObject myObject;

and I would like to substitute it to something like:

@JsonAsReference 
private ComplexeObject myObject;

I already tested the system without the custom annotation and it works fine. I though I could define my annotation as (for example):

@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@JsonSerialize(using = IViewableToReferenceSerializer.class, contentUsing = IViewableToReferenceSerializer.class)
public @interface JsonAsReference {
}

So my question is: Is it even possible? Or am I just doing something wrong?

UPDATE: I found an answer for the particuliar Jackson case in this thread Create a custom Jackson annotation

but I am open to any general case solution.

Community
  • 1
  • 1
le-doude
  • 3,345
  • 2
  • 25
  • 55
  • found the answer for Jackson json in this thread http://stackoverflow.com/questions/12921812/create-a-custom-jackson-annotation ... but I'd be interested in a general answer. – le-doude Jan 23 '14 at 08:20

1 Answers1

1

With the default jackson library you will have no luck.

Here a code snippet from JacksonAnnotationIntrospector (from jackson 1.9.13)

public Object findSerializer(Annotated a) 
{
    /* 21-May-2009, tatu: Slight change; primary annotation is now
     *    @JsonSerialize; @JsonUseSerializer is deprecated
     */
    JsonSerialize ann = a.getAnnotation(JsonSerialize.class);
    if (ann != null) {
        Class<? extends JsonSerializer<?>> serClass = ann.using();
        if (serClass != JsonSerializer.None.class) {
            return serClass;
        }
    }

   ....
}

As you can see only the annotation JsonSerialize will be taken into account. Jackson will not search for a JsonSerialize annotation on other annotation like you wanted.

You can implement an own AnnotationIntrospector and provide it to the SerializationConfig and DeserializationConfig, e.g.

AnnotationIntrospector annIntr = ....; // your implementation

ClassIntrospector<? extends BeanDescription> intr = new BasicClassIntrospector();
VisibilityChecker<?> vc = VisibilityChecker.Std.defaultInstance();
TypeFactory typeFactory= TypeFactory.defaultInstance();

SerializationConfig serConfig = new SerializationConfig(intr, annIntr, vc, null,
            null, typeFactory, null);
DeserializationConfig deserConfig = new DeserializationConfig(intr, annIntr, vc, null,
            null, typeFactory, null);

// null means use a default implementation
ObjectMapper objectMapper = new ObjectMapper(null, null, null, serConfig, deserConfig);
René Link
  • 48,224
  • 13
  • 108
  • 140
  • thanks Rene but like I said regarding the peculiar case of Jackson I already found a solution ... that is way more simple than yours.http://stackoverflow.com/questions/12921812/create-a-custom-jackson-annotation – le-doude Jan 23 '14 at 08:58
  • 1
    Yes ok. So my answer is just a hint if you can not use another lib. :) – René Link Jan 23 '14 at 09:00