Collection Deserialization

97 views
Skip to first unread message

Sea

unread,
Apr 16, 2013, 11:59:16 AM4/16/13
to googl...@googlegroups.com
Hello,

I try collection deserialization with this kind of code :

    List<Contact> contacts = jsonToCollection(contactsSt, Contact.class, gson);
   
    private static <T> List<T> jsonToCollection(String input, Class<T> clazz, Gson gson) {
        Type listType = new TypeToken<List<T>>(){}.getType();
        return gson.fromJson(input, listType);
       
    }


But my method returns a Collection of StringMap instead of a list of Contact :(
Any idea to have this kind of method ?

Thanks,

Stéphane

Brandon Mintern

unread,
Apr 16, 2013, 4:57:40 PM4/16/13
to google-gson
This can't work because Java doesn't keep around type information at runtime, *except* when it is part of a class instantiation. That is, TypeToken<List<Contact>> is basically a hack to allow the Java type system to keep the List<Contact> information around.

When you call jsonToCollection with some Class<T>, all that Java sees is Class, and then the TypeToken you construct is effectively TypeToken<List<Object>>. My guess is that Gson's default handling of an Object is to create a StringMap.

There's not a great way around using TypeToken, else Gson would have done it :).



--
You received this message because you are subscribed to the Google Groups "google-gson" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-gson...@googlegroups.com.
To post to this group, send email to googl...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-gson?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Sea

unread,
Apr 17, 2013, 4:19:58 AM4/17/13
to googl...@googlegroups.com
ok, thanks for your answer.

Brandon Mintern

unread,
Apr 17, 2013, 4:28:42 AM4/17/13
to google-gson
If you don't mind it being immutable, you might like:

List<Contact> contacts = Arrays.asList(gson.fromJson(contactsSt, Contact[].class));

better than the TypeToken way. I haven't digged into it, but you might be able to use something like that or something with TypeToken in order to construct the appropriate Class at runtime and implement your jsonToCollection method after all. It's just that TypeToken<List<T>> is definitely not going to get you anywhere because the T is gone at runtime.

Sea

unread,
Apr 18, 2013, 4:21:59 AM4/18/13
to googl...@googlegroups.com
At the end, I use this kind of code :

    private static <T> List<T> jsonToCollection(String input, Class<T> clazz) throws JSONException {
        List<T> list = new ArrayList<T>();
        JSONArray arr = new JSONArray(input);
        for (int i = 0; i < arr.length(); i++) {
            String objectAttributes = arr.getString(i);
            T object = gson.fromJson(objectAttributes, clazz);
            list.add(object);           
        }
       
        return list;
       
    }

Brandon Mintern

unread,
Apr 18, 2013, 1:53:21 PM4/18/13
to google-gson
Is this mixing together org.json and com.google.gson? I haven't done that and don't really recommend it, but if it's working for you, more power to you.


Sea

unread,
Apr 19, 2013, 10:47:05 AM4/19/13
to googl...@googlegroups.com
I think the performance is not really good with this mix but my data are not so huge...

Sea

unread,
Apr 22, 2013, 4:09:17 AM4/22/13
to googl...@googlegroups.com
Here is an implementation without Mix (only Gson based) :

    private static <T> List<T> jsonToCollection(String input, Class<T> clazz) throws JSONException {
        List<T> list = new ArrayList<T>();
        JsonArray jsonArray = new JsonParser().parse(input).getAsJsonArray();
        for (int i = 0; i < jsonArray.size(); i++) {
            JsonElement obj = jsonArray.get(i);
            T object = gson.fromJson(obj, clazz);
            list.add(object);           
        }
       
        return list;
       
    }


Inderjeet Singh

unread,
Apr 24, 2013, 12:54:25 PM4/24/13
to googl...@googlegroups.com
Actually, there is one another way to create parameterized types at run-time. 

Using it, you can write code like this:

private static <T> List<T> jsonToCollection(String json, Class<T> classOfCollectionElement) {
  Type listType = Types.newParameterizedType(List.class, classOfCollectionElement);
  List<T> list = gson.fromJson(json, listType);
  return list;
}

HTH
Inder

----
Reply all
Reply to author
Forward
0 new messages