title: GSON Serialization very very slow
body: <p>I'm trying to serialize an array of 7000 POJO using GSON and the serialization time is extremely slow. It's on the order of 3-5 seconds to serialize an array of the following object:</p>
  
  <pre><code>public class Case {
      private Long caseId;
      private Key&lt;Organization&gt; orgKey;
  
      private Key&lt;Workflow&gt; workflowKey;
      private Key&lt;User&gt; creatorKey;
  
      private Date creationTimestamp;
      private Date lastUpdatedTimestamp;
  
      private String name;
      private String stage;
      private String notes;
  }
  </code></pre>
  
  <p>The key fields are serialized using a custom serializer/deserializer:</p>
  
  <pre><code>public class GsonKeySerializerDeserializer implements JsonSerializer&lt;Key&lt;?&gt;&gt;, JsonDeserializer&lt;Key&lt;?&gt;&gt;{
  
  @Override
  public JsonElement serialize(Key&lt;?&gt; src, Type typeOfSrc, JsonSerializationContext arg2) {
      return new JsonPrimitive(src.getString());
  }
  
  @Override
  public Key&lt;?&gt; deserialize(JsonElement src, Type typeOfSrc, JsonDeserializationContext arg2) throws JsonParseException {
      if (src.isJsonNull() || src.getAsString().isEmpty()) {
          return null;
      }
  
      String s = src.getAsString();
      com.google.appengine.api.datastore.Key k = KeyFactory.stringToKey(s);
      return new Key(k);
  }
  }
  </code></pre>
  
  <p>To test the performance against hand writing a JSON serializer, I tested the following code and it could serialize the same array of Case objects approximately 10x faster than GSON. </p>
  
  <pre><code>List&lt;Case&gt; cases = (List&lt;Case&gt;) retVal;
  JSONArray a = new JSONArray();
  for (Case c : cases) {
      JSONObject o = new JSONObject();
      o.put("caseId", c.getCaseId());
      o.put("orgKey", c.getOrgKey().getString());
      o.put("workflowKey", c.getWorkflowKey().getString());
      o.put("creatorKey", c.getCreatorKey().getString());
      o.put("creationTimestamp", c.getCreationTimestamp().getTime());
      o.put("lastUpdatedTimestamp", c.getLastUpdatedTimestamp().getTime());
      o.put("name", c.getName());
      o.put("stage", c.getStage());
      o.put("notes", c.getNotes());
      a.put(o);
  
  }
  String json = a.toString();
  </code></pre>
  
  <p>Any ideas why GSON is performing so badly in this case?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Here's the code that actually starts the serialization:</p>
  
  <pre><code>Object retVal = someFunctionThatReturnsAList();
  String json = g.toJson(retVal);
  resp.getWriter().print(json);
  </code></pre>
  
  <p><strong>UPDATE2</strong></p>
  
  <p>Here's a very simple test case that illustrates the poor performance relative to org.json:</p>
  
  <pre><code>List&lt;Foo&gt; list = new ArrayList&lt;Foo&gt;();
  for (int i = 0; i &lt; 7001; i++) {
      Foo f = new Foo();
      f.id = new Long(i);
      list.add(f);
  }
  
  Gson gs = new Gson();
  long start = System.currentTimeMillis();
  String s = gs.toJson(list);
  System.out.println("Serialization time using Gson: " + ((double) (System.currentTimeMillis() - start) / 1000));
  
  
  start = System.currentTimeMillis();
  JSONArray a = new JSONArray();
  for (Foo f : list) {
      JSONObject o = new JSONObject();
      o.put("id", f.id);
      a.put(o);
  
  }
  String json = a.toString();
  System.out.println("Serialization time using org.json: " + ((double) (System.currentTimeMillis() - start) / 1000));
  
  System.out.println(json.equals(s));
  </code></pre>
  
  <p>Where Foo is: </p>
  
  <pre><code>public class Foo {
  public Long id;
  }
  </code></pre>
  
  <p>This outputs:</p>
  
  <pre><code>Serialization time using Gson: 0.233
  Serialization time using org.json: 0.028
  true
  </code></pre>
  
  <p>An almost 10x performance difference!</p>