Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I give type hints to the Jackson deserializer?
    primarykey
    data
    text
    <p>I'm using Jackson as a tool to declare some objects whose classes I can't annotate (or modify at all). One of the classes has a setter and getter for an untyped list. Here's a sanitized version:</p> <pre><code>public class Family { private List members; public List getMembers() { return members; } public void setMembers(List members) { this.members = members; } //...many, many other properties } public class Member { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } </code></pre> <p>Here's the JSON I'm trying to deserialize:</p> <pre><code>{ "members" : [ { "name" : "Mark" } ] } </code></pre> <p>The naive code I would use is this:</p> <pre><code>ObjectMapper mapper = new ObjectMapper(); Family family = mapper.readValue(json, Family.class); Member member = (Member) family.getMembers().get(0); System.out.println(member.getName()); </code></pre> <p>But of course this fails, as Jackson did not know to create a list of <code>Members</code> instead of its fallback, a list of <code>LinkedHashMap</code>s.</p> <p><strong>What's the easiest way to instruct Jackson to treat <code>members</code> as a <code>List&lt;Member&gt;</code>?</strong> I don't think I want to use a fully custom deserializer for the class, since there are many other properties that Jackson handles fine.</p> <p>Here's the best I could come up with, using BeanDeserializerModifier:</p> <pre><code>mapper.setDeserializerProvider(new StdDeserializerProvider() .withDeserializerModifier(new BeanDeserializerModifier() { @Override public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BasicBeanDescription beanDesc, BeanDeserializerBuilder builder) { if (beanDesc.getBeanClass() == Family.class) { CollectionType type = CollectionType.construct(ArrayList.class, SimpleType.construct(Member.class)); TypeDeserializer typeDeserializer = type.getTypeHandler(); SettableBeanProperty.MethodProperty membersProperty = (SettableBeanProperty.MethodProperty) builder.removeProperty("members"); builder.addProperty(new SettableBeanProperty.MethodProperty( "members", type, typeDeserializer, beanDesc.getClassAnnotations(), (AnnotatedMethod) membersProperty.getMember() )); } return builder; }})); </code></pre> <p>It works, but seems really low level (and verbose!) for what I'm trying to do. What am I missing here?</p> <h2>Edit</h2> <p>I should note, I'm using Jackson 1.8.2, but could update if there's a compelling reason to.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload