These are the Classes which i'm working with:
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);
}
}
others class extends Resource -> Wood, Stone, Servant
and Then i have a Class that is a group of this Classes
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;
}
Now My question is: How can i Deserialize a json file(using Gson) to create a ResourceSet object ? File json:
"resourcePurchase": {
"coins": {
"valore": 1},
"wood": {
"valore": 2},
"stone": {
"valore": 3},
"servant": {
"valore": 4},
"resourceset": [
{
"valore": 3},
{
"valore": 1},
{
"valore": 2},
{
"valore": 4}
]
}
i've tried this code but it gives me an error:
""Exception in thread "main" java.lang.RuntimeException: Unable to invoke no-args constructor for class it.polimi.ingsw.ps13.Resourcesfolder.Resource. Register an InstanceCreator with Gson for this type may fix this problem.""
Collection <Resource> set = null;
BufferedReader br = new BufferedReader(new FileReader("ResourceSet.json"));
set = gson.fromJson(br, new TypeToken<HashSet<Resource>>(){}.getType());