JSON schema update, probably

This commit is contained in:
AppleDash 2019-11-04 05:01:09 -05:00
parent dfaca9285e
commit 9fdb09fc79
1 changed files with 29 additions and 9 deletions

View File

@ -1,5 +1,6 @@
package org.appledash.saneeconomy.economy.backend.type; package org.appledash.saneeconomy.economy.backend.type;
import com.avaje.ebeaninternal.server.cluster.DataHolder;
import com.google.common.reflect.TypeToken; import com.google.common.reflect.TypeToken;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
@ -38,18 +39,25 @@ public class EconomyStorageBackendJSON extends EconomyStorageBackendCaching {
try { try {
// try to load the old format and convert it // try to load the old format and convert it
balances = new ConcurrentHashMap<>((Map)gson.fromJson(new FileReader(file), new TypeToken<Map<String, Double>>(){}.getType())); // if that fails, load the new format
DataHolderOld dataHolder = gson.fromJson(new FileReader(file), DataHolderOld.class);
this.balances = new ConcurrentHashMap<>();
this.uuidToName = new ConcurrentHashMap<>(dataHolder.uuidToName);
dataHolder.balances.forEach((s, bal) -> {
this.balances.put(s, new BigDecimal(bal));
});
this.saveDatabase(); this.saveDatabase();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new RuntimeException("Failed to load database!", e); throw new RuntimeException("Failed to load database!", e);
} catch (Exception e) { } catch (Exception e) {
// if that fails, load the new format
try { try {
DataHolder dataHolder = gson.fromJson(new FileReader(file), DataHolder.class); DataHolder dataHolder = gson.fromJson(new FileReader(file), DataHolder.class);
//FIXME this.balances = new ConcurrentHashMap<>(dataHolder.balances); this.balances = new ConcurrentHashMap<>(dataHolder.balances);
this.uuidToName = new ConcurrentHashMap<>(dataHolder.uuidToName); this.uuidToName = new ConcurrentHashMap<>(dataHolder.uuidToName);
} catch (FileNotFoundException e1) { } catch (FileNotFoundException ex) {
throw new RuntimeException("Failed to load database!", e1); throw new RuntimeException("Failed to load database!", e);
} }
} }
} }
@ -61,20 +69,32 @@ public class EconomyStorageBackendJSON extends EconomyStorageBackendCaching {
private synchronized void saveDatabase() { private synchronized void saveDatabase() {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, false))) { try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, false))) {
//FIXME DataHolder dataHolder = new DataHolder(this.balances, this.uuidToName); DataHolder dataHolder = new DataHolder(this.balances, this.uuidToName);
//FIXMEbufferedWriter.write(gson.toJson(dataHolder)); bufferedWriter.write(gson.toJson(dataHolder));
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Failed to save database", e); throw new RuntimeException("Failed to save database", e);
} }
} }
private static class DataHolder { private static class DataHolderOld {
@SerializedName("balances") @SerializedName("balances")
private Map<String, Double> balances; private Map<String, Double> balances;
@SerializedName("uuidToName") @SerializedName("uuidToName")
private Map<String, String> uuidToName; private Map<String, String> uuidToName;
public DataHolder(Map<String, Double> balances, Map<String, String> uuidToName) { public DataHolderOld(Map<String, Double> balances, Map<String, String> uuidToName) {
this.balances = balances;
this.uuidToName = uuidToName;
}
}
private static class DataHolder {
@SerializedName("balances")
private Map<String, BigDecimal> balances;
@SerializedName("uuidToName")
private Map<String, String> uuidToName;
public DataHolder(Map<String, BigDecimal> balances, Map<String, String> uuidToName) {
this.balances = balances; this.balances = balances;
this.uuidToName = uuidToName; this.uuidToName = uuidToName;
} }