title: Java Gson encode with Object is not working
body: <p>The following code is working</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import com.google.gson.Gson;
  
  class DataObject&lt;T&gt; {
  
      private int data1 = 100;
      private String data2 = "hello";
  DataObject child;
  private List&lt;String&gt; list = new ArrayList&lt;String&gt;() {
    {
      add("String 1");
      add("String 2");
      add("String 3");
        }
      };
      private Map&lt;String, DataObject&gt; data=null;
  
      public DataObject(int i){
          this.data1 = i;
          this.data = new HashMap&lt;String, DataObject&gt;();
      }
  
      //getter and setter methods
  
      @Override
      public String toString() {
         return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list="
      + list + "]";
      }
      public Map&lt;String, DataObject&gt; getData() {
          return data;
      }
  
      public void addData(final String key, DataObject value, Class&lt;T&gt; t) {
          data.put(key, value);
      }
  }
  
  public class test {
  
  
  
      /**
   * @param args
   */
  public static void main(String[] args) {    
      DataObject obj = new DataObject(12);
      obj.child = new DataObject(25);
      obj.addData("myOtherData", new DataObject(32), DataObject.class);
          Gson gson = new Gson();
          System.out.println(gson.toJson(obj));
      }
  
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>{"data1":12,"data2":"hello","child":{"data1":25,"data2":"hello","list":["String 1","String 2","String 3"],"data":{}},"list":["String 1","String 2","String 3"],"data":{"myOtherData":{"data1":32,"data2":"hello","list":["String 1","String 2","String 3"],"data":{}}}}
  </code></pre>
  
  <p>but I actually need to make the work with a generic </p>
  
  <pre><code>Map&lt;String, Object&gt; data
  </code></pre>
  
  <p>So this code:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import com.google.gson.Gson;
  
  class DataObject&lt;T&gt; {
  
      private int data1 = 100;
      private String data2 = "hello";
  DataObject child;
  private List&lt;String&gt; list = new ArrayList&lt;String&gt;() {
    {
      add("String 1");
      add("String 2");
      add("String 3");
        }
      };
      private Map&lt;String, Object&gt; data=null;
  
      public DataObject(int i){
          this.data1 = i;
          this.data = new HashMap&lt;String, Object&gt;();
      }
  
      //getter and setter methods
  
      @Override
      public String toString() {
         return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list="
      + list + "]";
      }
      public Map&lt;String, Object&gt; getData() {
          return data;
      }
  
      public void addData(final String key, Object value, Class&lt;T&gt; t) {
          data.put(key, value);
      }
  }
  
  public class test {
  
  
  
      /**
   * @param args
   */
  public static void main(String[] args) {    
      DataObject obj = new DataObject(12);
      obj.child = new DataObject(25);
      obj.addData("myOtherData", new DataObject(32), DataObject.class);
          Gson gson = new Gson();
          System.out.println(gson.toJson(obj));
      }
  
  }
  </code></pre>
  
  <p>which is not working:
  output:</p>
  
  <pre><code>{"data1":12,"data2":"hello","child":{"data1":25,"data2":"hello","list":["String 1","String 2","String 3"],"data":{}},"list":["String 1","String 2","String 3"],"data":{"myOtherData":{}}}
  </code></pre>
  
  <p>myOtherData object is missing, because Gson cannot work with general Object</p>
  
  <p>That's why I started putting the class as third argument of the addData method, I would need to put </p>
  
  <pre><code>Map&lt;String, &lt;T&gt;&gt; data;
  </code></pre>
  
  <p>I don't know the way to declare a configurable type in the map</p>
  
  <p>thanks for anyone who will make that work</p>
  
  <hr>
  
  <p>edit : it's almost working like that</p>
  
  <p>I'm getting the error 
  Exception in thread "main" java.lang.ClassCastException: DataObject cannot be cast to java.lang.Class
      at DataObject.addData(test.java:41)</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import com.google.gson.Gson;
  
  class DataObject&lt;T&gt; {
  
      private int data1 = 100;
      private String data2 = "hello";
  DataObject child;
  private List&lt;String&gt; list = new ArrayList&lt;String&gt;() {
    {
      add("String 1");
      add("String 2");
      add("String 3");
        }
      };
      private Map&lt;String, Class&lt;T&gt;&gt; data=null;
  
      public DataObject(int i){
          this.data1 = i;
          this.data = new HashMap&lt;String, Class&lt;T&gt;&gt;();
      }
  
      //getter and setter methods
  
      @Override
      public String toString() {
         return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list="
      + list + "]";
      }
      public Map&lt;String, Class&lt;T&gt;&gt; getData() {
          return data;
      }
  
      public void addData(final String key, Object value, Class&lt;T&gt; t) {
          data.put(key, (Class&lt;T&gt;) value);
      }
  }
  
  public class test {
  
  
  
      /**
   * @param args
   */
  public static void main(String[] args) {
  
      DataObject obj = new DataObject(12);
      obj.child = new DataObject(25);
      obj.addData("myOtherData", new DataObject(32), DataObject.class);
          Gson gson = new Gson();
          System.out.println(gson.toJson(obj));
      }
  
  }
  </code></pre>