Skip to content

JsonDeserialize(contentAs=...) broken with raw collections #2553

@cpopp

Description

@cpopp

JsonDeserialize(contentAs=...) doesn't seem to be honored if used with raw collections.

In the following example I would expect that the items field would be deserialized as the MyItem class but instead comes out as a java.util.LinkedHashMap. Adjusting the following example from List to List<Object> causes things to work as expected. (I tried it in 2.9.10 and 2.10.1)

public static class MyCollection {
	@JsonDeserialize(contentAs = MyItem.class)
	public List items;
}

public static class MyItem {
	public String name;
}

public static void main(String[] args) throws Exception {
	ObjectMapper objectMapper = new ObjectMapper();
	
	MyCollection myCollection = 
			objectMapper.readValue("{\"items\": [{\"name\":\"item1\"}]}", MyCollection.class);
	
	System.out.println(myCollection.items);
	System.out.println(myCollection.items.get(0).getClass());
}

[{name=item1}]
class java.util.LinkedHashMap

I initially encountered this while doing some dynamic class creation via a refineDeserializationType method in a custom AnnotationIntrospector which caused the type from the subclass to be lost.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions