Skip to content

Commit 760bf99

Browse files
authored
Add new JsonTypeInfo.Id.SIMPLE_NAME (#234)
1 parent a647764 commit 760bf99

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ public enum Id {
119119
*/
120120
NAME("@type"),
121121

122+
/**
123+
* Means that the simple name of the Java class, equivalent to the value returned by {@link Class#getSimpleName()},
124+
* is used as the default type identifier, unless explicit name is specified by annotation {@link JsonTypeName}.
125+
*<br>
126+
* For instance:
127+
* <ul>
128+
* <li>For a class "com.example.MyClass", only "MyClass" is used.</li>
129+
* <li>For an inner class "com.example.MyClass$Inner", only "Inner" is used.</li>
130+
* </ul>
131+
* <b>Note:</b> This approach reduces verbosity but requires the simple names to be unique
132+
* to avoid conflicts. If multiple classes share the same simple name, <b>the last declared one</b>
133+
* will be used. This approach should be used with careful consideration of your type hierarchy.
134+
*
135+
* @since 2.16
136+
*/
137+
SIMPLE_NAME("@type"),
138+
122139
/**
123140
* Means that no serialized typing-property is used. Types are <i>deduced</i> based
124141
* on the fields available. Deduction is limited to the <i>names</i> of fields

src/test/java/com/fasterxml/jackson/annotation/JsonTypeInfoTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public void testMutators() throws Exception
6161
assertSame(v, v.withIdType(JsonTypeInfo.Id.CLASS));
6262
JsonTypeInfo.Value v2 = v.withIdType(JsonTypeInfo.Id.MINIMAL_CLASS);
6363
assertEquals(JsonTypeInfo.Id.MINIMAL_CLASS, v2.getIdType());
64+
JsonTypeInfo.Value v3 = v.withIdType(JsonTypeInfo.Id.SIMPLE_NAME);
65+
assertEquals(JsonTypeInfo.Id.SIMPLE_NAME, v3.getIdType());
6466

6567
assertEquals(JsonTypeInfo.As.PROPERTY, v.getInclusionType());
6668
assertSame(v, v.withInclusionType(JsonTypeInfo.As.PROPERTY));

0 commit comments

Comments
 (0)