Skip to content

Commit

Permalink
Add new JsonTypeInfo.Id.SIMPLE_NAME (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHyukKim authored Aug 19, 2023
1 parent a647764 commit 760bf99
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/fasterxml/jackson/annotation/JsonTypeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ public enum Id {
*/
NAME("@type"),

/**
* Means that the simple name of the Java class, equivalent to the value returned by {@link Class#getSimpleName()},
* is used as the default type identifier, unless explicit name is specified by annotation {@link JsonTypeName}.
*<br>
* For instance:
* <ul>
* <li>For a class "com.example.MyClass", only "MyClass" is used.</li>
* <li>For an inner class "com.example.MyClass$Inner", only "Inner" is used.</li>
* </ul>
* <b>Note:</b> This approach reduces verbosity but requires the simple names to be unique
* to avoid conflicts. If multiple classes share the same simple name, <b>the last declared one</b>
* will be used. This approach should be used with careful consideration of your type hierarchy.
*
* @since 2.16
*/
SIMPLE_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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public void testMutators() throws Exception
assertSame(v, v.withIdType(JsonTypeInfo.Id.CLASS));
JsonTypeInfo.Value v2 = v.withIdType(JsonTypeInfo.Id.MINIMAL_CLASS);
assertEquals(JsonTypeInfo.Id.MINIMAL_CLASS, v2.getIdType());
JsonTypeInfo.Value v3 = v.withIdType(JsonTypeInfo.Id.SIMPLE_NAME);
assertEquals(JsonTypeInfo.Id.SIMPLE_NAME, v3.getIdType());

assertEquals(JsonTypeInfo.As.PROPERTY, v.getInclusionType());
assertSame(v, v.withInclusionType(JsonTypeInfo.As.PROPERTY));
Expand Down

0 comments on commit 760bf99

Please sign in to comment.