Serialize/Deserialize maps with different types with Jackson
<p>I'm trying to serialize and deserialize different types of maps differently. I want </p>

<pre><code>Map&lt;String, MyInterface&gt; map1;
</code></pre>

<p>to be serialized and deserialized differently than</p>

<pre><code>Map&lt;String, String&gt; map2;
</code></pre>

<p>I have created two serializers </p>

<pre><code>public class MyFirstSerializer extends JsonSerializer&lt;Map&lt;String, Property&gt;&gt; {
    ...
}
</code></pre>

<p>and</p>

<pre><code>public class MySecondSerializer extends JsonSerializer&lt;Map&lt;String, String&gt;&gt; {
    ...
}
</code></pre>

<p>and two deserializers</p>

<pre><code>public class MyFirstDeserializer extends JsonDeserializer&lt;Map&lt;String, Property&gt;&gt; {
    ...
}
</code></pre>

<p>and</p>

<pre><code>public class MySecondDeserializer extends JsonDeserializer&lt;Map&lt;String, String&gt;&gt; {
    ...
}
</code></pre>

<p>I then add them to my <code>Module</code> and specify the types:</p>

<pre><code>SimpleModule module = new SimpleModule("MyModule", new Version(1, 0, 0, "beta"));
module.addSerializer(new MyFirstSerializer());
module.addDeserializer((Class&lt;Map&lt;String, Property&gt;&gt;) (Class&lt;?&gt;) Map.class, new MyFirstDeserializer());

module.addSerializer(new MySecondSerializer());
module.addDeserializer((Class&lt;Map&lt;String, String&gt;&gt;) (Class&lt;?&gt;) Map.class, new MySecondDeserializer());
</code></pre>

<p>The problem is, that maps of type  is sometimes passed to <code>MyFirstSerializer</code> or maps of type  is sometimes passed to <code>MySecondSerializer</code>.</p>

<p>I think the issue is that the types of the maps are erased at run time and there is no way to distinguish between the two different maps. How do I get around this?</p>

<p>I have looked at <a href="https://stackoverflow.com/questions/27871226/serialize-deserialize-mapstring-object-with-jackson">Serialize/Deserialize Map&lt;String, Object&gt; with Jackson</a> but could not find a solution.</p>