Jdkserializationredisserializer analysis of redisserializer

Time:2020-11-29

Jdkserializationredisserializer analysis of redisserializer

When redis caches POJO, it needs to serialize POJO into byte array for storage. Spring data redis implements the class jdkserializationredisserializer to serialize POJO. The structure of the class diagram is as follows:

1. The jdkserializationredisserializer class implements the interface redisserializer class
Byte [] Serialize (t t) and t deserialize (byte [] bytes) interfaces.

2. By defining serializingconverter and deserializingconverter objects, POJO can be serialized and deserialized.

public JdkSerializationRedisSerializer() {
        this(new SerializingConverter(), new DeserializingConverter());
    }
    

3. For serializing converter, define the defaultserializer object and call the Serialize (object, OutputStream, OutputStream) method to serialize POJO.

    public SerializingConverter() {
            this.serializer = new DefaultSerializer();
        }

    @Override
    public byte[] convert(Object source) {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
        try  {
            this.serializer.serialize(source, byteStream);
            return byteStream.toByteArray();
        }
        catch (Throwable ex) {
            throw new SerializationFailedException("Failed to serialize object using " +
                    this.serializer.getClass().getSimpleName(), ex);
        }
    }
    
    public class DefaultSerializer implements Serializer<Object> {
    @Override
    public void serialize(Object object, OutputStream outputStream) throws IOException {
        if (!(object instanceof Serializable)) {
            throw new IllegalArgumentException(getClass().getSimpleName() + " requires a Serializable payload " +
                    "but received an object of type [" + object.getClass().getName() + "]");
        }
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        objectOutputStream.writeObject(object);
        objectOutputStream.flush();
    }

}

4. For deserializing converter, define the defaultdeserializer object and call deserialize (InputStream InputStream) method to deserialize POJO.

public DeserializingConverter() {
        this.deserializer = new DefaultDeserializer();
    }

@Override
    public Object convert(byte[] source) {
        ByteArrayInputStream byteStream = new ByteArrayInputStream(source);
        try {
            return this.deserializer.deserialize(byteStream);
        }
        catch (Throwable ex) {
            throw new SerializationFailedException("Failed to deserialize payload. " +
                    "Is the byte array a result of corresponding serialization for " +
                    this.deserializer.getClass().getSimpleName() + "?", ex);
        }
    }

public class DefaultDeserializer implements Deserializer<Object> {

    private final ClassLoader classLoader;

    @Override
    @SuppressWarnings("resource")
    public Object deserialize(InputStream inputStream) throws IOException {
        ObjectInputStream objectInputStream = new ConfigurableObjectInputStream(inputStream, this.classLoader);
        try {
            return objectInputStream.readObject();
        }
        catch (ClassNotFoundException ex) {
            throw new NestedIOException("Failed to deserialize object type", ex);
        }
    }

}

5. The bottom layer still realizes the serialization and deserialization of POJO by calling the IO operations of JDK, objectinputstream and objectoutputstream.

Recommended Today

Regular expression sharing for checking primes

This regular expression is shown as follows: Regular expressions for checking prime numbers or not To use this positive regular expression, you need to convert the natural number into multiple 1 strings. For example, 2 should be written as “11”, 3 should be written as “111”, 17 should be written as “11111111111”. This kind of […]