Skip to content

Commit

Permalink
Any possible fix for #2251 (work around, but solves a real underlying…
Browse files Browse the repository at this point in the history
… problem)
  • Loading branch information
cowtowncoder committed Feb 9, 2019
1 parent 83e8f5e commit 0dc7f00
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public abstract class BasicDeserializerFactory
final static HashMap<String, Class<? extends Map>> _mapFallbacks =
new HashMap<String, Class<? extends Map>>();
static {
_mapFallbacks.put(Map.class.getName(), LinkedHashMap.class);
@SuppressWarnings("rawtypes")
final Class<? extends Map> DEFAULT_MAP = LinkedHashMap.class;
_mapFallbacks.put(Map.class.getName(), DEFAULT_MAP);
_mapFallbacks.put(AbstractMap.class.getName(), DEFAULT_MAP);
_mapFallbacks.put(ConcurrentMap.class.getName(), ConcurrentHashMap.class);
_mapFallbacks.put(SortedMap.class.getName(), TreeMap.class);

Expand All @@ -78,12 +81,21 @@ public abstract class BasicDeserializerFactory
final static HashMap<String, Class<? extends Collection>> _collectionFallbacks =
new HashMap<String, Class<? extends Collection>>();
static {
_collectionFallbacks.put(Collection.class.getName(), ArrayList.class);
_collectionFallbacks.put(List.class.getName(), ArrayList.class);
_collectionFallbacks.put(Set.class.getName(), HashSet.class);
@SuppressWarnings("rawtypes")
final Class<? extends Collection> DEFAULT_LIST = ArrayList.class;
@SuppressWarnings("rawtypes")
final Class<? extends Collection> DEFAULT_SET = HashSet.class;

_collectionFallbacks.put(Collection.class.getName(), DEFAULT_LIST);
_collectionFallbacks.put(List.class.getName(), DEFAULT_LIST);
_collectionFallbacks.put(Set.class.getName(), DEFAULT_SET);
_collectionFallbacks.put(SortedSet.class.getName(), TreeSet.class);
_collectionFallbacks.put(Queue.class.getName(), LinkedList.class);

// 09-Feb-2019, tatu: How did we miss these? Related in [databind#2251] problem
_collectionFallbacks.put(AbstractList.class.getName(), DEFAULT_LIST);
_collectionFallbacks.put(AbstractSet.class.getName(), DEFAULT_SET);

// then JDK 1.6 types:
/* 17-May-2013, tatu: [databind#216] Should be fine to use straight Class references EXCEPT
* that some god-forsaken platforms (... looking at you, Android) do not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ static class ListAsIterable {
public Iterable<String> values;
}

// [databind#2251]
static class ListAsAbstract {
public AbstractList<String> values;
}

static class SetAsAbstract {
public AbstractSet<String> values;
}

static class ListAsIterableX {
public Iterable<XBean> nums;
}
Expand Down Expand Up @@ -284,4 +293,18 @@ public void testWrapExceptions() throws Exception
assertEquals("I want to catch this exception", exc.getMessage());
}
}

// [databind#2251]
public void testAbstractListAndSet() throws Exception
{
final String JSON = "{\"values\":[\"foo\", \"bar\"]}";

ListAsAbstract list = MAPPER.readValue(JSON, ListAsAbstract.class);
assertEquals(2, list.values.size());
assertEquals(ArrayList.class, list.values.getClass());

SetAsAbstract set = MAPPER.readValue(JSON, SetAsAbstract.class);
assertEquals(2, set.values.size());
assertEquals(HashSet.class, set.values.getClass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public static enum ConcreteType implements ITestType {

static class ClassStringMap extends HashMap<Class<?>,String> { }

static class AbstractMapWrapper {
public AbstractMap<String, Integer> values;
}

/*
/**********************************************************
/* Test methods, untyped (Object valued) maps
Expand Down Expand Up @@ -275,6 +279,14 @@ public void testGenericStringIntMap() throws Exception
assertNull(result.get(""));
}

public void testAbstractMapDefault() throws Exception
{
final AbstractMapWrapper result = MAPPER.readValue("{\"values\":{\"foo\":42}}",
AbstractMapWrapper.class);
assertNotNull(result);
assertEquals(LinkedHashMap.class, result.values.getClass());
}

/*
/**********************************************************
/* Test methods, maps with enums
Expand Down

0 comments on commit 0dc7f00

Please sign in to comment.