Skip to content

Serialization

Florian Fülling edited this page Jun 3, 2021 · 1 revision

EldoUtilities provides an easy way to serialize and deserialize objects when working with the ConfigurationSerializable interface.

General Usage

Use the map builder retrieved with SerializationUtil#newBuilder() to convert your class to a map.
Use the type resolving map rretrieved with SerializationUtil#mapOf(Map<String, Object>) to map the object map on your object again.

If you implemented it properly it should look like this (Please read the comments.):

    // We implement the ConfigurationSerializable interface
    @SerializableAs("SerializableClass")
    public class SerializableClass implements ConfigurationSerializable {
        // We add a default value to all values
        private int someInt = 1;
        private String someString = "Hewo";
        private NestedClass nestedClass = new NestedClass();

        public SerializableClass() {
        }

        // Constructor to map the map on your object
        // This constructor is called by bukkit to deserialize your object.
        public SerializableClass(Map<String, Object> objectMap) {
            TypeResolvingMap map = SerializationUtil.mapOf(objectMap);
            // We use the default value from above as a default value.
            // This way we dont have to care about missing keys.
            someInt = map.getValueOrDefault("someInt", someInt);
            someString = map.getValueOrDefault("someString", someString);
            nestedClass = map.getValueOrDefault("nestedClass", nestedClass);
        }

        // Method to serialize your object on a map.
        // Please not that we just throw our nested class in here
        @Override
        public @NotNull Map<String, Object> serialize() {
            return SerializationUtil.newBuilder()
                    .add("someInt", someInt)
                    .add("someString", someString)
                    .add("nestedClass", nestedClass)
                    .build();
        }
    }

    @SerializableAs("NestedClass")
    public class NestedClass implements ConfigurationSerializable {
        private String someString = "Amazing";
        private String someOtherString = "Much default";

        public NestedClass(Map<String, Object> objectMap) {
            TypeResolvingMap map = SerializationUtil.mapOf(objectMap);
            someString = map.getValueOrDefault("someInt", someString);
            someOtherString = map.getValueOrDefault("someInt", someOtherString);
        }

        public NestedClass() {
        }

        @Override
        public @NotNull Map<String, Object> serialize() {
            return SerializationUtil.newBuilder()
                    .add("someString", someString)
                    .add("someOtherString", someOtherString)
                    .build();
        }
    }

Simple implementation

We also provide a simpler method which is restricted in usage. Therefore it can only serialize objects without a map or enum values.
There are maybe some other limitations which we are not aware of currently.

However the simple way allows you to make serialization even simpler.

    // We implement the ConfigurationSerializable interface
    @SerializableAs("SerializableClass")
    public class SerializableClass implements ConfigurationSerializable {
        // We add a default value to all values
        private int someInt = 1;
        private String someString = "Hewo";
        private NestedClass nestedClass = new NestedClass();
        private transient int ignoreMe = 0;

        public SerializableClass() {
        }

        // Constructor to map the map on your object
        // This constructor is called by bukkit to deserialize your object.
        public SerializableClass(Map<String, Object> objectMap) {
            // We just call this method. It will map the map values on our object
            SerializationUtil.mapOnObject(objectMap, this);
        }

        // Method to serialize your object on a map.
        // Please not that we just throw our nested class in here
        @Override
        public @NotNull Map<String, Object> serialize() {
            // We just map our object to a map
            // the transient int from above will not be serialized.
            return SerializationUtil.objectToMap(this);
        }
    }

    @SerializableAs("NestedClass")
    public class NestedClass implements ConfigurationSerializable {
        private String someString = "Amazing";
        private String someOtherString = "Much default";

        public NestedClass(Map<String, Object> objectMap) {
            SerializationUtil.mapOnObject(objectMap, this);
        }

        public NestedClass() {
        }

        @Override
        public @NotNull Map<String, Object> serialize() {
            return SerializationUtil.objectToMap(this);
        }
    }
Clone this wiki locally