title: gson deserialize object hierarchy
body: <p>I have the following Classes:</p>
  
  <p>Class: TestClass:</p>
  
  <pre><code>public class TestXClass {
  
      Map&lt;String, NestedClass&gt; embeddedMap1;
      Map&lt;String, List&lt;NestedClass&gt;&gt; embeddedMap2;
  
      @Override
      public String toString() {
          return "TestClass [embeddedMap1=" + embeddedMap1 + "(" + embeddedMap1.getClass() + "), embeddedMap2=" + embeddedMap2 + "(" + embeddedMap2.getClass()+ ")]";
      }
  }
  </code></pre>
  
  <p>Class: NestedClass:</p>
  
  <pre><code>public class NestedClass {
  
      Map&lt;String, Integer&gt; embeddedMap;
  
      @Override
      public String toString() {
          return "NestedClass [embeddedMap=" + embeddedMap + "("+ 
      embeddedMap.getClass() +")]";
      }
  }
  </code></pre>
  
  <p>I want to serialize and deserialize an object of TestClass to and from JSON with gson.</p>
  
  <p>This is my testcode:</p>
  
  <pre><code>public static void main(String[] args) {
      Gson gson = new GsonBuilder().setPrettyPrinting().create();
  
      TestClass test = new TestClass();
      test.embeddedMap1 = new HashMap&lt;&gt;();
  
      NestedClass testNested1 = new NestedClass();
      testNested1.embeddedMap = new HashMap&lt;&gt;();
      testNested1.embeddedMap.put("X", 1);
      test.embeddedMap1.put("A", testNested1);
  
      List&lt;NestedClass&gt; testNestedList = new ArrayList&lt;&gt;();
      NestedClass listElement = new NestedClass();
      listElement.embeddedMap = new HashMap&lt;&gt;();
      listElement.embeddedMap.put("ABC", 3);
      testNestedList.add(listElement);
  
      test.embeddedMap2 = new HashMap&lt;&gt;();
      test.embeddedMap2.put("Z", testNestedList);
  
      String jsonString = gson.toJson(test);
  
      System.out.println(jsonString);
  
      TestClass fromJson = gson.fromJson(jsonString, TestClass.class);
      System.out.println(fromJson);
  }
  </code></pre>
  
  <p>The serialization to Json works fine:</p>
  
  <pre><code>{
    "embeddedMap1": {
      "A": {
        "embeddedMap": {
          "X": 1
        }
      }
    },
    "embeddedMap2": {
      "Z": [
        {
          "embeddedMap": {
            "ABC": 3
          }
        }
      ]
    }
  }
  </code></pre>
  
  <p>But when I try to deserialize it to my TestClass, I have the following output:</p>
  
  <blockquote>
    <p>TestClass [embeddedMap1= {A=NestedClass [embeddedMap={X=1}(class
    com.google.gson.internal.LinkedTreeMap)]}(class
    com.google.gson.internal.LinkedTreeMap), embeddedMap2={Z=[NestedClass
    [embeddedMap={ABC=3}(class
    com.google.gson.internal.LinkedTreeMap)]]}(class
    com.google.gson.internal.LinkedTreeMap)]</p>
  </blockquote>
  
  <p>Every Map or List is Replaced by a type of gson.</p>
  
  <p>How can I deserialize such an object structure to plain java types?</p>