I am using GSON to create a settings file for a client application, but for some reason it sometimes creates a malformed save file, which "simply" has a double bracket ( }} ) at the end.
I am not sure why or how this happens, and it does not happen that often, but it will reset all the settings even though the entire file is still intact (except for an additional } at the end)
Attempts at googling this did not seem to show any similair results, and I am not sure why it would appear I am the only one experiencing this?
The only thing I could come up with is that if you're accessing the tool multiple times, and it somehow writes the settings file for 2 accounts at the same time, it corrupts it? But the file does not get written to that often and should not take too long to write, so I somewhat doubt this would be the case, also wouldn't that just result in the final file being written twice?
Example of a "corrupted" save file (scrapped many settings from the middle section)
{
"rememberMe": true,
"myUsername": "Alex",
"closeWarning": true,
"closeMode": 0,
"escClose": true,
"group1Items": [],
"group2Items": [],
"group3Items": []
}}
How I read the json file:
try (FileReader fileReader = new FileReader(file)) {
Gson builder = new GsonBuilder().setLenient().create();
JsonObject reader = JsonParser.parseReader(fileReader).getAsJsonObject();
if (reader.has("rememberMe"))
Client.instance.rememberMe = reader.get("rememberMe").getAsBoolean();
if (reader.has("myUsername"))
Client.instance.myUsername = reader.get("myUsername").getAsString();
if (reader.has("closeWarning"))
Configuration.closeClientWarning = reader.get("closeWarning").getAsBoolean();
if (reader.has("closeMode")) {
Configuration.closeClientMode = reader.get("closeMode").getAsInt();
CloseWarningSetting.updateCloseOption();
}
if (reader.has("group1Items"))
for (int i : builder.fromJson(reader.get("group1Items").getAsJsonArray(), Integer[].class))
Client.instance.group1Items.add(i);
} catch (Exception e) {
e.printStackTrace();
}
How I write the json file:
Path path = Paths.get(SETTINGS_DIRECTORY + "settings.json");
File file = path.toFile();
file.getParentFile().setWritable(true);
// Attempt to make the save directory if it doesn't exist.
if (!file.getParentFile().exists()) {
try {
file.getParentFile().mkdirs();
} catch (SecurityException e) {
//System.out.println("Unable to create directory for player save data!");
}
}
try (FileWriter writer = new FileWriter(file)) {
Gson builder = new GsonBuilder().setPrettyPrinting().create();
JsonObject object = new JsonObject();
object.addProperty("rememberMe", Client.instance.rememberMe);
if (Client.instance.rememberMe)
object.addProperty("myUsername", Client.instance.myUsername);
object.addProperty("closeWarning", Configuration.closeClientWarning);
object.addProperty("closeMode", Configuration.closeClientMode);
object.add("group1Items", builder.toJsonTree(Client.instance.group1Items));
writer.write(builder.toJson(object));
writer.close();
} catch (Exception e) {
// An error happened while saving.
}