Gson issue serializing ArrayList from POJO

I planned to use simpleXML for my serialization tasks, but decided that I would try JSON to learn something new.

This is the code I use to try to serialize the ArrayList of a POJO test using Gson 1.7.1.

Note. I removed Reader / Writers for the line to simplify the 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<TestObject> list = Collections.synchronizedList(new ArrayList<TestObject>() ); list.add(new TestObject()); list.add(new TestObject()); s = gson.toJson(list, ArrayList.class); System.out.println(s); //Eat Serial List<TestObject> list2 = Collections.synchronizedList(gson.fromJson(s, ArrayList.class) ); System.out.println(list2.get(0) ); System.out.println(list2.get(1) ); } } 

Here is the result I get:

 [{"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 

To my newcomers, this looks right. Only, the list of DeSerialized objects contains the base objects, and TestObject I is not serialized. Can someone explain to me what, if anything, I can do to make this work?

EDIT:

Fixed for validation: thanks ColinD

 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<List<TestObject>>(){}.getType(); //Make Serial Writer osWriter = new OutputStreamWriter( new FileOutputStream(fileName)); List<TestObject> list = Collections.synchronizedList(new ArrayList<TestObject>() ); 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<TestObject> list2 = Collections.synchronizedList( (List<TestObject>)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 ---"); } } 

exit:

 --- 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 --- 

EDIT2:

I honestly don’t know why, but when I replaced the simple String [] built into my TestObject with an ArrayList, it started to serialize correctly.

 --- 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 --- 
+61
java json arraylist gson serialization
Apr 28 2018-11-11T00:
source share
1 answer

You need to provide Gson information about the specific generic List type that you use (or some general type that you use with it). In particular, with JSON deserialization, this information should be able to determine what type of object it should deserialize each element of the array.

 Type listOfTestObject = new TypeToken<List<TestObject>>(){}.getType(); String s = gson.toJson(list, listOfTestObject); List<TestObject> list2 = gson.fromJson(s, listOfTestObject); 

This is described in the Gson User Guide .

+149
Apr 28 2018-11-11T00:
source share



All Articles