user1773926
user1773926

Reputation: 1

GSON on generic types (Java Collection)

I'm trying to save an object of a class using Gson, but unfortunately it doesn't work. My class is implementing the Collection interface using an ArrayList. When i try to get the json string I get an ugly error. Here is some code:

public class Prob1<T> implements Collection<T>{


    ArrayList<T> alb = new ArrayList<>();

    @Override
    public boolean add(T e) {
        // TODO Auto-generated method stub
        alb.add(e);
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends T> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean contains(Object o) {
        // TODO Auto-generated method stub
        return alb.contains(o);
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return alb.isEmpty();
    }

    @Override
    public Iterator<T> iterator() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean remove(Object o) {
        // TODO Auto-generated method stub
        alb.remove(o);
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return alb.size();
    }

    @Override
    public Object[] toArray() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public <T> T[] toArray(T[] a) {
        // TODO Auto-generated method stub
        return null;
    }
    public String toString() {
        return alb.toString();
    }
}

Here it's my attempt to get the JSON string using GSon :

Prob1 p = new Prob1<String>();
    p.add("Johny");
    p.add("Albus");
    p.add("Sirrius");
    System.out.println(p);

    Gson gson = new Gson();
    String json = gson.toJson(p);

And here is the error that i get:

Exception in thread "main" java.lang.NullPointerException at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:95) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60) at com.google.gson.Gson.toJson(Gson.java:586) at com.google.gson.Gson.toJson(Gson.java:565) at com.google.gson.Gson.toJson(Gson.java:520) at com.google.gson.Gson.toJson(Gson.java:500) at Prob1.main(Prob1.java:104)

Any thoughts?

Upvotes: 0

Views: 259

Answers (1)

Aleksander Blomsk&#248;ld
Aleksander Blomsk&#248;ld

Reputation: 18552

You've created a collection which breaks the contract given by the interface. This is a recipe to get strange behaviour in users of your class. I checked the Gson source, and specifically for your case it looks like it goes into a NullPointerException because you're returning null in your iterator() method.

I'm not sure why you're creating your own Collection class, and if you really need to do it. If you do, I would suggest you to extend ForwardingCollection from Google guava, and only override the methods you really need to override.

Upvotes: 3

Related Questions