1
Hi, I’m doing some college work and I’m having trouble faking a persistence. The idea is this, I have an Observable/Singleton class that does both the control of updating data from some lists and does the storage and import of this data from a json. Below follows 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 populates the other fields that are not of the type ArrayList<T>
but in these (which are the most important) I can’t pull... Below is the json file I’m 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 ?
This Json is just like that?
– novic