diff --git a/pom.xml b/pom.xml index 8b8a6c9a6..2b4bb43e3 100644 --- a/pom.xml +++ b/pom.xml @@ -167,6 +167,11 @@ jackson-databind 2.5.4 + + com.fasterxml.jackson.dataformat + jackson-dataformat-cbor + 2.5.4 + diff --git a/src/main/java/org/redisson/codec/CborJacksonCodec.java b/src/main/java/org/redisson/codec/CborJacksonCodec.java new file mode 100644 index 000000000..17794eb22 --- /dev/null +++ b/src/main/java/org/redisson/codec/CborJacksonCodec.java @@ -0,0 +1,31 @@ +/** + * Copyright 2014 Nikita Koksharov, Nickolay Borbit + * + * 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.redisson.codec; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.cbor.CBORFactory; + +/** + * + * @author Faye Li + * @date 2015-10-16 + */ +public class CborJacksonCodec extends JsonJacksonCodec { + @Override + protected ObjectMapper initObjectMapper() { + return new ObjectMapper(new CBORFactory()); + } +} diff --git a/src/main/java/org/redisson/codec/JsonJacksonCodec.java b/src/main/java/org/redisson/codec/JsonJacksonCodec.java index 564a97881..d2d45d8e7 100755 --- a/src/main/java/org/redisson/codec/JsonJacksonCodec.java +++ b/src/main/java/org/redisson/codec/JsonJacksonCodec.java @@ -44,7 +44,11 @@ import io.netty.buffer.ByteBufInputStream; */ public class JsonJacksonCodec implements Codec { - private final ObjectMapper mapObjectMapper = new ObjectMapper(); + private final ObjectMapper mapObjectMapper = initObjectMapper(); + + protected ObjectMapper initObjectMapper() { + return new ObjectMapper(); + } private final Encoder encoder = new Encoder() { @Override diff --git a/src/test/java/org/redisson/RedissonCodecTest.java b/src/test/java/org/redisson/RedissonCodecTest.java new file mode 100644 index 000000000..30304aec7 --- /dev/null +++ b/src/test/java/org/redisson/RedissonCodecTest.java @@ -0,0 +1,88 @@ +package org.redisson; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.junit.Assert; +import org.junit.Test; +import org.redisson.client.codec.Codec; +import org.redisson.codec.CborJacksonCodec; +import org.redisson.codec.JsonJacksonCodec; +import org.redisson.codec.KryoCodec; +import org.redisson.codec.SerializationCodec; +import org.redisson.core.RMap; + +public class RedissonCodecTest extends BaseTest { + private Codec codec = new SerializationCodec(); + private Codec kryoCodec = new KryoCodec(); + private Codec jsonCodec = new JsonJacksonCodec(); + private Codec cborCodec = new CborJacksonCodec(); + + @Test + public void testJdk() { + Config config = createConfig(); + config.setCodec(codec); + redisson = Redisson.create(config); + + test(); + } + + @Test + public void testJson() { + Config config = createConfig(); + config.setCodec(jsonCodec); + redisson = Redisson.create(config); + + test(); + } + + public void test() { + RMap> map = redisson.getMap("getAll"); + Map a = new HashMap(); + a.put("double", new Double(100000.0)); + a.put("float", 100.0f); + a.put("int", 100); + a.put("long", 10000000000L); + a.put("boolt", true); + a.put("boolf", false); + a.put("string", "testString"); + a.put("array", Arrays.asList(1, 2.0, "adsfasdfsdf")); + + map.fastPut(1, a); + Map resa = map.get(1); + Assert.assertEquals(a, resa); + + Set set = redisson.getSet("set"); + + set.add(new TestObject("1", "2")); + set.add(new TestObject("1", "2")); + set.add(new TestObject("2", "3")); + set.add(new TestObject("3", "4")); + set.add(new TestObject("5", "6")); + + Assert.assertTrue(set.contains(new TestObject("2", "3"))); + Assert.assertTrue(set.contains(new TestObject("1", "2"))); + Assert.assertFalse(set.contains(new TestObject("1", "9"))); + } + + // @Test + public void testKryo() { + Config config = createConfig(); + config.setCodec(kryoCodec); + redisson = Redisson.create(config); + + test(); + } + + @Test + public void testCbor() { + Config config = createConfig(); + config.setCodec(cborCodec); + redisson = Redisson.create(config); + + test(); + + } +}