I'm trying to make a 2D tile game and I was trying to add items by importing them from a JSON file. I tried importing the JSON file using GSON library, but whenever I run the code I get the following error:
java.lang.IllegalArgumentException: class javax.swing.JFrame declares multiple JSON fields named state
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at com.google.gson.Gson.getAdapter(Gson.java:457)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at com.google.gson.Gson.getAdapter(Gson.java:457)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at com.google.gson.Gson.getAdapter(Gson.java:457)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at com.google.gson.Gson.getAdapter(Gson.java:457)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at com.google.gson.Gson.getAdapter(Gson.java:457)
at com.google.gson.internal.bind.ArrayTypeAdapter$1.create(ArrayTypeAdapter.java:48)
at com.google.gson.Gson.getAdapter(Gson.java:457)
at com.google.gson.Gson.fromJson(Gson.java:921)
at com.google.gson.Gson.fromJson(Gson.java:860)
at dev.bako.tilegame.utils.JSONImporter.ItemJSONReader(JSONImporter.java:13)
at dev.bako.tilegame.Game.init(Game.java:63)
at dev.bako.tilegame.Game.run(Game.java:101)
at java.lang.Thread.run(Thread.java:745)
.
The JSONImporter class is the following:
public class JSONImporter {
public static void ItemJSONReader() throws Exception {
Item[] items = new Gson().fromJson(new FileReader("res/JSON/Item.json"), Item[].class);//This is where I get the error
System.out.println("Loaded file!" + items);
}
}
The JSON file I'm trying to import:
{
"Wood": {
"id": 0
},
"Rock": {
"id": 2
}
}
Itemand why are you deserialisingItem[]when your data seems to beMap<String, Item>? Then what does Swing and JFrame have to do with all this, with the process of data deserialisation? And please do not tell that you do not have a DTO to deserialise into which is independent of the view layer. - Oleg Sklyar