Module omix.api

Interface TypeSerializationHandlerInterface

All Known Implementing Classes:
MathTypeHandler

public interface TypeSerializationHandlerInterface
This interface allows to implement serialization of arbitrary data types.
The omix file I/O procedure is able to write and read a comprehensive but limited set of class types like String, Integer, Double, List etc. In order to extend this set with specific class types available in plugin bundles or libraries this interface can be implemented.

Following example code serializes the class GradientPaint:

public class MyTypeSerializationHandler implements TypeSerializationHandlerInterface {

    public boolean supports(Class<?> type) {
        if(type==GradientPaint.class) {
            return true;
        }
        return false;
    }

    public AbstractSerialization serialize(Object object) {
        if(object instanceof GradientPaint) {
            GradientPaint paint = (GradientPaint)object;
            float x1 = paint.getPoint1().getX();
            float y1 = paint.getPoint1().getY();
            int color1 = paint.getColor1().getRGB();
            float x2 = paint.getPoint2().getX();
            float y2 = paint.getPoint2().getY();
            int color2 = paint.getColor2().getRGB();
            boolean cyclic = paint.isCyclic();
            
            // creating a content array from all components...
            return new ComponentArraySerialization(x1, y1, color1, x2, y2, color2, cyclic);
        }
        return null;
    }

    public Object deserialize(Class<?> type, AbstractSerialization serialization, VersionNumber version) throws Exception {
        if(type==GradientPaint.class) {
            if(serialization instanceof ComponentArraySerialization) {
                Object[] components = ((ComponentArraySerialization)serialization).components;
                if(components.length==7
                        && components[0] instanceof Float
                        && components[1] instanceof Float
                        && components[2] instanceof Integer
                        && components[3] instanceof Float
                        && components[4] instanceof Float
                        && components[5] instanceof Integer
                        && components[6] instanceof Boolean) {
                    float x1 = (Float)components[0];
                    float y1 = (Float)components[1];
                    int color1 = (Integer)components[2];
                    float x2 = (Float)components[3];
                    float y2 = (Float)components[4];
                    int color2 = (Integer)components[5];
                    boolean cyclic = (Boolean)components[6];
                    return new GradientPaint(x1, y1, color1, x2, y2, color2, cyclic);
                } else {
                    throw new Exception("Cannot deserialize GradientPaint.");
                }
            } else {
                throw new Exception("Cannot deserialize GradientPaint.");
            }
        }
        return null;
    }
}

Since:
1.3.11
Author:
Dr. Peter Droste, Omix Visualization