用Gson反序列化json文件和抽象类的HashSet(Java)

时间:2017-05-19 08:33:02

标签: java json gson deserialization hashset

这些是我正在使用的课程:

public abstract class Resource{

private int valore;

public Resource(int valore){
    this.valore=valore;
}
public void add(int elem){
    this.valore = this.valore + elem;
}
public int get(){
    return this.valore;
}

/////

public class Coin extends Resource {

public Coin(int initialCoins){
    super(initialCoins);
}
}   

其他类扩展资源 - >木头,石头,仆人

然后我有一个Class,它是这个类的一组

public class ResourceSet {

private Coin coins;
private Wood wood;
private Stone stone;
private Servant servant;
private Set <Resource> resourceset;

public ResourceSet(int coins, int wood, int stone, int servant){
    this.coins = new Coin(coins);
    this.wood = new Wood(wood);
    this.stone = new Stone(stone);
    this.servant = new Servant(servant);

    resourceset = new HashSet <Resource> ();
    resourceset.add(this.coins);
    resourceset.add(this.wood);
    resourceset.add(this.stone);
    resourceset.add(this.servant);
}

public Set<Resource> getSetofResource(){
    //restituisce insieme resourceset contenente tutte le risorse
    return resourceset;
}

public Coin getCoin(){
    return this.coins;
}
public Wood getWood(){
    return this.wood;
}
public Stone getStone(){
    return this.stone;
}
public Servant getServant(){
    return this.servant;
}

现在我的问题是:我如何反序列化一个json文件(使用Gson)来创建一个ResourceSet对象? 文件json:

"resourcePurchase": {
 "coins": {
    "valore": 1},
 "wood": {
    "valore": 2},
 "stone": {
    "valore": 3},
 "servant": {
    "valore": 4},
 "resourceset": [
    {
      "valore": 3},
    {
      "valore": 1},
    {
      "valore": 2},
    {
      "valore": 4}
  ]
} 

我尝试过这段代码,但它给了我一个错误:

“”线程中的异常“main”java.lang.RuntimeException:无法为类it.polimi.ingsw.ps13.Resourcesfolder.Resource调用no-args构造函数。使用Gson为此类型注册InstanceCreator可以解决此问题。“”

Collection <Resource> set = null;
BufferedReader br = new BufferedReader(new FileReader("ResourceSet.json"));
set = gson.fromJson(br, new TypeToken<HashSet<Resource>>(){}.getType());

1 个答案:

答案 0 :(得分:0)

public class ResourceSetDeserializer implements JsonDeserializer<ResourceSet>{

    @Override
    public ResourceSet deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException{

        JsonObject jObj = (JsonObject) json;


            ResourceSet set = new ResourceSet(0,0,0,0);

            Resource c = new Coin(jObj.get("coin").getAsInt());
            Wood w = new Wood(jObj.get("wood").getAsInt());
            Stone s = new Stone(jObj.get("stone").getAsInt());
            Servant se = new Servant(jObj.get("servant").getAsInt());

            set.addResources(c);
            set.addResources(s);
            set.addResources(w);
            set.addResources(se);


        return set;
    }
}

这实际上对我有用。

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(ResourceSet.class, new ResourceSetDeserializer());
Gson gson = gsonBuilder.create();
BufferedReader br = new BufferedReader(new FileReader("ResourceSet.json"));
    ResourceSet response = gson.fromJson(br, ResourceSet.class);
    response.printAllResources();

这是使用的.json文件

{
  "coin": 3,
  "wood": 1,
  "stone": 2,
  "servant": 1
}