Serializers and Deserializers

Apr 2, 2018
Serializers and Deserializers
  • This page will introduce serializers and deserializers, and how to setup them, which are the implementations of the SQLObjectConverter API. There are two types of them, which one is setup inside the target class and another one is setup outside the target class.

    Internal Serializer and Deserializer

    Lets take an example that we want to save and load a Customer:
    Code (Java):
    package me.rcextract;

    public class Customer {
        private String name;
        private String desc;
     
        public Customer(String name, String desc) {
            this.name = name;
            this.desc = desc;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDescription() {
            return desc;
        }

        public void setDescription(String desc) {
            this.desc = desc;
        }

    }
    First, implement DatabaseSerializable:
    Code (Java):
    package me.rcextract;

    import com.rcextract.minecord.sql.DatabaseSerializable;
    import com.rcextract.minecord.utils.ArrayMap;

    public class Customer implements DatabaseSerializable {
        ...
        @Override
        public ArrayMap<String, Object> serialize() {
            // TODO Auto-generated method stub
            return null;
        }

    }
    Then, instantiate a new ArrayMap and put all the properties you want to save into the map along with their names. Then, return it:
    Code (Java):
        ...
        @Override
        public ArrayMap<String, Object> serialize() {
            ArrayMap<String, Object> map = new ArrayMap<String, Object>();
            map.put("name", name);
            map.put("description", desc);
            return map;
        }
        ...
    After that, you will need to tell the SQLObjectConverter about the table name which table is used to store objects of this type by using an annotating com.rcextract.minecord.sql.SerializableAs on the class declaration and pass the name:
    Code (Java):
    package me.rcextract;

    import com.rcextract.minecord.sql.DatabaseSerializable;
    import com.rcextract.minecord.utils.ArrayMap;

    @SerializableAs("customer")
    public class Customer implements DatabaseSerializable {
        ...
    }
    Finally, declare a constructor that accepts an ArrayMap. When deserializing, the SQLObjectConverter will invoke this constructor and pass in the loaded properties. You can then get the properties by name and assign them to the fields:
    Code (Java):
        ...
        public Customer(ArrayMap<String, Object> map) {
            Map<String, Object> internal = map.toMap();
            this.name = internal.get("name");
            this.desc = internal.get("description");
        }
        ...
    External Serializer and Deserializer

    Sometimes the object you want to save is neither implementing DatabaseSerializable, nor a primitve object, nor a String. In this situation, you will need to add at least one pair of external serializer and deserializer. The multiple use of external serializer and deserializer in the same serialization or deserialization is more advanced. We will talk about this later.

    External Serializer and Deserializer only support converting objects to primitive objects or Strings or nulls, but not to properties.
    The SQLObjectConverter will try to see if the input type is implementing DatabaseSerializable. The external converters which involve this type will only be used if false.
    First, obtain a com.rcextract.minecord.sql.TypeConverter by instantiating a subclass with type parameters, where I represents the type before conversion, and O represents the type after conversion. You should usually do it by declaring an anonymous class:
    Code (Java):
    TypeConverter<UUID, String> c = new TypeConverter<UUID, String>() {

        @Override
        public String serialize(UUID uuid) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public UUID deserialize(String string) {
            // TODO Auto-generated method stub
            return null;
        }

    }
    Then, fill in the method contents:
    Code (Java):
    TypeConverter<UUID, String> c = new TypeConverter<UUID, String>() {

        @Override
        public String serialize(UUID uuid) {
            return uuid.toString();
        }

        @Override
        public UUID deserialize(String string) {
            return UUID.fromString(string);
        }

    }
    Finally, suppose converter variable is the desired SQLObjectConverter, add the type converter to it:
    Code (Java):
    converter.getConverters().add(c);
    Now, the SQLObjectConverter will know how to serialize UUID and deserialize UUID from String.
  • Loading...
  • Loading...