|
| 1 | +package com.fasterxml.jackson.annotation; |
| 2 | + |
| 3 | +import java.lang.annotation.ElementType; |
| 4 | +import java.lang.annotation.Retention; |
| 5 | +import java.lang.annotation.RetentionPolicy; |
| 6 | +import java.lang.annotation.Target; |
| 7 | + |
| 8 | +/** |
| 9 | + * Annotation to indicate override of intended serialization type; |
| 10 | + * a more generic type (super-type) than declared one. |
| 11 | + * Can be used on types ({@code Class}es) or on properties (property accessors |
| 12 | + * like "getters", "setters" and constructor parameters). |
| 13 | + * Overrides can apply to: |
| 14 | + *<ul> |
| 15 | + * <li>{@code value} itself (for all types and properties) |
| 16 | + * </li> |
| 17 | + * <li>{@code content} of structured types (array and {@link java.util.Collection} |
| 18 | + * elements; {@link java.util.Map} values) |
| 19 | + * </li> |
| 20 | + * <li>{@code key}s of {@link java.util.Map}s |
| 21 | + * </li> |
| 22 | + *<p> |
| 23 | + * To indicate that no override is to be used, {@code Void.class} is used |
| 24 | + * as the marker (will use declared type) -- this is necessary as Annotation |
| 25 | + * properties cannot have {@code null} values. |
| 26 | + *<p> |
| 27 | + * Example usage: |
| 28 | + *<pre> |
| 29 | + * public class POJO { |
| 30 | + * // ValueImpl extends GenericValue |
| 31 | + * @JsonSerializeAs(GenericValue.class) |
| 32 | + * public ValueImpl value; |
| 33 | + * |
| 34 | + * @JsonSerializeAs(keys = GenericKey.class, content = GenericValue.class) |
| 35 | + * public Map<KeyImpl, ValueImpl> props; |
| 36 | + * } |
| 37 | + *</pre> |
| 38 | + */ |
| 39 | +@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD, |
| 40 | + ElementType.TYPE, ElementType.PARAMETER}) |
| 41 | +@Retention(RetentionPolicy.RUNTIME) |
| 42 | +@JacksonAnnotation |
| 43 | +public @interface JsonSerializeAs |
| 44 | +{ |
| 45 | + /** |
| 46 | + * Type to serialize values as, instead of type declared. |
| 47 | + * Must be a super-type of declared type (or type itself); otherwise an |
| 48 | + * exception may be thrown by serializer. |
| 49 | + *<p> |
| 50 | + * Bogus type {@link java.lang.Void} is used to indicate that declared |
| 51 | + * type is used as-is (i.e. this annotation property has no setting). |
| 52 | + */ |
| 53 | + public Class<?> value() default Void.class; |
| 54 | + |
| 55 | + /** |
| 56 | + * Type to serialize content entries (array and {@link java.util.Collection} |
| 57 | + * elements, {@link java.util.Map} values) as, instead of type declared. |
| 58 | + * Must be a super-type of declared type (or type itself); otherwise an |
| 59 | + * exception may be thrown by serializer. |
| 60 | + *<p> |
| 61 | + * Bogus type {@link java.lang.Void} is used to indicate that declared |
| 62 | + * type is used as-is (i.e. this annotation property has no setting). |
| 63 | + */ |
| 64 | + public Class<?> content() default Void.class; |
| 65 | + |
| 66 | + /** |
| 67 | + * Type to serialize {@link java.util.Map} keys as, instead of type declared. |
| 68 | + * Must be a super-type of declared type (or type itself); otherwise an |
| 69 | + * exception may be thrown by serializer. |
| 70 | + *<p> |
| 71 | + * Bogus type {@link java.lang.Void} is used to indicate that declared |
| 72 | + * type is used as-is (i.e. this annotation property has no setting). |
| 73 | + */ |
| 74 | + public Class<?> key() default Void.class; |
| 75 | +} |
0 commit comments