Handling Odin Elements
In addition to objects and primitives that can be serialized to store data, Odin contains a model to store any other file usage ( properties, configuration files, i18n ).
This model has 3 types of elements: Node, Array and Object.
In indented format, comments and blank lines can be added to all these elements.
Odin Node
OdinNode is a map where each element is designated by a String. A node has the same format as an object written with the default Odin adapter. This way, an object written as a node can be read as another Object and same for the other way around.
OdinNode can be written using writeNode(node) method of OdinWriter and can be read using readNode().
Values are indexed and added at the end of the content, so you can get elements by the key or by its index. Node also contains an Iterator<NodeEntry> that allows to get the key and the value of each entry
OdinNode node = new OdinNode();
node.add("value", true);
node.addIfAbsent("foo", 10);
boolean exists = node.contains("foo");
int value = node.getValue("foo");
int index = node.indexOf("foo");
node.remove("foo");
Odin Array
OdinArray is a list of elements that use the same format than Java Array
OdinArray can be write using writeArray(array) method of OdinWriter and can be read using readArray().
OdinArray array = new OdinArray();
array.add(true);
array.addTo(0, 10);
boolean exists = node.contains(10);
int value = array.getValue(0);
array.removeFrom(0);
Odin Object
OdinObject is a container for any Java Object
OdinObject object = new OdinObject(10);
Element Builder
Each element contains a method run that can be used to easily build the model. The method takes a lambda and consumes itself:
OdinNode node = new OdinNode().run((OdinNode node1) -> {
node1.addNode("config").run((OdinNode node2) -> {
node2.add("version", "1.5");
});
node1.addArray("contents").run((OdinArray array1) -> {
array1.add(10);
array1.add(15);
});
});
Element Extra
Each element contains an extra. Node and array also contain a content extra, which is used for comments placed after the last child element.
Extra is a list of String. Empty String define an empty line, else it's a comment
OdinObject object = new OdinObject(10);
object.getExtra().addComment("Set the default value");
object.getExtra().addSpace();
object.getExtra().addComment("The value needs to be between 0 and 10");
# Set the default value
# The value needs to be between 0 and 10
10
Find with Path
In order to use this model in configuration file, a find method is available. This method takes a path and can search the wanted element inside the whole depth of the model.
The path separator is . but can also be [] to be easier to read with arrays.
{
data = {
players = [
{
id = 1
name = "test"
}
]
}
}
int id = node.findValue("data.players.0.id")
String name = node.findValue("data.players[0].id")