title: Transforming gson to JSON
body: <p>I am having trouble converting gson to JSON, using Google's library 'gson' library. I am using the Java Spark web framework, and ResponseTransformer is a part of the Spark lib. The model argument variable can be any POJO.</p>
  
  <pre><code>public class JsonTransformer implements ResponseTransformer {
  
      private Gson gson = new Gson();
  
      @Override
      public String render(Object model) {
  
          String json = null;
          if (model instanceof java.util.List) {
              model = ((java.util.List) model).get(0);
          }
          try {
              json = gson.toJson(model);
          } catch (Exception e) {
              e.printStackTrace();
              System.exit(5);
          }
  
          return json;
      }
  }
  </code></pre>
  
  <p>there is some sort of problem when model is a List object, the gson library is having trouble converting/serializing it to a json String.</p>
  
  <p>So this is just a quick fix to try to debug this: I am trying to reassign model to the first element of the model List, if model is an instance of List. So this is more of a basic Java question... When I attempt to reassign model to the first element of model, Java is not letting that happen. Why is that so?</p>
  
  <p>Also, if anyone knows why gson is failing so hard in this case, please LMK. This should not be happening. There is no obvious infinite recursion happening.</p>
  
  <p>To prove it, here is the contents:</p>
  
  <p><img src="https://i.stack.imgur.com/hjGx7.png" alt="enter image description here"></p>
  
  <p>I get a infinite recursion stacktrace though:</p>
  
  <p>java.lang.StackOverflowError
   at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
          at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
          at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
          at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
          at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
          at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
   at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:82)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
          at com.google.gson.Gson.getAdapter(Gson.java:356)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:82)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
          at com.google.gson.Gson.getAdapter(Gson.java:356)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:82)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
          at com.google.gson.Gson.getAdapter(Gson.java:356)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:82)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
          at com.google.gson.Gson.getAdapter(Gson.java:356)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:82)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
          at com.google.gson.Gson.getAdapter(Gson.java:356)</p>
  
  <p>the object I am trying to convert from a POJO to JSON looks like this:</p>
  
  <pre><code>public class Model {
  
      private String id;
      private String title;
      private boolean done;
      private Date createdOn;
  
      public Model(BasicDBObject dbObject) {
          this.id = ((ObjectId) dbObject.get("_id")).toString();
          this.title = dbObject.getString("title") == null ? "placeholder title" : dbObject.getString("title");
          this.done = ((Boolean)dbObject.getBoolean("done")) == null ? false : dbObject.getBoolean("done");
          this.createdOn = dbObject.getDate("createdOn") == null ? new Date() : dbObject.getDate("createdOn");
      }
  }
  </code></pre>