diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/serializer/BasicNumberToStringSerializer.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/serializer/BasicNumberToStringSerializer.java deleted file mode 100644 index e7a493879..000000000 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/serializer/BasicNumberToStringSerializer.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2011 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.data.keyvalue.redis.serializer; - -import java.lang.reflect.Constructor; -import java.nio.charset.Charset; - -import org.springframework.beans.BeanUtils; -import org.springframework.util.Assert; - -/** - * Simple toString() serializer for the core (lang) numberic JDK types. - * - * @see String#valueOf(Object) - * @see Long#valueOf(String) - * @author Costin Leau - */ -public class BasicNumberToStringSerializer implements RedisSerializer { - - private final Charset charset; - private final Constructor ctor; - - public BasicNumberToStringSerializer(Class type) { - this(type, Charset.forName("UTF8")); - } - - public BasicNumberToStringSerializer(Class type, Charset charset) { - Assert.notNull(type); - this.charset = charset; - - if (!(Byte.class.isAssignableFrom(type) || Short.class.isAssignableFrom(type) - || Long.class.isAssignableFrom(type) || Integer.class.isAssignableFrom(type) - || Float.class.isAssignableFrom(type) || Double.class.isAssignableFrom(type))) { - throw new IllegalArgumentException("Type " + type + " not supported"); - } - - try { - ctor = type.getConstructor(String.class); - } catch (Exception ex) { - throw new IllegalArgumentException("Cannot find suitable constructor for " + type); - } - } - - @Override - public T deserialize(byte[] bytes) { - String string = new String(bytes, charset); - return BeanUtils.instantiateClass(ctor, string); - } - - @Override - public byte[] serialize(T object) { - String string = String.valueOf(object); - return string.getBytes(charset); - } -} \ No newline at end of file diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/serializer/GenericToStringSerializer.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/serializer/GenericToStringSerializer.java index 8daafa1b9..2da22f808 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/serializer/GenericToStringSerializer.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/serializer/GenericToStringSerializer.java @@ -23,6 +23,7 @@ import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.core.convert.ConversionService; +import org.springframework.core.convert.support.ConversionServiceFactory; import org.springframework.util.Assert; /** @@ -40,7 +41,7 @@ import org.springframework.util.Assert; public class GenericToStringSerializer implements RedisSerializer, BeanFactoryAware { private final Charset charset; - private Converter converter; + private Converter converter = new Converter(ConversionServiceFactory.createDefaultConversionService()); private Class type; public GenericToStringSerializer(Class type) { diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicInteger.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicInteger.java index cae600c76..16830923e 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicInteger.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicInteger.java @@ -24,7 +24,7 @@ import org.springframework.data.keyvalue.redis.core.RedisOperations; import org.springframework.data.keyvalue.redis.core.RedisTemplate; import org.springframework.data.keyvalue.redis.core.SessionCallback; import org.springframework.data.keyvalue.redis.core.ValueOperations; -import org.springframework.data.keyvalue.redis.serializer.BasicNumberToStringSerializer; +import org.springframework.data.keyvalue.redis.serializer.GenericToStringSerializer; import org.springframework.data.keyvalue.redis.serializer.StringRedisSerializer; /** @@ -50,7 +50,7 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound public RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory) { RedisTemplate redisTemplate = new RedisTemplate(factory); redisTemplate.setKeySerializer(new StringRedisSerializer()); - redisTemplate.setValueSerializer(new BasicNumberToStringSerializer(Integer.class)); + redisTemplate.setValueSerializer(new GenericToStringSerializer(Integer.class)); redisTemplate.setExposeConnection(true); this.key = redisCounter; this.generalOps = redisTemplate; diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicLong.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicLong.java index 001ee19ba..10275074a 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicLong.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/atomic/RedisAtomicLong.java @@ -24,7 +24,7 @@ import org.springframework.data.keyvalue.redis.core.RedisOperations; import org.springframework.data.keyvalue.redis.core.RedisTemplate; import org.springframework.data.keyvalue.redis.core.SessionCallback; import org.springframework.data.keyvalue.redis.core.ValueOperations; -import org.springframework.data.keyvalue.redis.serializer.BasicNumberToStringSerializer; +import org.springframework.data.keyvalue.redis.serializer.GenericToStringSerializer; import org.springframework.data.keyvalue.redis.serializer.StringRedisSerializer; /** @@ -50,7 +50,7 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBound redisTemplate = new RedisTemplate(factory); redisTemplate.setKeySerializer(new StringRedisSerializer()); - redisTemplate.setValueSerializer(new BasicNumberToStringSerializer(Long.class)); + redisTemplate.setValueSerializer(new GenericToStringSerializer(Long.class)); redisTemplate.setExposeConnection(true); this.key = redisCounter; this.generalOps = redisTemplate;