title: Gson.toJson gives StackOverFlowError, how to get proper json in this case? (public static class)
body: <p>I currently have the following class:</p>
  
  <pre><code> static final class TabInfo{
     public final String tag;
     public final Class&lt;?&gt; clss;
     public Bundle args;
  
     TabInfo(String _tag, Class&lt;?&gt; _class, Bundle _args) {
         tag = _tag;
         clss = _class;
         args = _args;
     }
   }
  </code></pre>
  
  <p>of which I would like to create json out of.
  In order to do this, I am using the following code:</p>
  
  <pre><code> Gson gson= new Gson();
   Type hmType= new TypeToken&lt;TabInfo&gt;(){}.getType();
   String json = gson.toJson(methodToGetAnInstanceOfTabInfoClassHere, hmType);
  </code></pre>
  
  <p>When I am doing this, I get a java.lang.StackOverFlowError:</p>
  
  <pre><code>E/AndroidRuntime(10353):    at Java.lang.StringBuffer.append(StringBuffer.java:219)
  E/AndroidRuntime(10353):    at java.io.StringWriter.write(StringWriter.java:147)
  E/AndroidRuntime(10353):    at com.google.gson.stream.JsonWriter.string(JsonWriter.java:519)
  E/AndroidRuntime(10353):    at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:387)
  E/AndroidRuntime(10353):    at com.google.gson.stream.JsonWriter.beginObject(JsonWriter.java:300)
  E/AndroidRuntime(10353):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:190)
  E/AndroidRuntime(10353):    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrap    E/AndroidRuntime(20692):     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
  E/AndroidRuntime(20692):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
  </code></pre>
  
  <p>So, my question is: how can I get my gson to create valid json of the java objects from class TabInfo without getting a stackoverflowerror?</p>
  
  <p>Btw. as all of you can see, I have not asked that many questions before, so if you have any feedback for me on how to improve my question: let me know!</p>
  
  <p><strong>EDIT 1:</strong>
  The bundle class is a standard bundle, fi:
       Bundle args = new Bundle();
       args.putint("someint", 1);</p>
  
  <p>See the updated stacktrace...</p>
  
  <p><strong>EDIT 2:</strong>
  If I take a single instance of a TabInfo, by using a String.class as an argument, eg:</p>
  
  <pre><code>  TabInfo test= new TabInfo("test", String.class, new Bundle());
    String result=gson.toJson(test, hmType);
  </code></pre>
  
  <p>Then I get the following stacktrace:</p>
  
  <pre><code>E/AndroidRuntime(22068): java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: java.lang.String. Forgot to register a type adapter?
  E/AndroidRuntime(22068):    at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:64)
  E/AndroidRuntime(22068):    at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:61)
  E/AndroidRuntime(22068):    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
  E/AndroidRuntime(22068):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
  E/AndroidRuntime(22068):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
  E/AndroidRuntime(22068):    at com.google.gson.Gson.toJson(Gson.java:586)
  E/AndroidRuntime(22068):    at com.google.gson.Gson.toJson(Gson.java:565)
  E/AndroidRuntime(22068):    at com.google.gson.Gson.toJson(Gson.java:520)
  </code></pre>
  
  <p>Does this mean that I need to make a TypeToken for all 24 different classes that might be part of a TabInfo class? How do I resolve this?</p>
  
  <p><strong>Edit 3</strong></p>
  
  <p>Ok, thanks to @Zaske, I have found a fix for the first part of my problem. Using another class signature as shown below</p>
  
  <pre><code>static final class TabInfo{
   TabInfo(String _tag, String _class, Bundle _args) {
     tag = _tag;
     clss = _class;
     args = _args;
   }
  
  }
  </code></pre>
  
  <p>Now I can make json out of it, but, when I try to do this by creating the actual HashMap &lt; String, &lt; Stack &lt; TabInfo > > > then again I run into trouble.
  The typetoken i use in that case is:</p>
  
  <pre><code> Type hmType = new TypeToken&lt;HashMap&lt;String, Stack&lt;TabInfo&gt;&gt;&gt;(){}.getType();
  </code></pre>
  
  <p>So, now my final question is: how can I convert a set of tabinfo-stacks in a hashmap to json?</p>
  
  <p><strong>Edit 4</strong>
  Here's some more information:
  The used Bundle class is the Bundle class used in Android to provide arguments to activities and fragments. (See <a href="http://developer.android.com/reference/android/os/Bundle.html" rel="nofollow">http://developer.android.com/reference/android/os/Bundle.html</a>)</p>
  
  <p>If I do</p>
  
  <pre><code> TabInfo test= new TabInfo("test", "String", new Bundle());
   //stage 1 test
   String result=gson.toJson(test);
   Log.d("new result=",result);
  </code></pre>
  
  <p>Then I do get my Json output (see below)</p>
  
  <pre><code> D/new result=(20816): {"args":{"mClassLoader":{"packages":{}},"mMap":{},"mHasFds":false,"mFdsKnown":true,"mAllowFds":true},"clss":"String","tag":"test"}
  </code></pre>
  
  <p>However, when I try to make a Hashmap of stacks out of the TabInfo classes, then it breaks (it runs out of memory...)...</p>