+ add back generics to RedisSerializer

+ add basic String serializer (most likely used for keys)
This commit is contained in:
Costin Leau
2010-11-12 16:34:18 +02:00
parent 9795a9254c
commit e8a2cd8b40
3 changed files with 53 additions and 32 deletions

View File

@@ -21,13 +21,9 @@ package org.springframework.datastore.redis.serializer;
* @author Mark Pollack
* @author Costin Leau
*/
public interface RedisSerializer {
public interface RedisSerializer<T> {
byte[] serialize(Object object);
byte[] serialize(T t);
String serializeAsString(Object object);
<T> T deserialize(byte[] bytes);
<T> T deserialize(String bytes);
T deserialize(byte[] bytes);
}

View File

@@ -15,12 +15,9 @@
*/
package org.springframework.datastore.redis.serializer;
import java.io.IOException;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.datastore.redis.UncategorizedRedisException;
/**
@@ -29,7 +26,7 @@ import org.springframework.datastore.redis.UncategorizedRedisException;
* @author Mark Pollack
* @author Costin Leau
*/
public class SimpleRedisSerializer implements RedisSerializer {
public class SimpleRedisSerializer implements RedisSerializer<Object> {
private Converter<Object, byte[]> serializer = new SerializingConverter();
private Converter<byte[], Object> deserializer = new DeserializingConverter();
@@ -39,23 +36,14 @@ public class SimpleRedisSerializer implements RedisSerializer {
@SuppressWarnings("unchecked")
@Override
public <T> T deserialize(byte[] bytes) {
public Object deserialize(byte[] bytes) {
try {
return (T) deserializer.convert(bytes);
return deserializer.convert(bytes);
} catch (Exception ex) {
throw new UncategorizedRedisException("Cannot deserialize", ex);
}
}
@Override
public <T> T deserialize(String bytes) {
try {
return deserialize(decoder.decodeBuffer(bytes));
} catch (IOException ex) {
throw new DataRetrievalFailureException("Unsupported encoding ", ex);
}
}
@Override
public byte[] serialize(Object object) {
try {
@@ -64,14 +52,4 @@ public class SimpleRedisSerializer implements RedisSerializer {
throw new UncategorizedRedisException("Cannot serialize", ex);
}
}
@Override
public String serializeAsString(Object object) {
try {
return encoder.encode(serialize(object));
} catch (Exception ex) {
throw new DataRetrievalFailureException("Unsupported encoding ", ex);
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.datastore.redis.serializer;
import java.nio.charset.Charset;
/**
* Simple String to byte[] (and back) serializer. Relies on the specified charset
* to properly convert the String into bytes and vice-versa.
*
* @author Costin Leau
*/
public class StringRedisSerializer implements RedisSerializer<String> {
private final Charset charset;
public StringRedisSerializer() {
this(Charset.forName("UTF8"));
}
public StringRedisSerializer(Charset charset) {
this.charset = charset;
}
@Override
public String deserialize(byte[] bytes) {
return new String(bytes, charset);
}
@Override
public byte[] serialize(String object) {
return object.toString().getBytes(charset);
}
}