Serialization
The serializer will serialize every field of the current instance as well as every superclass. As an example class we will use this:
public class Example {
// This will not be saved as it is 'static'
private static int i = 0;
// This will not be saved as it is 'transient'
private transient long l = 0;
private int integer = 0;
private String string = "Hello World";
}
To give the serializer hints what to serialize and what not to serialize there are some annotations that are useful. Here the annotated example:
import yapion.annotations.serialize.YAPIONSave;
@YAPIONSave
public class Example {
// This will not be saved as it is 'static'
private static int i = 0;
// This will not be saved as it is 'transient'
private transient long l = 0;
@YAPIONSave
private int integer = 0;
@YAPIONSave
private String string = "Hello World";
}
YAPIONSave defines that this should be serialized, If you just want to save everything from this class there is the YAPIONData shortcut. This shortcut imposes some limitations.
import yapion.annotations.object.YAPIONData;
@YAPIONData
public class Example {
// This will not be saved as it is 'static'
private static int i = 0;
// This will not be saved as it is 'transient'
private transient long l = 0;
private int integer = 0;
private String string = "Hello World";
}
The serialization code will look something like this:
import yapion.hierarchy.types.YAPIONObject;
import yapion.serializing.YAPIONSerializer;
public class ExampleSerialization {
public static void main(String[] args) {
YAPIONObject yapionObject = YAPIONSerializer.serialize(new Example());
}
}
The resulting hierarchy of the YAPIONObject will look something like this:
root YAPIONObject
`- '@type' Variable
| `- 'Example' Value.String
`- 'integer' Variable
| `- '0' Value.Int
`- 'string' Variable
`- 'Hello World' Value.String
If you ‘extend’ some class the fields of the superclass will be serialized, too:
import yapion.annotations.object.YAPIONData;
@YAPIONData
public class ExampleTwo extends Example {
private String text = "ExampleTwo";
}
The serialization code then looks the same as in the example above:
import yapion.hierarchy.types.YAPIONObject;
import yapion.serializing.YAPIONSerializer;
public class ExampleSerializationTwo {
public static void main(String[] args) {
YAPIONObject yapionObject = YAPIONSerializer.serialize(new ExampleTwo());
}
}
The resulting hierarchy of the YAPIONObject will look something like this:
root YAPIONObject
`- '@type' Variable
| `- 'ExampleTwo' Value.String
`- 'integer' Variable
| `- '0' Value.Int
`- 'string' Variable
| `- 'Hello World' Value.String
`- 'text' Variable
`- 'ExampleTwo' Value.String
If we expand example two to also print the YAPIONObject in String format you will see:
import yapion.hierarchy.types.YAPIONObject;
import yapion.serializing.YAPIONSerializer;
public class ExampleSerializationTwo {
public static void main(String[] args) {
YAPIONObject yapionObject = YAPIONSerializer.serialize(new ExampleTwo());
System.out.println(yapionObject);
}
}
Output:
{@type(ExampleTwo)text(Hello World)integer(0)string(Hello World)}