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 Redisseriali zer<object> {//derek:note that Serializingconverter be a class in Spring Core private CONVERTER<OBJEC
T, 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 obj
ECT to a byte array. * @author Gary Russell * @author Mark Fisher * @since 3.0.5/public class Serializingconverter implements Converte
R<object, byte[]> {private final serializer<object> serializer;
/** * Create A default serializingconverter that uses standard Java serialization. * * Public Serializingconverter () {//derek:consider Defaultserializer this.serializ
ER = new Defaultserializer (); /** * Create A serializingconverter that delegates to the provided {@link serializer} */PUBlic Serializingconverter (serializer<object> serializer) {Assert.notnull (serializer, "Serializer must not to be nul
L ");
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 ' for input (object), ByteStream are for output (byte stream)
this.serializer.serialize (source, ByteStream);
return Bytestream.tobytearray (); The catch (Throwable ex) {throw new Serializationfailedexception ("Failed to serialize object using" + This.seria
Lizer.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 a object to a 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 a 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 Seriali
Zable 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 is applied in networking serialization?