public
Last active

Real Collection Deserializer to parse arrays with GSON

  • Download Gist
CollectionDeserializer.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
 
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
 
public class CollectionDeserializer implements JsonDeserializer<Collection<?>> {
 
@Override
public Collection<?> deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
Type realType = ((ParameterizedType)typeOfT).getActualTypeArguments()[0];
return parseAsArrayList(json, realType);
}
 
/**
* @param serializedData
* @param type
* @return
*/
@SuppressWarnings("unchecked")
public <T> ArrayList<T> parseAsArrayList(JsonElement json, T type) {
ArrayList<T> newArray = new ArrayList<T>();
Gson gson = new Gson();
 
JsonArray array= json.getAsJsonArray();
Iterator<JsonElement> iterator = array.iterator();
 
while(iterator.hasNext()){
JsonElement json2 = (JsonElement)iterator.next();
T object = (T) gson.fromJson(json2, (Class<?>)type);
newArray.add(object);
}
 
return newArray;
}
}
JSONParsingTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 
public class JSONParsingTest {
List<World> worlds;
@Test
public void grantThatDeserializerWorksAndParseObjectArrays(){
 
String worldAsString = "{\"worlds\": [" +
"{\"name\":\"name1\",\"id\":1}," +
"{\"name\":\"name2\",\"id\":2}," +
"{\"name\":\"name3\",\"id\":3}" +
"]}";
 
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Collection.class, new CollectionDeserializer());
Gson gson = builder.create();
Object decoded = gson.fromJson((String)worldAsString, JSONParsingTest.class);
assertNotNull(decoded);
assertTrue(JSONParsingTest.class.isInstance(decoded));
JSONParsingTest decodedObject = (JSONParsingTest)decoded;
assertEquals(3, decodedObject.worlds.size());
assertEquals((Long)2L, decodedObject.worlds.get(1).getId());
}
}
World.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
public class World {
private String name;
private Long id;
 
public void setName(String name) {
this.name = name;
}
 
public String getName() {
return name;
}
 
public Long getId() {
return id;
}
 
public void setId(Long id) {
this.id = id;
}
}

Nice solution, I used it but I got error on: CollectionDeserializer.java
...
@Override
public Collection<?> deserialize(JsonElement json, Type typeOfT,
...

error:
The method deserialize(JsonElement, Type, JsonDeserializationContext) of type CollectionDeserializer must override a superclass method

Any idea?

Thanks in advance

Hi @amrafu
Sorry by the late, I missed up your answer notification.

Please, which version of GSon are you using?
I will try to simulate your problem to help you! ;)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.