Source taste:spring Data:Redis:JdkSerializationRedisSerializer

Source: Internet
Author: User
Tags assert object object redis serialization
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&LT;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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.