Source Taste: Spring Data: Redis: JdkSerializationRedisSerializer

來源:互聯網
上載者:User
PART 1:
package org.springframework.data.redis.serializer;import org.springframework.core.convert.converter.Converter;import org.springframework.core.serializer.support.DeserializingConverter;import org.springframework.core.serializer.support.SerializingConverter;/** * Java Serialization Redis serializer. * Delegates to the default (Java based) serializer in Spring 3. *  * @author Mark Pollack * @author Costin Leau */public class JdkSerializationRedisSerializer implements RedisSerializer<Object> {        //Derek: Note that SerializingConverter is a class in Spring Coreprivate Converter<Object, byte[]> serializer = new SerializingConverter();private Converter<byte[], Object> deserializer = new DeserializingConverter();public Object deserialize(byte[] bytes) {if (SerializationUtils.isEmpty(bytes)) {return null;}try {return deserializer.convert(bytes);} catch (Exception ex) {throw new SerializationException("Cannot deserialize", ex);}}public byte[] serialize(Object object) {if (object == null) {return SerializationUtils.EMPTY_ARRAY;}try {return serializer.convert(object);} catch (Exception ex) {throw new SerializationException("Cannot serialize", ex);}}}

PART 2:

package org.springframework.core.serializer.support;import java.io.ByteArrayOutputStream;import org.springframework.core.convert.converter.Converter;import org.springframework.core.serializer.DefaultSerializer;import org.springframework.core.serializer.Serializer;import org.springframework.util.Assert;/** * A {@link Converter} that delegates to a {@link org.springframework.core.serializer.Serializer} * to convert an object to a byte array. * * @author Gary Russell * @author Mark Fisher * @since 3.0.5 */public class SerializingConverter implements Converter<Object, byte[]> {private final Serializer<Object> serializer;/** * Create a default SerializingConverter that uses standard Java serialization. */public SerializingConverter() {                //Derek: consider DefaultSerializer                this.serializer = new DefaultSerializer();}/** * Create a SerializingConverter that delegates to the provided {@link Serializer} */public SerializingConverter(Serializer<Object> serializer) {Assert.notNull(serializer, "Serializer must not be null");this.serializer = serializer;}/** * Serializes the source object and returns the byte array result. */public byte[] convert(Object source) {ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128);try  {                        //Derek: note that source is for input(object), byteStream is for output(byte stream)                         this.serializer.serialize(source, byteStream);return byteStream.toByteArray();}catch (Throwable ex) {throw new SerializationFailedException("Failed to serialize object using " +this.serializer.getClass().getSimpleName(), ex);}}}
PART 3:
package org.springframework.core.serializer;import java.io.IOException;import java.io.ObjectOutputStream;import java.io.OutputStream;import java.io.Serializable;/** * Serializer that writes an object to an output stream using Java Serialization. *  * @author Gary Russell * @author Mark Fisher * @since 3.0.5 */public class DefaultSerializer implements Serializer<Object> {/** * Writes the source object to an output stream using Java Serialization. * The source object must implement {@link Serializable}. */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();}}

CONTEMPLATE:

(1) Go further, how to deal with the 'web of objects'? deep copying?

(2) Any limit for this Serializer?

(3) Can be applied in Networking Serialization?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.