title: GSON Serialization Performance is Terrible - 161 seconds for 1499 objects
body: <p>I have read tons of topics discussing the performance of GSON, and many have suggested to use streaming if you are worried about performance. It seems like nobody is having a performance issue to the degree of mine, though.</p>
  
  <p>Here's how I am initializing my GSON object:</p>
  
  <pre><code>gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
  </code></pre>
  
  <p>Here is the <code>.toJson()</code> statement I am executing that is the only statement in the benchmark results I am about to show:</p>
  
  <pre><code>String resultString = gson.toJson(results.get(0));
  </code></pre>
  
  <p>... where <code>results.get(0)</code> evaluates to a <code>List&lt;PrblFldr&gt;</code> object that contains 1499 <code>PrblFldr</code> objects (I will show the non-primitive classes that are serialized later). Here are the results from my makeshift benchmark:</p>
  
  <pre><code>INFO: Starting: GSON CONVERSION
  INFO: Finished: GSON CONVERSION  ---  Time: 161705 ms
  </code></pre>
  
  <p>Finally, here are the classes I am serializing. I am only showing those with the <code>@Expose</code> annotation since those are the only fields that should be serialized.</p>
  
  <p>PrblFldr:</p>
  
  <pre><code>public class PrblFldr implements Serializable {
  
      @Expose
      @SerializedName("name")
      @Column(name="FLDR_NM")
      private String fldrNm;
  
      @Expose
      @SerializedName("template")
      @ManyToOne
      @JoinColumn(name="FLDR_TYP_SEQ_ID", insertable=false, updatable=false)
      private PrblTmplt prblTmplt;
  
      @Expose
      @SerializedName("attributes")
      @OneToMany(mappedBy="prblFldr")
      private List&lt;PrblFldrAtrbtVal&gt; prblFldrAtrbtVals;
  
      // other fields omitted...
  }
  </code></pre>
  
  <p>PrblTmplt:</p>
  
  <pre><code>public class PrblTmplt implements Serializable {
  
      @Expose
      @SerializedName("id")
      @Id
      @Column(name="TMPLT_SEQ_ID")
      private long tmpltSeqId;
  
      @Expose
      @SerializedName("name")
      @Column(name="TMPLT_NM")
      private String tmpltNm;
  
      // other fields omitted...
  }
  </code></pre>
  
  <p>PrblFldrAtrbtVal:</p>
  
  <pre><code>public class PrblFldrAtrbtVal implements Serializable {
  
      @Expose
      @SerializedName("id")
      @Id
      @Column(name="FLDR_ATRBT_VAL_SEQ_ID")
      private long fldrAtrbtValSeqId;
  
      @Expose
      @SerializedName("val")
      @Column(name="FLDR_ATRBT_VAL")
      private String fldrAtrbtVal;
  
      @Expose
      @SerializedName("attributeTemplate")
      @ManyToOne
      @JoinColumn(name="FLDR_ATRBT_SEQ_ID")
      private PrblTmpltAtrbt prblTmpltAtrbt;
  
      // other fields omitted...
  }
  </code></pre>
  
  <p>And finally, PrblTmpltAtrbt:</p>
  
  <pre><code>public class PrblTmpltAtrbt implements Serializable {
  
      @Expose
      @SerializedName("id")
      @Id
      @Column(name="TMPLT_ATRBT_SEQ_ID")
      private long tmpltAtrbtSeqId;
  
      @Expose
      @SerializedName("name")
      @Column(name="TMPLT_ATRBT_LBL_NM")
      private String tmpltAtrbtLblNm;
  
      @Expose
      @SerializedName("required")
      @Column(name="TMPLT_ATRBT_RQRD_IND")
      private String tmpltAtrbtRqrdInd;
  
      // other fields omitted...
  }
  </code></pre>
  
  <p>As you can see, everything breaks down to either a <code>long</code> or a <code>String</code> object. Even though there are 4 layers of object serialization going on here, I still don't think I should be seeing such a drastically long serialization time. Also, as you might have noticed, I am using EclipseLink JPA (hence the <code>@Id</code> and <code>@Column</code> annotations). Could that be a source of the issue?</p>
  
  <p><strong>Question:</strong> What is causing this extremely long serialization time?</p>
  
  <p><strong>EDIT:</strong> It also might be worth noting that the <code>List&lt;PrblFlderAtrbtVal&gt; prblFldrAtrbtVals</code> field in the <code>PrblFldr</code> class probably has an average size of 5 or 6, with a maximum of 15 or so.</p>