Jackson deserializer for generic type
<p>I need to write a custom deserializer for a class with generics. I couldn't find a way to do this, however I cannot imagine I'm the only one with this problem. As far as I've thought through it, there would be two ways to implement this, yet none of them are implementable:</p>

<ol>
<li>Providing a Class parameter to the deserializer in the constructor of the deserializer doesn't work, because when registering the deserializer, the relationship between Type.class and the instance of the deserializer with the passed Class instance is lost.</li>
</ol>

<p>For example:</p>

<pre><code>public class Foo&lt;T&gt; {}
public class FooDeserializer&lt;T&gt; {
    public FooDeserializer(Class&lt;T&gt; type) { ... }
    ...
}
// Boilerplate code...
module.addDeserializer(Foo.class, new FooDeserializer&lt;Bar1&gt;(Bar1.class));
module.addDeserializer(Foo.class, new FooDeserializer&lt;Bar2&gt;(Bar2.class));
</code></pre>

<p>This doesn't work, when an ObjectMapper instance gets an instance of Foo, there's no type information of the generic parameter available (type erasure) so it simply chooses the last deserializer registered.</p>

<ol start="2">
<li>It doesn't help to keep a reference of the generic type in the class because an instantiated version of the class cannot be passed to the deserializer (the interface is readValue(String, Class)).</li>
</ol>

<p>For example:</p>

<pre><code>String json = "...";
ObjectMapper mapper = ...;
Foo&lt;Bar1&gt; foo = new Foo&lt;&gt;(Bar1.class);
foo = mapper.readValue(json, Foo.class); // Can't pass empty foo instance with Class&lt;?&gt; field containing Bar1.class
</code></pre>

<p>Something like this would be needed:</p>

<pre><code>mapper.readValue(json, Foo.class, Bar1.class); // Doesn't exist in jackson
</code></pre>

<p>Any suggestions how to do this?</p>

<p>EDIT:
I found a way to solve the problem, however it's not a clean solution:</p>

<p>I extend the FooDeserializer with a Class field to save the type of Foo's generic parameter. Then, every time I want to deserialize some json into a new Foo instance, I get a new ObjectMapper instance (I use ObjectMapper#copy on a preconfigured instance from a factory) and pass it a new Module which contains an instance of the FooDeserializer with the class parameter (I know the type at this time). Module, FooDeserializer and the ObjectMapper copy are short living, they only get instantiated for this single deserialization action. As I said, not very clean, but still better than subclassing Foo many times and writing a deserializer for each.</p>

<p>Example:</p>

<pre><code>public class FooDeserializer&lt;T&gt; extends StdDeserializer&lt;T&gt; {
    private Class&lt;T&gt; type;
    public FooDeserializer(Class&lt;T&gt; type) { this.type = type }
    ...
}

// Meanwhile, before deserialization:
ObjectMapper mapper = MyObjectMapperFactory.get().copy();
SimpleModule module = new SimpleModule(new Version(0,0,1,null,null,null);
module.addDeserializer(Foo.class, new FooDeserializer(Bar1.class);
mapper.addModule(module);
Foo&lt;Bar1&gt; x = mapper.readValue(json, Foo.class); 
</code></pre>

<p>Probably putting this into a utility method to hide the uglyness.</p>