mirror of https://github.com/beemdevelopment/Aegis
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.5 KiB
Java
56 lines
1.5 KiB
Java
package me.impy.aegis.db;
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONObject;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
public class Database {
|
|
private static final int VERSION = 1;
|
|
private List<DatabaseEntry> _entries = new ArrayList<>();
|
|
|
|
public byte[] serialize() throws Exception {
|
|
JSONArray array = new JSONArray();
|
|
for (DatabaseEntry e : _entries) {
|
|
array.put(e.serialize());
|
|
}
|
|
|
|
JSONObject obj = new JSONObject();
|
|
obj.put("version", VERSION);
|
|
obj.put("entries", array);
|
|
|
|
return obj.toString().getBytes("UTF-8");
|
|
}
|
|
|
|
public void deserialize(byte[] data) throws Exception {
|
|
JSONObject obj = new JSONObject(new String(data, "UTF-8"));
|
|
|
|
// TODO: support different VERSION deserialization providers
|
|
int ver = obj.getInt("version");
|
|
if (ver != VERSION) {
|
|
throw new Exception("Unsupported version");
|
|
}
|
|
|
|
JSONArray array = obj.getJSONArray("entries");
|
|
for (int i = 0; i < array.length(); i++) {
|
|
DatabaseEntry e = new DatabaseEntry(null);
|
|
e.deserialize(array.getJSONObject(i));
|
|
_entries.add(e);
|
|
}
|
|
}
|
|
|
|
public void addKey(DatabaseEntry entry) {
|
|
_entries.add(entry);
|
|
}
|
|
|
|
public void removeKey(DatabaseEntry entry) {
|
|
_entries.remove(entry);
|
|
}
|
|
|
|
public List<DatabaseEntry> getKeys() {
|
|
return Collections.unmodifiableList(_entries);
|
|
}
|
|
}
|