Pretty print the json of the database if it's exported in plain text

This commit is contained in:
Alexander Bakker 2017-12-27 23:23:05 +01:00
parent db54d38c14
commit fdf8da1f32
2 changed files with 7 additions and 2 deletions

View file

@ -14,6 +14,10 @@ public class Database {
private long _counter = 0; private long _counter = 0;
public byte[] serialize() throws Exception { public byte[] serialize() throws Exception {
return serialize(false);
}
public byte[] serialize(boolean pretty) throws Exception {
JSONArray array = new JSONArray(); JSONArray array = new JSONArray();
for (DatabaseEntry e : _entries) { for (DatabaseEntry e : _entries) {
array.put(e.serialize()); array.put(e.serialize());
@ -23,7 +27,8 @@ public class Database {
obj.put("version", VERSION); obj.put("version", VERSION);
obj.put("entries", array); obj.put("entries", array);
return obj.toString().getBytes("UTF-8"); String string = pretty ? obj.toString(4) : obj.toString();
return string.getBytes("UTF-8");
} }
public void deserialize(byte[] data) throws Exception { public void deserialize(byte[] data) throws Exception {

View file

@ -111,7 +111,7 @@ public class DatabaseManager {
public String export(boolean encrypt) throws Exception { public String export(boolean encrypt) throws Exception {
assertState(false, true); assertState(false, true);
byte[] bytes = _db.serialize(); byte[] bytes = _db.serialize(!encrypt);
encrypt = encrypt && getFile().isEncrypted(); encrypt = encrypt && getFile().isEncrypted();
if (encrypt) { if (encrypt) {
CryptResult result = _key.encrypt(bytes); CryptResult result = _key.encrypt(bytes);