From 37bd46561aae5f62840f24c908101ad08f5d28f5 Mon Sep 17 00:00:00 2001 From: Nikita Date: Wed, 12 Mar 2014 20:23:17 +0400 Subject: [PATCH] Long serialization fixed --- .../org/redisson/codec/JsonJacksonCodec.java | 28 ++++++++++++++- .../java/org/redisson/RedissonMapTest.java | 34 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/redisson/codec/JsonJacksonCodec.java b/src/main/java/org/redisson/codec/JsonJacksonCodec.java index 0a7ba1061..edcd06085 100644 --- a/src/main/java/org/redisson/codec/JsonJacksonCodec.java +++ b/src/main/java/org/redisson/codec/JsonJacksonCodec.java @@ -21,6 +21,7 @@ import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper.DefaultTypeResolverBuilder; @@ -49,7 +50,32 @@ public class JsonJacksonCodec implements RedissonCodec { objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); // type info inclusion - TypeResolverBuilder typer = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL); + TypeResolverBuilder typer = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL) { + public boolean useForType(JavaType t) + { + switch (_appliesFor) { + case NON_CONCRETE_AND_ARRAYS: + while (t.isArrayType()) { + t = t.getContentType(); + } + // fall through + case OBJECT_AND_NON_CONCRETE: + return (t.getRawClass() == Object.class) || !t.isConcrete(); + case NON_FINAL: + while (t.isArrayType()) { + t = t.getContentType(); + } + // to fix problem with wrong long to int conversion + if (t.isPrimitive() || t.getRawClass() == Long.class) { + return true; + } + return !t.isFinal(); // includes Object.class + default: + //case JAVA_LANG_OBJECT: + return (t.getRawClass() == Object.class); + } + } + }; typer.init(JsonTypeInfo.Id.CLASS, null); typer.inclusion(JsonTypeInfo.As.PROPERTY); objectMapper.setDefaultTyping(typer); diff --git a/src/test/java/org/redisson/RedissonMapTest.java b/src/test/java/org/redisson/RedissonMapTest.java index e01247469..f9b4b8d93 100644 --- a/src/test/java/org/redisson/RedissonMapTest.java +++ b/src/test/java/org/redisson/RedissonMapTest.java @@ -111,6 +111,40 @@ public class RedissonMapTest extends BaseTest { } + @Test + public void testInteger() { + Redisson redisson = Redisson.create(); + Map map = redisson.getMap("test_int"); + map.put(1, 2); + map.put(3, 4); + + Assert.assertEquals(2, map.size()); + + Integer val = map.get(1); + Assert.assertEquals(2, val.intValue()); + Integer val2 = map.get(3); + Assert.assertEquals(4, val2.intValue()); + + clear(map, redisson); + } + + @Test + public void testLong() { + Redisson redisson = Redisson.create(); + Map map = redisson.getMap("test_long"); + map.put(1L, 2L); + map.put(3L, 4L); + + Assert.assertEquals(2, map.size()); + + Long val = map.get(1L); + Assert.assertEquals(2L, val.longValue()); + Long val2 = map.get(3L); + Assert.assertEquals(4L, val2.longValue()); + + clear(map, redisson); + } + @Test public void testNull() { Redisson redisson = Redisson.create();