Skip to content

Commit 01a80c1

Browse files
committed
Improvements to handling of polymorphic type information for AtomicReference, ref types.
1 parent 96f3454 commit 01a80c1

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ public boolean useForType(JavaType t)
228228
}
229229
// fall through
230230
case OBJECT_AND_NON_CONCRETE:
231+
// 19-Apr-2016, tatu: ReferenceType like Optional also requires similar handling:
232+
while (t.isReferenceType()) {
233+
t = t.getReferencedType();
234+
}
231235
return t.isJavaLangObject()
232236
|| (!t.isConcrete()
233237
// [databind#88] Should not apply to JSON tree models:
@@ -237,7 +241,11 @@ public boolean useForType(JavaType t)
237241
while (t.isArrayType()) {
238242
t = t.getContentType();
239243
}
240-
// [Issue#88] Should not apply to JSON tree models:
244+
// 19-Apr-2016, tatu: ReferenceType like Optional also requires similar handling:
245+
while (t.isReferenceType()) {
246+
t = t.getReferencedType();
247+
}
248+
// [databind#88] Should not apply to JSON tree models:
241249
return !t.isFinal() && !TreeNode.class.isAssignableFrom(t.getRawClass());
242250
default:
243251
//case JAVA_LANG_OBJECT:
@@ -1333,7 +1341,7 @@ public ObjectMapper setDefaultPrettyPrinter(PrettyPrinter pp) {
13331341

13341342
/*
13351343
/**********************************************************
1336-
/* Type information configuration (1.5+)
1344+
/* Type information configuration
13371345
/**********************************************************
13381346
*/
13391347

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/ClassNameIdResolver.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,29 @@ protected JavaType _typeFromId(String id, TypeFactory typeFactory)
7777

7878
protected final String _idFrom(Object value, Class<?> cls)
7979
{
80-
// [JACKSON-380] Need to ensure that "enum subtypes" work too
80+
// Need to ensure that "enum subtypes" work too
8181
if (Enum.class.isAssignableFrom(cls)) {
8282
if (!cls.isEnum()) { // means that it's sub-class of base enum, so:
8383
cls = cls.getSuperclass();
8484
}
8585
}
8686
String str = cls.getName();
8787
if (str.startsWith("java.util")) {
88-
/* 25-Jan-2009, tatu: There are some internal classes that
89-
* we can not access as is. We need better mechanism; for
90-
* now this has to do...
91-
*/
92-
/* Enum sets and maps are problematic since we MUST know
93-
* type of contained enums, to be able to deserialize.
94-
* In addition, EnumSet is not a concrete type either
95-
*/
88+
// 25-Jan-2009, tatu: There are some internal classes that we can not access as is.
89+
// We need better mechanism; for now this has to do...
90+
91+
// Enum sets and maps are problematic since we MUST know type of
92+
// contained enums, to be able to deserialize.
93+
// In addition, EnumSet is not a concrete type either
9694
if (value instanceof EnumSet<?>) { // Regular- and JumboEnumSet...
9795
Class<?> enumClass = ClassUtil.findEnumType((EnumSet<?>) value);
9896
// not optimal: but EnumSet is not a customizable type so this is sort of ok
99-
str = TypeFactory.defaultInstance().constructCollectionType(EnumSet.class, enumClass).toCanonical();
97+
str = _typeFactory.constructCollectionType(EnumSet.class, enumClass).toCanonical();
10098
} else if (value instanceof EnumMap<?,?>) {
10199
Class<?> enumClass = ClassUtil.findEnumType((EnumMap<?,?>) value);
102100
Class<?> valueClass = Object.class;
103101
// not optimal: but EnumMap is not a customizable type so this is sort of ok
104-
str = TypeFactory.defaultInstance().constructMapType(EnumMap.class, enumClass, valueClass).toCanonical();
102+
str = _typeFactory.constructMapType(EnumMap.class, enumClass, valueClass).toCanonical();
105103
} else {
106104
String end = str.substring(9);
107105
if ((end.startsWith(".Arrays$") || end.startsWith(".Collections$"))

src/main/java/com/fasterxml/jackson/databind/ser/std/AtomicReferenceSerializer.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,22 @@ public void serializeWithType(AtomicReference<?> ref,
279279
return;
280280
}
281281

282+
// 19-Apr-2016, tatu: In order to basically "skip" the whole wrapper level
283+
// (which is what non-polymorphic serialization does too), we will need
284+
// to simply delegate call, I think, and NOT try to use it here.
285+
282286
// Otherwise apply type-prefix/suffix, then std serialize:
287+
/*
283288
typeSer.writeTypePrefixForScalar(ref, g);
284289
serialize(ref, g, provider);
285290
typeSer.writeTypeSuffixForScalar(ref, g);
291+
*/
292+
293+
JsonSerializer<Object> ser = _valueSerializer;
294+
if (ser == null) {
295+
ser = _findCachedSerializer(provider, value.getClass());
296+
}
297+
ser.serializeWithType(value, g, provider, typeSer);
286298
}
287299

288300
/*

0 commit comments

Comments
 (0)