Skip to content

Commit 06549c5

Browse files
authored
Fix #315 and #324: add @JsonDeserializeAs, @JsonSerializeAs (#325)
1 parent b9ac951 commit 06549c5

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ NOTE: Jackson 3.x components rely on 2.x annotations; there are no separate
1818

1919
#314: Add `JsonInclude.Value` convenience constants
2020
(contributed by @runeflobakk)
21+
#315: Add `@JsonDeserializeAs`
2122
#316: Make `JsonFormat.Features` java.io.Serializable
2223
(requested by @tiger9800)
2324
#320: Add `radix` property to `@JsonFormat` annotation
2425
(contributed by @tiger9800)
26+
#324: Add `@JsonSerializeAs`
2527
- Fix SBOM generation, publishing
2628

2729
2.20 (28-Aug-2025)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 deserialization target type;
10+
* a more specialized type (sub-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+
* &#64;JsonDeserializeAs(ValueImpl.class)
31+
* public Value value;
32+
*
33+
* &#64;JsonDeserializeAs(keys = KeyEnum.class, content = ValueImpl.class)
34+
* public Map&lt;Object, Object> props;
35+
* }
36+
*</pre>
37+
*/
38+
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD,
39+
ElementType.TYPE, ElementType.PARAMETER})
40+
@Retention(RetentionPolicy.RUNTIME)
41+
@JacksonAnnotation
42+
public @interface JsonDeserializeAs
43+
{
44+
/**
45+
* Type to deserialize values as, instead of type declared.
46+
* Must be a sub-type of declared type; otherwise an
47+
* exception may be thrown by deserializer.
48+
*<p>
49+
* Bogus type {@link java.lang.Void} is used to indicate that declared
50+
* type is used as-is (i.e. this annotation property has no setting).
51+
*/
52+
public Class<?> value() default Void.class;
53+
54+
/**
55+
* Type to deserialize content entries (array and {@link java.util.Collection}
56+
* elements, {@link java.util.Map} values) as, instead of type declared.
57+
* Must be a sub-type of declared type; otherwise an
58+
* exception may be thrown by deserializer.
59+
*<p>
60+
* Bogus type {@link java.lang.Void} is used to indicate that declared
61+
* type is used as-is (i.e. this annotation property has no setting).
62+
*/
63+
public Class<?> content() default Void.class;
64+
65+
/**
66+
* Type to deserialize {@link java.util.Map} keys as, instead of type declared.
67+
* Must be a sub-type of declared type; otherwise an
68+
* exception may be thrown by deserializer.
69+
*<p>
70+
* Bogus type {@link java.lang.Void} is used to indicate that declared
71+
* type is used as-is (i.e. this annotation property has no setting).
72+
*/
73+
public Class<?> keys() default Void.class;
74+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
* &#64;JsonSerializeAs(GenericValue.class)
32+
* public ValueImpl value;
33+
*
34+
* &#64;JsonSerializeAs(keys = GenericKey.class, content = GenericValue.class)
35+
* public Map&lt;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

Comments
 (0)