title: Java - deserialization into dynamic nested generic types with ObjectMapper
body: <p>I am writing a general JSON deserialization, using ObjectMapper (of com.fasterxml.jackson library)
  The functions receives the objects type and the Collection/map type as parameters.</p>
  
  <p>This is my code:</p>
  
  <pre><code>// Reading a single object from JSON String
  public static &lt;T&gt; Object readObjectFromString(String string, Class&lt;T&gt; type) {
      try {
              return objectMapper.readValue(string, type);
      } catch (Exception e) {
              e.printStackTrace();
              return null;
      }
  }
  
  // Reading a Collection object from JSON String
  public static &lt;T&gt; Object readCollectionObjectsFromString(String string,  Class&lt;? extends Collection&gt; collectionType, Class&lt;T&gt; type) {
      try {
              CollectionType typeReference =
                      TypeFactory.defaultInstance().constructCollectionType(collectionType, type);
              return objectMapper.readValue(string, typeReference);
      } catch (Exception e) {
              e.printStackTrace();
              return null;
      }
  }
  
  // Reading a Map object from JSON String
  public static &lt;T&gt; Object readCollectionObjectsFromString(String string, Class&lt;? extends Map&gt; mapType, Class&lt;T&gt; keyType, Class&lt;T&gt; valueType) {
      try {
              MapType typeReference =
                      TypeFactory.defaultInstance().constructMapType(mapType, keyType, valueType);
              return objectMapper.readValue(string, typeReference);
      } catch (Exception e) {
              e.printStackTrace();
              return null;
      }
  }
  </code></pre>
  
  <p>But What if the user needs to deserialized a complicated nested generic object, something like:</p>
  
  <pre><code>Map&lt;A,List&lt;Map&lt;B,C&gt;&gt;&gt; nestedGenericObject1
  List&lt;Map&lt;A,B&gt;&gt; nestedGenericObject2
  Map&lt;List&lt;A&gt;,List&lt;B&gt;&gt; nestedGenericObject3
  // etc...
  </code></pre>
  
  <p>How to implement it as a general selution?</p>