Skip to content

Commit db20eb9

Browse files
authored
Polymorphic deduction annotation @JsonTypeInfo(use = DEDUCTION) (#175)
Introduce @JsonTypeInfo(use=DEDUCTION)
1 parent 39b8662 commit db20eb9

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,33 +179,36 @@ If you need to read and write values of Objects where there are multiple possibl
179179
This can be done by adding `@JsonTypeInfo` annotation on ''base class'':
180180

181181
```java
182-
// Include Java class name ("com.myempl.ImplClass") as JSON property "class"
183-
@JsonTypeInfo(use=Id.CLASS, include=As.PROPERTY, property="class")
184-
public abstract class BaseClass {
182+
@JsonTypeInfo(use=Id.MINIMAL_CLASS, include=As.PROPERTY, property="type") // Include Java class simple-name as JSON property "type"
183+
@JsonSubTypes({@Type(Car.class), @Type(Aeroplane.class)}) // Required for deserialization only
184+
public abstract class Vehicle {
185185
}
186-
187-
public class Impl1 extends BaseClass {
188-
public int x;
186+
public class Car extends Vehicle {
187+
public String licensePlate;
189188
}
190-
public class Impl2 extends BaseClass {
191-
public String name;
189+
public class Aeroplane extends Vehicle {
190+
public int wingSpan;
192191
}
193192

194193
public class PojoWithTypedObjects {
195-
public List<BaseClass> items;
194+
public List<Vehicle> items;
196195
}
197196
```
198197

199-
and this could result in serialized JSON like:
198+
which gives serialized JSON like:
200199

201200
```json
202-
{ "items" : [
203-
{ "class":"Impl2", "name":"Bob" },
204-
{ "class":"Impl1", "x":13 }
201+
{ "items": [
202+
{ "type": "Car", "licensePlate": "X12345" },
203+
{ "type": "Aeroplane", "wingSpan": 13 }
205204
]}
206205
```
207206

208-
Note that this annotation has lots of configuration possibilities: for more information check out
207+
Alternatively, `@JsonTypeInfo(use=DEDUCTION)` can be used to avoid requiring the 'type' field. For deserialization, types are _deduced_ based
208+
on the fields available. Exceptions will be raised if subtypes do not have a distinct signature of fieldnames or JSON does
209+
not resolve to single known signature.
210+
211+
Note that `@JsonTypeInfo` has lots of configuration possibilities: for more information check out
209212
[Intro to polymorphic type handling](http://www.cowtowncoder.com/blog/archives/2010/03/entry_372.html)
210213

211214
### Changing property auto-detection

src/main/java/com/fasterxml/jackson/annotation/JsonTypeInfo.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.fasterxml.jackson.annotation;
22

3-
import java.lang.annotation.*;
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
47

58
/**
69
* Annotation used for configuring details of if and how type information is
@@ -116,6 +119,18 @@ public enum Id {
116119
*/
117120
NAME("@type"),
118121

122+
/**
123+
* Means that no serialized typing-property is used. Types are <i>deduced</i> based
124+
* on the fields available. Deduction is limited to the <i>names</i> of fields
125+
* (not their values or, consequently, any nested descendants). Exceptions will be
126+
* thrown if not enough unique information is present to select a single subtype.
127+
* <br>If deduction is being used annotation properties {@code visible},
128+
* {@code property} and {@code include} are ignored.
129+
*
130+
* @since 2.12.0.
131+
*/
132+
DEDUCTION(null),
133+
119134
/**
120135
* Means that typing mechanism uses customized handling, with possibly
121136
* custom configuration. This means that semantics of other properties is

0 commit comments

Comments
 (0)