Added human readable serialization
This commit is contained in:
parent
09ecdb27e4
commit
3fed68a1f2
|
@ -2,25 +2,32 @@ package bdf;
|
||||||
|
|
||||||
import bdf.file.BdfFileManager;
|
import bdf.file.BdfFileManager;
|
||||||
import bdf.types.BdfArray;
|
import bdf.types.BdfArray;
|
||||||
|
import bdf.types.BdfNamedList;
|
||||||
import bdf.types.BdfObject;
|
import bdf.types.BdfObject;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
|
BdfFileManager bdf = new BdfFileManager("db/file.db");
|
||||||
|
|
||||||
BdfFileManager file = new BdfFileManager("test/test2/db.bdf");
|
BdfNamedList bdf_nl = new BdfNamedList();
|
||||||
|
bdf_nl.set("greeting", BdfObject.getNew("Hello, World!"));
|
||||||
|
bdf_nl.set("integer", BdfObject.getNew(21));
|
||||||
|
bdf_nl.set("integer", BdfObject.getNew(52));
|
||||||
|
|
||||||
|
BdfArray bdf_array = new BdfArray();
|
||||||
|
bdf_array.add(BdfObject.getNew(61));
|
||||||
|
bdf_array.add(BdfObject.getNew(42.0d));
|
||||||
|
bdf_array.add(BdfObject.getNew(67F));
|
||||||
|
bdf_array.add(BdfObject.getNew("hello!"));
|
||||||
|
|
||||||
file.setArray(new BdfArray());
|
bdf_nl.set("array", BdfObject.getNew(bdf_array));
|
||||||
file.getArray().add(BdfObject.getNew("Hello"));
|
bdf.setNamedList(bdf_nl);
|
||||||
System.out.println(file.getArray().get(0).getString());
|
|
||||||
file.saveDatabase();
|
|
||||||
|
|
||||||
|
bdf.saveDatabase();
|
||||||
|
|
||||||
BdfFileManager file2 = new BdfFileManager("test/test2/db.bdf");
|
System.out.println(bdf.serializeHumanReadable());
|
||||||
System.out.println(file2.getArray().get(0).getString());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,6 +55,26 @@ public class BdfArray implements IBdfType
|
||||||
return serialized;
|
return serialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String serializeHumanReadable()
|
||||||
|
{
|
||||||
|
String data = "[";
|
||||||
|
|
||||||
|
for(int i=0;i<elements.size();i++)
|
||||||
|
{
|
||||||
|
BdfObject o = elements.get(i);
|
||||||
|
|
||||||
|
data += o.serializeHumanReadable();
|
||||||
|
|
||||||
|
if(elements.size() > i+1)
|
||||||
|
{
|
||||||
|
data += ", ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return data + "]";
|
||||||
|
}
|
||||||
|
|
||||||
public BdfArray add(BdfObject o)
|
public BdfArray add(BdfObject o)
|
||||||
{
|
{
|
||||||
// Add an element
|
// Add an element
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package bdf.types;
|
package bdf.types;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import bdf.data.BdfDatabase;
|
import bdf.data.BdfDatabase;
|
||||||
|
@ -72,6 +73,29 @@ public class BdfNamedList implements IBdfType
|
||||||
return serialized;
|
return serialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String serializeHumanReadable()
|
||||||
|
{
|
||||||
|
String data = "{";
|
||||||
|
|
||||||
|
for(int i=0;i<elements.size();i++)
|
||||||
|
{
|
||||||
|
Element e = elements.get(i);
|
||||||
|
|
||||||
|
data += "\"";
|
||||||
|
data += (new String(e.key, StandardCharsets.UTF_8)).replaceAll("\"", "\\\"");
|
||||||
|
data += "\": ";
|
||||||
|
data += e.object.serializeHumanReadable();
|
||||||
|
|
||||||
|
if(elements.size() > i+1)
|
||||||
|
{
|
||||||
|
data += ", ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return data + "}";
|
||||||
|
}
|
||||||
|
|
||||||
public BdfObject get(String key)
|
public BdfObject get(String key)
|
||||||
{
|
{
|
||||||
// Get the object to send back
|
// Get the object to send back
|
||||||
|
@ -171,6 +195,23 @@ public class BdfNamedList implements IBdfType
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String[] getKeys()
|
||||||
|
{
|
||||||
|
// Get the keys to send back
|
||||||
|
String[] keys = new String[elements.size()];
|
||||||
|
|
||||||
|
// Loop over the elements
|
||||||
|
for(int i=0;i<elements.size();i++)
|
||||||
|
{
|
||||||
|
// Get the element
|
||||||
|
Element e = elements.get(i);
|
||||||
|
keys[i] = new String(e.key, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the list of keys as strings
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
return elements.size();
|
return elements.size();
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,37 @@ public class BdfObject implements IBdfType
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BdfDatabase serialize()
|
||||||
|
{
|
||||||
|
if(type == BdfTypes.STRING) database = new BdfDatabase(this.getString());
|
||||||
|
if(type == BdfTypes.ARRAY) database = ((BdfArray)object).serialize();
|
||||||
|
if(type == BdfTypes.NAMED_LIST) database = ((BdfNamedList)object).serialize();
|
||||||
|
|
||||||
|
return BdfDatabase.add(new BdfDatabase(type), database);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String serializeHumanReadable()
|
||||||
|
{
|
||||||
|
if(type == BdfTypes.BOOLEAN) {
|
||||||
|
if(this.getBoolean()) return "true";
|
||||||
|
else return "false";
|
||||||
|
}
|
||||||
|
|
||||||
|
if(type == BdfTypes.ARRAY) return ((IBdfType)object).serializeHumanReadable();
|
||||||
|
if(type == BdfTypes.NAMED_LIST) return ((IBdfType)object).serializeHumanReadable();
|
||||||
|
if(type == BdfTypes.BYTE) return (Byte.toString(this.getByte())+"B");
|
||||||
|
if(type == BdfTypes.DOUBLE) return (Double.toString(this.getDouble())+"D");
|
||||||
|
if(type == BdfTypes.FLOAT) return (Float.toString(this.getFloat())+"F");
|
||||||
|
if(type == BdfTypes.INTEGER) return (Integer.toString(this.getInteger())+"I");
|
||||||
|
if(type == BdfTypes.LONG) return (Long.toString(this.getLong())+"L");
|
||||||
|
if(type == BdfTypes.SHORT) return (Short.toString(this.getShort())+"S");
|
||||||
|
if(type == BdfTypes.STRING) return "\""+((String)object).replaceAll("\"", "\\\"")+"\"";
|
||||||
|
|
||||||
|
// Return null if the object is undefined
|
||||||
|
return "undefined";
|
||||||
|
}
|
||||||
|
|
||||||
public BdfObject() {
|
public BdfObject() {
|
||||||
database = new BdfDatabase();
|
database = new BdfDatabase();
|
||||||
}
|
}
|
||||||
|
@ -82,16 +113,6 @@ public class BdfObject implements IBdfType
|
||||||
return (new BdfObject()).setNamedList(v);
|
return (new BdfObject()).setNamedList(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public BdfDatabase serialize()
|
|
||||||
{
|
|
||||||
if(type == BdfTypes.STRING) database = new BdfDatabase(this.getString());
|
|
||||||
if(type == BdfTypes.ARRAY) database = ((BdfArray)object).serialize();
|
|
||||||
if(type == BdfTypes.NAMED_LIST) database = ((BdfNamedList)object).serialize();
|
|
||||||
|
|
||||||
return BdfDatabase.add(new BdfDatabase(type), database);
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte getType() {
|
public byte getType() {
|
||||||
return this.type;
|
return this.type;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,4 +5,5 @@ import bdf.data.BdfDatabase;
|
||||||
public interface IBdfType
|
public interface IBdfType
|
||||||
{
|
{
|
||||||
public BdfDatabase serialize();
|
public BdfDatabase serialize();
|
||||||
|
public String serializeHumanReadable();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue