Merge branch 'master' of github.com:mrniko/redisson
commit
0b5a890ed3
@ -0,0 +1,47 @@
|
||||
package org.redisson.client.codec;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufInputStream;
|
||||
import org.redisson.client.handler.State;
|
||||
import org.redisson.client.protocol.Decoder;
|
||||
import org.redisson.client.protocol.Encoder;
|
||||
import org.redisson.codec.JsonJacksonCodec;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class JsonJacksonMapValueCodec<T> extends JsonJacksonCodec {
|
||||
private final ObjectMapper mapper;
|
||||
private TypeReference<T> typeReference;
|
||||
|
||||
public JsonJacksonMapValueCodec(Class<T> klass) {
|
||||
this(new TypeReference<T>() {
|
||||
});
|
||||
}
|
||||
|
||||
public JsonJacksonMapValueCodec(TypeReference<T> typeReference) {
|
||||
this.typeReference = typeReference;
|
||||
this.mapper = initObjectMapper();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Decoder<Object> getMapValueDecoder() {
|
||||
return new Decoder<Object>() {
|
||||
@Override
|
||||
public Object decode(ByteBuf buf, State state) throws IOException {
|
||||
return mapper.readValue(new ByteBufInputStream(buf), typeReference);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Encoder getMapValueEncoder() {
|
||||
return new Encoder() {
|
||||
@Override
|
||||
public byte[] encode(Object in) throws IOException {
|
||||
return mapper.writeValueAsBytes(in);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package org.redisson.client.codec;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.PooledByteBufAllocator;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.redisson.client.handler.State;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class JsonJacksonMapValueCodecTest {
|
||||
|
||||
private final JsonJacksonMapValueCodec<Map<String, List<String>>> mapCodec = new JsonJacksonMapValueCodec<Map<String, List<String>>>(new TypeReference<Map<String, List<String>>>() {
|
||||
});
|
||||
|
||||
private final JsonJacksonMapValueCodec<String> stringCodec = new JsonJacksonMapValueCodec<String>(String.class);
|
||||
|
||||
private HashMap<String, List<String>> map;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
map = new HashMap<String, List<String>>();
|
||||
map.put("foo", new ArrayList<String>(Arrays.asList("bar")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeserializeTheMapCorrectly() throws Exception {
|
||||
ByteBuf buf = new PooledByteBufAllocator(true).buffer();
|
||||
buf.writeBytes(new ObjectMapper().writeValueAsBytes(map));
|
||||
assertThat(mapCodec.getMapValueDecoder().decode(buf, new State(false)))
|
||||
.isInstanceOf(Map.class)
|
||||
.isEqualTo(map);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSerializeTheMapCorrectly() throws Exception {
|
||||
assertThat(new String(mapCodec.getMapValueEncoder().encode(map), "UTF-8"))
|
||||
.isEqualTo("{\"foo\":[\"bar\"]}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeserializeTheStringCorrectly() throws Exception {
|
||||
ByteBuf buf = new PooledByteBufAllocator(true).buffer();
|
||||
buf.writeBytes(new ObjectMapper().writeValueAsBytes("axk"));
|
||||
assertThat(stringCodec.getMapValueDecoder().decode(buf, new State(false)))
|
||||
.isInstanceOf(String.class)
|
||||
.isEqualTo("axk");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSerializeTheStringCorrectly() throws Exception {
|
||||
assertThat(new String(stringCodec.getMapValueEncoder().encode("foo"), "UTF-8"))
|
||||
.isEqualTo("\"foo\"");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue