-
Notifications
You must be signed in to change notification settings - Fork 0
3.1 Serialization
OpAPI's serialization system is mainly based on bukkit's system. It is refractored to work with custom map objects and to have easier to build serialization and deserialization analyzer.
Both objects are used for registering a serializer class, however one is a interface and the second one not.
Serializable is an interface that works standalone and needs additional register at startup (mainly something like this):
Serialization.registerClass(getClass());
public static void registerClass(@NotNull Class<? extends Serializable> clazz);
Serialize is an abstract class that implements Serializable and adds additional constructor which will automatically register the class for you. No work if you choose this!!
In order to use built-in serialization you have two options of implementation:
- implement Serializable interface,
- implement Serialize abstract class.
It is recomended to use Serialize over Serializable as much as you can.
In order to use serialization you just need to call method .serialize() on your object.
For deserialization you can call one of these methods:
public Serializable deserialize(OpMap<String, ?> args);
public static Serializable deserializeObject(@NotNull OpMap<String, ?> args, @NotNull Class<? extends Serializable> clazz);
public static Serializable deserializeObject(@NotNull OpMap<String, ?> args)
Test.class
import me.opkarol.opc.api.configuration.v2.Serialize;
import me.opkarol.opc.api.map.OpMap;
import me.opkarol.opc.api.map.OpMapBuilder;
public class Test extends Serialize {
private int number;
public Test(OpMapBuilder<String, Object> objects) {
super(objects);
if (objects.containsKey("number")) {
number = (int) objects.unsafeGet("number");
}
}
public Test(int number) {
super(null); //used only to register class (not use map, that's why you can have null object provided here)
this.number = number;
}
@Override
public OpMap<String, Object> serialize() {
return getMapBuilder()
.setValue("number", number);
}
public void setNumber(int number) {
this.number = number;
}
}
Use method:
Test test = new Test(1);
var map = test.serialize();
OpAPI.logInfo(VariableUtil.stringValueOfMap(map));
for (int i : VariableUtil.range(1, 5)) {
Test test1 = (Test) Serialization.deserializeObject(map, Test.class);
assert test1 != null;
if (i % 2 == 0) {
test1.setNumber(2);
} else {
test1.setNumber(0);
}
map = test1.serialize();
OpAPI.logInfo(VariableUtil.stringValueOfMap(map));
}
Output:
[16:57:42 INFO]: [opc] ==-class me.opkarol.opc.api.configuration.test.Test, number-1,
[16:57:42 INFO]: [opc] ==-class me.opkarol.opc.api.configuration.test.Test, number-0,
[16:57:42 INFO]: [opc] ==-class me.opkarol.opc.api.configuration.test.Test, number-2,
[16:57:42 INFO]: [opc] ==-class me.opkarol.opc.api.configuration.test.Test, number-0,
[16:57:42 INFO]: [opc] ==-class me.opkarol.opc.api.configuration.test.Test, number-2,
[16:57:42 INFO]: [opc] ==-class me.opkarol.opc.api.configuration.test.Test, number-0,
If you are stuck with something, have any ideas, want to improve anything or tell me anything, don't hesitate to DM me on discord: KarolGajda#5694.