title: Gson exclusion strategy and custom serialization
body: <p>I am using Gson custom serialization on my persistent object. At the same time, I am also using serialization exclusion strategy. The code is as shown below :</p>
  
  <pre><code>public class GsonFactory {
  
      public static Gson build(Type type, final List&lt;String&gt; fieldExclusions,
              final List&lt;Class&lt;?&gt;&gt; classExclusions) {
          GsonBuilder gsonBuilder = new GsonBuilder();
          gsonBuilder.addSerializationExclusionStrategy(new ExclusionStrategy() {
              public boolean shouldSkipField(FieldAttributes f) {
                  return fieldExclusions == null ? false : fieldExclusions
                          .contains(f.getName());
              }
  
              public boolean shouldSkipClass(Class&lt;?&gt; clazz) {
                  return classExclusions == null ? false : classExclusions
                          .contains(clazz);
              }
          });
          // Uncommenting this line will produce error
          // gsonBuilder.registerTypeAdapter(type,
          // new PersistentObjectJsonSerializer());
          return gsonBuilder.create();
      }
  }
  
  class PersistentObjectJsonSerializer implements
          JsonSerializer&lt;PersistentObject&gt; {
  
      public JsonElement serialize(PersistentObject src, Type typeOfSrc,
              JsonSerializationContext context) {
          src.setDT_RowId(src.getId());
          Gson gson = new Gson();
          return gson.toJsonTree(src);
      }
  
  }
  </code></pre>
  
  <p>However if I uncomment the <code>gsonBuilder.registerTypeAdapter(type, new PersistentObjectJsonSerializer());</code>, upon creation of <code>gsonBuilder</code> will give the following error:</p>
  
  <pre><code>java.lang.StackOverflowError
      com.google.gson.reflect.TypeToken.equals(TypeToken.java:284)
      java.util.HashMap.get(HashMap.java:305)
      java.util.Collections$SynchronizedMap.get(Collections.java:1979)
      com.google.gson.Gson.getAdapter(Gson.java:337)
      com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:55)
      com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
      com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
      com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:883)
  </code></pre>
  
  <p>My PersistentObject java class:</p>
  
  <pre><code>@MappedSuperclass
  public abstract class PersistentObject implements Serializable {
  
  ....
  
  @Transient
  protected long DT_RowId;
  // getters, setters not shown
  
  ...
  
  }
  </code></pre>
  
  <p>This is how I call the GsonFactory.build in GenericHibernateDAO:</p>
  
  <pre><code>public abstract class GenericHibernateDAO&lt;T, ID extends Serializable&gt; {
  
  private Class&lt;T&gt; persistentClass;
  
  @SuppressWarnings("unchecked")
  public GenericHibernateDAO() {
      this.persistentClass = (Class&lt;T&gt;) ((ParameterizedType) getClass()
              .getGenericSuperclass()).getActualTypeArguments()[0];
  }
  
  
  public String listAsJsonResponse(){
      ...
      // testing start
      List&lt;Class&lt;?&gt;&gt; classExclusions = new ArrayList&lt;Class&lt;?&gt;&gt;();
      classExclusions.add(Role.class);
      List&lt;String&gt; fieldExclusions = new ArrayList&lt;String&gt;();
      fieldExclusions.add("users");
      fieldExclusions.add("password");
      Gson gson = GsonFactory.build(persistentClass,fieldExclusions, null);
      ...
      return jsonResponse.toString();
  }
  }
  </code></pre>
  
  <p>The <code>persistentClass</code> here refers to <code>User</code> class:</p>
  
  <pre><code>@Repository
  public class UserDAO extends GenericHibernateDAO&lt;User, Long&gt; {
  
  }
  </code></pre>
  
  <p>Basically I can't manage to use both function at the same time. Any pointer on what might cause this issue ?</p>