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.

1

1 Answer 1

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);
2
  • thanks Rene but like I said regarding the peculiar case of Jackson I already found a solution ... that is way more simple than yours.stackoverflow.com/questions/12921812/…
    – le-doude
    Jan 23, 2014 at 8:58
  • 1
    Yes ok. So my answer is just a hint if you can not use another lib. :)
    – René Link
    Jan 23, 2014 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.