From faf3e811b4049b7414ccc21a4fcdd7e427544541 Mon Sep 17 00:00:00 2001 From: Marc Carter Date: Wed, 12 Aug 2020 14:26:20 +0100 Subject: [PATCH 1/2] Introduce @JsonTypeInfo(use=DEDUCTION) --- .../jackson/annotation/JsonTypeInfo.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/annotation/JsonTypeInfo.java b/src/main/java/com/fasterxml/jackson/annotation/JsonTypeInfo.java index c838d2ab..09c6a077 100644 --- a/src/main/java/com/fasterxml/jackson/annotation/JsonTypeInfo.java +++ b/src/main/java/com/fasterxml/jackson/annotation/JsonTypeInfo.java @@ -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 @@ -116,6 +119,18 @@ public enum Id { */ NAME("@type"), + /** + * Means that no serialized typing-property is used. Types are deduced based + * on the fields available. Deduction is limited to the names 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. + *
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 From 374d610f8d2715c9cc30cf2c9ffa030daf40ba43 Mon Sep 17 00:00:00 2001 From: Marc Carter Date: Wed, 12 Aug 2020 14:28:06 +0100 Subject: [PATCH 2/2] Document polymorphic deduction mode Also updated example to be a little more approachable. --- README.md | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index fc18209b..2fb33b32 100644 --- a/README.md +++ b/README.md @@ -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 items; + public List 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