Hello, I'm doing a college job and I'm having trouble simulating a persistence. The idea is this, I have an Observable / Singleton class that does both the data update control of some lists and does the storage and import of that data from a json. Here is my code:
/**
* Singleton class. Representa uma estrutura simples de base de dados mantendo as
* informações em um arquivo JSON no root da aplicação.
*/
public class Db extends Observable {
// construtores
private Db() {
fileName = "db.json";
assinantes = new ArrayList<>();
mensagens = new ArrayList<>();
regras = new ArrayList<>();
conquistas = new ArrayList<>();
}
private Db(int i) throws IOException {
fileName = "db.json";
try {
get();
} catch (IOException e) {
assinantes = new ArrayList<>();
mensagens = new ArrayList<>();
regras = new ArrayList<>();
conquistas = new ArrayList<>();
set();
}
}
// implementação singleton
private static Db instance;
public static Db getInstance() throws IOException {
if (instance == null) instance = new Db(0);
return instance;
}
// implementação persistence
protected String fileName;
// getters & setters
private ArrayList<AssinanteFree> assinantes;
public ArrayList<AssinanteFree> getAssinantes() {
return instance.assinantes;
}
public void setAssinantes(ArrayList<AssinanteFree> v) {
assinantes = v;
emit();
}
private ArrayList<Mensagem> mensagens;
public ArrayList<Mensagem> getMensagens() {
return instance.mensagens;
}
public void setMensagens(ArrayList<Mensagem> v) {
mensagens = v;
emit();
}
private ArrayList<Regra> regras;
public ArrayList<Regra> getRegras() {
return instance.regras;
}
private ArrayList<Conquista> conquistas;
public ArrayList<Conquista> getConquistas() {
return instance.conquistas;
}
// methods
private void emit() {
setChanged();
notifyObservers();
}
private void get() throws IOException {
try (FileReader reader = new FileReader(fileName)) {
Gson gson = new Gson();
instance = gson.fromJson(reader, Db.class);
}
}
private void set() throws IOException {
try (Writer writer = new FileWriter(fileName)) {
Gson gson = new GsonBuilder().create();
gson.toJson(this, writer);
}
}
}
My problem is this. I can pull a json file for this class, so much so that it even populates the other fields that are not of type ArrayList<T>
but in these (which are the ones that matter most) I can not pull ... Below is the json file that I am using:
{
"fileName": "db.json",
"assinantes": [
{
"id": 1,
"nome": "Leandro",
"tipo": "Vip",
"pontuacao": 0,
"dataCriado": "2018-06-27 05:00:00"
},
{
"id": 1,
"nome": "André",
"tipo": "Premium",
"pontuacao": 0
},
{
"id": 1,
"nome": "Maria",
"tipo": "Free"
}
],
"mensagens": [],
"regras": [
{ "tipo": "Free", "limiteMsg": 50, "addPontos": 0 },
{ "tipo": "Premium", "limiteMsg": 75, "addPontos": 1 },
{ "tipo": "Vip", "limiteMsg": 100, "addPontos": 1.4 }
],
"conquistas": [
{ "descricao": "Novato", "minimo": 0 },
{ "descricao": "Iniciante", "minimo": 1 },
{ "descricao": "Experiente", "minimo": 4 },
{ "descricao": "Sênior", "minimo": 10 },
{ "descricao": "Legendario", "minimo": 100 }
],
"changed": false,
"obs": []
}
How do I import ArrayList's?