Reputation: 3
I am using GSON today for the first time. Right now I am trying to deserialise and serialise a class with values. Deserialise works perfectly but upon loading it just outputs all fields as null. I really want to know what I am doing wrong, or why it is not working.
Class to be filled:
public class PlayerData {
private static Map<UUID, PlayerData> playerDataMap = new HashMap<UUID, PlayerData>();
public UUID uuid;
public Integer kills;
public Integer level;
public Integer xp;
public SkillType skillType;
public PlayerData() {
playerDataMap.put(getUUID(), this);
String logString = "[DEBUG] " + uuid + " " + skillType + " " + level;
ZombieSurvival.getInstance().getLogger().info(logString);
}
}
Saving Code:
private synchronized void savePlayerData(){
try {
Map<UUID, PlayerData> map = PlayerData.getPlayerDataMap();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String seperator = File.separator;
String path = getDataFolder().getAbsolutePath() + seperator + "PlayerData" + seperator;
File folder = new File(path);
folder.mkdirs();
for(UUID uuid : map.keySet()){
PlayerData pd = map.get(uuid);
String json = gson.toJson(pd);
File myFile = new File(folder,uuid.toString() + ".json");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
myOutWriter.append(json);
myOutWriter.close();
fOut.close();
}
} catch (Exception e){
e.printStackTrace();
}
}
INFO: JSON Content was created by this method!
Loading Code:
private void loadPlayerData() {
try {
//Setup GSON Loading
String seperator = File.separator;
File invasionMobFolder = new File(getDataFolder() + seperator + "PlayerData" + seperator);
invasionMobFolder.mkdirs();
File[] listOfFiles = invasionMobFolder.listFiles();
for (File file : listOfFiles) {
if (file.getName().endsWith(".json")) {
FileInputStream fIn = new FileInputStream(file);
BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = ""; //Holds the text
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow;
}
myReader.close();
Gson gson = new Gson();
gson.fromJson(aBuffer, PlayerData.class);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
JSON Content:
{
"uuid": "ba79c71f-aa2c-4f8b-a7e5-d6bfea7a4dcc",
"kills": 0,
"level": 0,
"xp": 0,
"skillType": "KNIGHT"
}
Upvotes: 0
Views: 1172
Reputation: 36
As Steve Bishop said or tried to say, the return value from the gson.fromJson
method call in loadPlayerData
is not used. If you want to save the PlayerData objects to a static variable you can't do that in the constructor of the PlayerData class for each one because GSON will most likely call the constructor first and then fill the variables with the matching fields from your json string through reflection resulting in your null-filled fields.
What you have to do is to get the deserialized JSON from gson.fromJson
and put that into your static PlayerData.playerDataMap
field which obviously needs at least a public setter.
Upvotes: 1
Reputation: 11
You did not save the results.
private List<PlayerData> loadPlayerData(){
List<PlayerData> players = new ArrayList<>();
try {
//Setup GSON Loading
String seperator = File.separator;
File invasionMobFolder = new File(getDataFolder() + seperator + "PlayerData" + seperator);
invasionMobFolder.mkdirs();
File[] listOfFiles = invasionMobFolder.listFiles();
for(File file : listOfFiles){
if(file.getName().endsWith(".json")){
FileInputStream fIn = new FileInputStream(file);
BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = ""; //Holds the text
while ((aDataRow = myReader.readLine()) != null)
{
aBuffer += aDataRow ;
}
myReader.close();
Gson gson = new Gson();
players.add(gson.fromJson(aBuffer, PlayerData.class));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return players;
}
Upvotes: 0
Reputation: 403
Your fields are private,GSON maps only public fields by default.
Upvotes: 0