title: Trouble with Gson serializing an ArrayList of POJO's
body: <p>I had been planning on using simpleXML for my serialization needs, but figured I would try JSON out, to learn something new.  </p>
  
  <p>This is the code I am using to try and serialize an ArrayList of test POJO's using Gson 1.7.1. </p>
  
  <p>Note: I removed the Reader/Writers for a String "s" to simplify the code.</p>
  
  <pre><code>package test;
  
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.List;
  
  import com.google.gson.Gson;
  
  public class TestGsonSerialDeserialList {
      public static void main(String[] args) throws IOException{
          Gson gson = new Gson();
  
          //Make Serial 
          String s;
          List&lt;TestObject&gt; list = Collections.synchronizedList(new ArrayList&lt;TestObject&gt;() );
          list.add(new TestObject());
          list.add(new TestObject());
  
          s = gson.toJson(list, ArrayList.class);
          System.out.println(s);
  
          //Eat Serial
          List&lt;TestObject&gt; list2 = Collections.synchronizedList(gson.fromJson(s, ArrayList.class) );
          System.out.println(list2.get(0) );
          System.out.println(list2.get(1) );
      }
  }
  </code></pre>
  
  <p>Here is the output I get:</p>
  
  <pre><code>[{"objectID":1,"i1":12345,"name":"abcdefg","s":["a","b","c"]},{"objectID":2,"i1":12345,"name":"abcdefg","s":["a","b","c"]}]
  java.lang.Object@5c74c3aa
  java.lang.Object@75d9fd51
  </code></pre>
  
  <p>To my newbie eyes this looks correct. Only, the DeSerialized list of objects contains basic Objects, rather then the TestObject's I serialized. Can anyone please explain to me what, if anything, I can do to make this work?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Corrected to test: Thanks to ColinD</p>
  
  <pre><code>package test;
  
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.io.Reader;
  import java.io.Writer;
  import java.lang.reflect.Type;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.List;
  
  import com.google.gson.Gson;
  import com.google.gson.reflect.TypeToken;
  
  public class TestGsonSerialDeserialList {
      public static void main(String[] args) throws IOException{
          System.out.println("--- Serialize / Deserialize Started ---");
          String fileName = "json\\testList.json";
  
          Gson gson = new Gson();
          Type listOfTestObject = new TypeToken&lt;List&lt;TestObject&gt;&gt;(){}.getType();
  
          //Make Serial 
          Writer osWriter = new OutputStreamWriter( new FileOutputStream(fileName));
          List&lt;TestObject&gt; list = Collections.synchronizedList(new ArrayList&lt;TestObject&gt;() );
          list.add(new TestObject());
          list.add(new TestObject());
          list.add(new TestObject());
          list.add(new TestObject());
          gson.toJson(list, osWriter);
          osWriter.close();
  
  
          //Eat Serial
          Reader isReader = new InputStreamReader( new FileInputStream((fileName) ) );
          List&lt;TestObject&gt; list2 = Collections.synchronizedList(
              (List&lt;TestObject&gt;)gson.fromJson(isReader, listOfTestObject) 
          );
          isReader.close();
          System.out.println(list2.get(0) );
          System.out.println(list2.get(1) );
          System.out.println(list2.get(2) );
          System.out.println(list2.get(3) );
          System.out.println("--- Serialize / Deserialize Ended ---");
      }
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>--- Serialize / Deserialize Started ---
  ID#: 1, i1: 12345, name: abcdefg, s[]: [Ljava.lang.String;@95c083
  ID#: 2, i1: 12345, name: abcdefg, s[]: [Ljava.lang.String;@6791d8c1
  ID#: 3, i1: 12345, name: abcdefg, s[]: [Ljava.lang.String;@182d9c06
  ID#: 4, i1: 12345, name: abcdefg, s[]: [Ljava.lang.String;@5a5e5a50
  --- Serialize / Deserialize Ended ---
  </code></pre>
  
  <p><strong>EDIT2:</strong></p>
  
  <p>I honestly don't know why, but when I replaced the simple String[] embedded in my TestObject with an ArrayList, it started serializing correctly.</p>
  
  <pre><code>--- Serialize / Deserialize Started ---
  ID#: 1, i1: 12345, name: abcdefg, s[]: [a, b, c]
  ID#: 2, i1: 12345, name: abcdefg, s[]: [a, b, c]
  ID#: 3, i1: 12345, name: abcdefg, s[]: [a, b, c]
  ID#: 4, i1: 12345, name: abcdefg, s[]: [a, b, c]
  --- Serialize / Deserialize Ended ---
  </code></pre>