Skip to content

Object Serialization

Ryandw11 edited this page May 8, 2020 · 1 revision

In ODS objects can be serialized and stored.

Serializing Objects

To serialize objects you need to mark the fields you want to store with the Serializable annotations.

public class Car {

    @Serializable
    private String type;

    @Serializable
    private int gas;

    @Serializable
    private List<String> cords;

    // Must have an empty constructor.
    public Car(){}
}

Then just simply use the utility method from the ODS class called serialize.

Car car = new Car();
// populate car fields.

ObjectTag = ODS.serialize("Car", car);

That method returns an ObjectTag that you can then put into your tag list to save.

Deserialization

To deserialize an object you just used the Utility method ODS.deserialize(tag, class)

Car c = ODS.deserialize(ods.get("Car"), Car.class);

Child Objects with Serialization

The serializer can serialize child objects as long as the child object can be serialized.
There is no difference serializing and deserializing these objects.

public class Car {

    @Serializable
    private String type;

    @Serializable
    private int gas;

    @Serializable
    private List<String> cords;

    @Serializable
    private Owner owner;

    // Must have an empty constructor.
    public Car(){}
}

public class Owner {
    @Serializable
    private String firstName;

    @Serializable
    private String lastName;

    @Serializable
    private int age;

    public Owner(){}
}

Clone this wiki locally