Skip to content

Polymorphic deduction annotation @JsonTypeInfo(use = DEDUCTION) #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,33 +179,36 @@ If you need to read and write values of Objects where there are multiple possibl
This can be done by adding `@JsonTypeInfo` annotation on ''base class'':

```java
// Include Java class name ("com.myempl.ImplClass") as JSON property "class"
@JsonTypeInfo(use=Id.CLASS, include=As.PROPERTY, property="class")
public abstract class BaseClass {
@JsonTypeInfo(use=Id.MINIMAL_CLASS, include=As.PROPERTY, property="type") // Include Java class simple-name as JSON property "type"
@JsonSubTypes({@Type(Car.class), @Type(Aeroplane.class)}) // Required for deserialization only
public abstract class Vehicle {
}

public class Impl1 extends BaseClass {
public int x;
public class Car extends Vehicle {
public String licensePlate;
}
public class Impl2 extends BaseClass {
public String name;
public class Aeroplane extends Vehicle {
public int wingSpan;
}

public class PojoWithTypedObjects {
public List<BaseClass> items;
public List<Vehicle> items;
}
```

and this could result in serialized JSON like:
which gives serialized JSON like:

```json
{ "items" : [
{ "class":"Impl2", "name":"Bob" },
{ "class":"Impl1", "x":13 }
{ "items": [
{ "type": "Car", "licensePlate": "X12345" },
{ "type": "Aeroplane", "wingSpan": 13 }
]}
```

Note that this annotation has lots of configuration possibilities: for more information check out
Alternatively, `@JsonTypeInfo(use=DEDUCTION)` can be used to avoid requiring the 'type' field. For deserialization, types are _deduced_ based
on the fields available. Exceptions will be raised if subtypes do not have a distinct signature of fieldnames or JSON does
not resolve to single known signature.

Note that `@JsonTypeInfo` has lots of configuration possibilities: for more information check out
[Intro to polymorphic type handling](http://www.cowtowncoder.com/blog/archives/2010/03/entry_372.html)

### Changing property auto-detection
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/com/fasterxml/jackson/annotation/JsonTypeInfo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.fasterxml.jackson.annotation;

import java.lang.annotation.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation used for configuring details of if and how type information is
Expand Down Expand Up @@ -116,6 +119,18 @@ public enum Id {
*/
NAME("@type"),

/**
* Means that no serialized typing-property is used. Types are <i>deduced</i> based
* on the fields available. Deduction is limited to the <i>names</i> of fields
* (not their values or, consequently, any nested descendants). Exceptions will be
* thrown if not enough unique information is present to select a single subtype.
* <br>If deduction is being used annotation properties {@code visible},
* {@code property} and {@code include} are ignored.
*
* @since 2.12.0.
*/
DEDUCTION(null),

/**
* Means that typing mechanism uses customized handling, with possibly
* custom configuration. This means that semantics of other properties is
Expand Down