Skip to content

Commit

Permalink
SimpleNameIdResolver and _defaultTypeId(cls);
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHyukKim committed Aug 3, 2023
1 parent 2ffd950 commit ebe04f5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@
/**
* {@link com.fasterxml.jackson.databind.jsontype.TypeIdResolver} implementation
* that converts between (JSON) Strings and simple Java class names via {@link Class#getSimpleName()}.
* <p>
* Note that this implementation is identical to {@link TypeNameIdResolver} except that instead of
* {@link TypeNameIdResolver#_defaultTypeId(Class)}, this implementation uses {@link Class#getSimpleName()}.
*
* @since 2.16
*/
public class SimpleClassNameIdResolver
public class SimpleNameIdResolver
extends TypeIdResolverBase
{
protected final MapperConfig<?> _config;
Expand All @@ -42,12 +39,9 @@ public class SimpleClassNameIdResolver
*/
protected final Map<String, JavaType> _idToType;

/**
* @since 2.11
*/
protected final boolean _caseInsensitive;

protected SimpleClassNameIdResolver(MapperConfig<?> config, JavaType baseType,
protected SimpleNameIdResolver(MapperConfig<?> config, JavaType baseType,
ConcurrentHashMap<String, String> typeToId,
HashMap<String, JavaType> idToType)
{
Expand All @@ -58,7 +52,7 @@ protected SimpleClassNameIdResolver(MapperConfig<?> config, JavaType baseType,
_caseInsensitive = config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES);
}

public static SimpleClassNameIdResolver construct(MapperConfig<?> config, JavaType baseType,
public static SimpleNameIdResolver construct(MapperConfig<?> config, JavaType baseType,
Collection<NamedType> subtypes, boolean forSer, boolean forDeser)
{
// sanity check
Expand Down Expand Up @@ -86,7 +80,7 @@ public static SimpleClassNameIdResolver construct(MapperConfig<?> config, JavaTy
// no name? Need to figure out default; for now, let's just
// use non-qualified class name
Class<?> cls = t.getType();
String id = t.hasName() ? t.getName() : cls.getSimpleName(); // not {@code _defaultTypeId(cls);}
String id = t.hasName() ? t.getName() : _defaultTypeId(cls);
if (forSer) {
typeToId.put(cls.getName(), id);
}
Expand All @@ -107,11 +101,11 @@ public static SimpleClassNameIdResolver construct(MapperConfig<?> config, JavaTy
}
}
}
return new SimpleClassNameIdResolver(config, baseType, typeToId, idToType);
return new SimpleNameIdResolver(config, baseType, typeToId, idToType);
}

@Override
public JsonTypeInfo.Id getMechanism() { return JsonTypeInfo.Id.SIMPLE_CLASS_NAME; }
public JsonTypeInfo.Id getMechanism() { return JsonTypeInfo.Id.SIMPLE_NAME; }

@Override
public String idFromValue(Object value) {
Expand Down Expand Up @@ -140,7 +134,7 @@ protected String idFromClass(Class<?> clazz)
}
if (name == null) {
// And if still not found, let's choose default?
name = cls.getSimpleName();
name = _defaultTypeId(cls);
}
_typeToId.put(key, name);
}
Expand Down Expand Up @@ -191,4 +185,21 @@ public String getDescForKnownTypeIds() {
public String toString() {
return String.format("[%s; id-to-type=%s]", getClass().getName(), _idToType);
}

/*
/*********************************************************
/* Helper methods
/*********************************************************
*/

/**
* If no name was explicitly given for a class, we will just
* use simple class name
*/
protected static String _defaultTypeId(Class<?> cls)
{
String n = cls.getName();
int ix = Math.max(n.lastIndexOf('.'), n.lastIndexOf('$'));
return (ix < 0) ? n : n.substring(ix+1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ protected TypeIdResolver idResolver(MapperConfig<?> config,
return ClassNameIdResolver.construct(baseType, config, subtypeValidator);
case MINIMAL_CLASS:
return MinimalClassNameIdResolver.construct(baseType, config, subtypeValidator);
case SIMPLE_CLASS_NAME:
return SimpleClassNameIdResolver.construct(config, baseType, subtypes, forSer, forDeser);
case SIMPLE_NAME:
return SimpleNameIdResolver.construct(config, baseType, subtypes, forSer, forDeser);
case NAME:
return TypeNameIdResolver.construct(config, baseType, subtypes, forSer, forDeser);
case NONE: // hmmh. should never get this far with 'none'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class JsonTypeInfoSimpleClassName4061Test extends BaseMapTest
{

@JsonTypeInfo(
use = JsonTypeInfo.Id.SIMPLE_CLASS_NAME)
use = JsonTypeInfo.Id.SIMPLE_NAME)
@JsonSubTypes({
@JsonSubTypes.Type(value = InnerSub4061A.class),
@JsonSubTypes.Type(value = InnerSub4061B.class)
Expand All @@ -38,7 +38,7 @@ static class MinimalInnerSub4061A extends MinimalInnerSuper4061 { }
static class MinimalInnerSub4061B extends MinimalInnerSuper4061 { }

@JsonTypeInfo(
use = JsonTypeInfo.Id.SIMPLE_CLASS_NAME)
use = JsonTypeInfo.Id.SIMPLE_NAME)
@JsonSubTypes({
@JsonSubTypes.Type(value = MixedSub4061A.class),
@JsonSubTypes.Type(value = MixedSub4061B.class)
Expand Down Expand Up @@ -131,7 +131,7 @@ public void testMixedMinimalClass() throws Exception
}

@JsonTypeInfo(
use = JsonTypeInfo.Id.SIMPLE_CLASS_NAME)
use = JsonTypeInfo.Id.SIMPLE_NAME)
@JsonSubTypes({
@JsonSubTypes.Type(value = BasicSub4061A.class),
@JsonSubTypes.Type(value = BasicSub4061B.class)
Expand Down

0 comments on commit ebe04f5

Please sign in to comment.