2012-12-25 129 views
0

我試圖用Gson保存一個類的對象,但不幸的是它不起作用。 我的類正在使用ArrayList實現Collection接口。當我嘗試獲取json字符串時,我得到一個醜陋的錯誤。下面是一些代碼:泛型類型的GSON(Java集合)

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(); 
} 

這是我試圖讓JSON字符串中使用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); 

,這裏是我得到的錯誤:在線程

異常「 main「java.lang.NullPointerException at com.google.gson.internal.bind.CollectionTypeAdapterFactory $ Adapter.write(CollectionTypeAdapterFactory.java:95) at com.google.gson.internal.bind.Collection TypeAdapterFactory $ 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) 在com.google.gson.Gson.toJson(Gson.java:500) 在Prob1.main(Prob1.java:104)

有什麼想法?

+0

問題不在於Gson,而在於你的'Collection'實現(下面的答案提示)。你不能從'iterator()'返回'null'。 Gson沒有任何問題序列化/反序列化集合,只要它們被正確編寫並遵守界面。 –

+0

另外...你確定你應該使用'ArrayList'嗎?那*是一個'集合'。如果是這種情況,你可以直接從'iterator()'方法返回'Iterator'。 –

回答

3

您創建了一個集合,該集合違反了interface給出的合同。這是在你的班級的用戶中得到奇怪行爲的祕訣。我檢查了Gson源代碼,特別是針對你的情況,它看起來像進入NullPointerException,因爲你在你的iterator()方法中返回null。

我不確定你爲什麼要創建自己的Collection類,以及是否真的需要這樣做。如果你這樣做,我建議你從谷歌guava延伸ForwardingCollection,並且只覆蓋你真正需要覆蓋的方法。

+0

我必須用一個ArrayList來實現一個作業集合類,我需要能夠只添加同一個類的對象(這就是爲什麼我使用泛型類型)。如果我使用ForwardingCollection,我將能夠使用GSON? – user1773926