Package dev.orne.beans
Class JacksonSpiTypeIdResolver
java.lang.Object
com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase
dev.orne.beans.JacksonSpiTypeIdResolver
- All Implemented Interfaces:
com.fasterxml.jackson.databind.jsontype.TypeIdResolver
@API(status=EXPERIMENTAL,
since="0.6")
public class JacksonSpiTypeIdResolver
extends com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase
Implementation of
TypeIdResolver
that discovers interface/bean
subtypes using SPI configuration files.
Uses JsonTypeName
annotations to map ID to subtype.
Example:
package a.b.c; @JsonTypeInfo( use=JsonTypeInfo.Id.NAME, defaultImpl=DefaultImpl.class) @JsonTypeIdResolver(JacksonSpiTypeIdResolver.class) interface IBase { ... } package a.b.c; @JsonTypeName("Default") class DefaultImpl implements IBase { ... } package a.b.c.extra; @JsonTypeName("Extra") class ExtraImpl implements IBase { ... }In (potentially multiple)
META-INF/services/a.b.c.IBase
file:
a.b.c.DefaultImpl a.b.c.extra.ExtraImpl
- Since:
- 0.6
- Version:
- 1.0, 2023-11
- Author:
- (w) Iker Hernaez
- See Also:
-
TypeIdResolver
-
Field Summary
Fields inherited from class com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase
_baseType, _typeFactory
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
checkForRepeatedName
(@NotNull Map<String, Class<?>> known, @NotNull String id, @NotNull Class<?> type) Checks the known sub-types for repeated IDs for different sub-type.static String
defaultTypeId
(@NotNull Class<?> cls) If no name was explicitly given for a class, we will just use non-qualified class namestatic String
defaultTypeId
(@NotNull String clsName) If no name was explicitly given for a class, we will just use non-qualified class namefindInheritedSubTypes
(@NotNull Class<?> type) Discovers the registered sub-types scanning the classpath for service providers of the base type.protected String
getIdFromAnnotation
(Class<?> type) Extracts the type ID from theJsonTypeName
annotation of the specified type.protected @NotNull String
getIdFromBean
(@NotNull Object bean) Retrieves the type ID from the bean instance.com.fasterxml.jackson.annotation.JsonTypeInfo.Id
getRegisteredSubTypes
(@NotNull Class<?> baseClass) Discovers the registered sub-types scanning the classpath for service providers of the base type.Returns the registered types.idFromValue
(@NotNull Object value) idFromValueAndType
(Object value, @NotNull Class<?> suggestedType) void
init
(@NotNull com.fasterxml.jackson.databind.JavaType bt) com.fasterxml.jackson.databind.JavaType
typeFromId
(com.fasterxml.jackson.databind.DatabindContext context, String id) Methods inherited from class com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase
getDescForKnownTypeIds, idFromBaseType
-
Constructor Details
-
JacksonSpiTypeIdResolver
public JacksonSpiTypeIdResolver()Creates a new instance.
-
-
Method Details
-
getSubtypes
Returns the registered types.- Returns:
- The registered types
-
init
public void init(@NotNull @NotNull com.fasterxml.jackson.databind.JavaType bt) - Specified by:
init
in interfacecom.fasterxml.jackson.databind.jsontype.TypeIdResolver
- Overrides:
init
in classcom.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase
-
getMechanism
public com.fasterxml.jackson.annotation.JsonTypeInfo.Id getMechanism() -
idFromValue
-
idFromValueAndType
-
typeFromId
public com.fasterxml.jackson.databind.JavaType typeFromId(com.fasterxml.jackson.databind.DatabindContext context, String id) - Specified by:
typeFromId
in interfacecom.fasterxml.jackson.databind.jsontype.TypeIdResolver
- Overrides:
typeFromId
in classcom.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase
-
getRegisteredSubTypes
@NotNull protected @NotNull Map<String,Class<?>> getRegisteredSubTypes(@NotNull @NotNull Class<?> baseClass) Discovers the registered sub-types scanning the classpath for service providers of the base type.- Parameters:
baseClass
- The base type.- Returns:
- The map of IDs to subtypes
-
findInheritedSubTypes
@NotNull protected @NotNull Map<String,Class<?>> findInheritedSubTypes(@NotNull @NotNull Class<?> type) Discovers the registered sub-types scanning the classpath for service providers of the base type. Searchs for- Parameters:
type
- The base type.- Returns:
- The map of IDs to subtypes
-
checkForRepeatedName
protected void checkForRepeatedName(@NotNull @NotNull Map<String, Class<?>> known, @NotNull @NotNull String id, @NotNull @NotNull Class<?> type) Checks the known sub-types for repeated IDs for different sub-type.- Parameters:
known
- The known sub-types.id
- The type ID to check.type
- The type to check.- Throws:
IllegalArgumentException
- If the ID has been used for another sub-type.
-
getIdFromBean
Retrieves the type ID from the bean instance.Default implementation calls to
getIdFromAnnotation(Class)
when bean class.- Parameters:
bean
- The bean to retrieve the type ID from- Returns:
- The type ID, or
null
if not resolved
-
getIdFromAnnotation
Extracts the type ID from theJsonTypeName
annotation of the specified type.- Parameters:
type
- The actual bean type- Returns:
- The type ID, or
null
if not resolved
-
defaultTypeId
If no name was explicitly given for a class, we will just use non-qualified class name- Parameters:
cls
- The type class- Returns:
- The non-qualified class name
- See Also:
-
TypeNameIdResolver
-
defaultTypeId
If no name was explicitly given for a class, we will just use non-qualified class name- Parameters:
clsName
- The class name- Returns:
- The non-qualified class name
- See Also:
-
TypeNameIdResolver
-