Map decoding/encoding support
parent
a50a2f0154
commit
8082f9f8fa
@ -1,35 +0,0 @@
|
|||||||
package org.redisson.client.protocol;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import io.netty.buffer.ByteBufInputStream;
|
|
||||||
|
|
||||||
public class JsonCodec implements Codec {
|
|
||||||
|
|
||||||
public static final JsonCodec INSTANCE = new JsonCodec();
|
|
||||||
|
|
||||||
private final ObjectMapper mapper = new ObjectMapper();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte[] encode(int paramIndex, Object in) {
|
|
||||||
try {
|
|
||||||
return mapper.writeValueAsBytes(in);
|
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
throw new IllegalStateException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object decode(ByteBuf buf) {
|
|
||||||
try {
|
|
||||||
return mapper.readValue(new ByteBufInputStream(buf), Object.class);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new IllegalStateException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,82 @@
|
|||||||
|
/**
|
||||||
|
* 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.client.protocol;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.util.CharsetUtil;
|
||||||
|
|
||||||
|
public class StringIntegerCodec implements Codec {
|
||||||
|
|
||||||
|
public static final StringIntegerCodec INSTANCE = new StringIntegerCodec();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Decoder<Object> getValueDecoder() {
|
||||||
|
return new Decoder<Object>() {
|
||||||
|
@Override
|
||||||
|
public Object decode(ByteBuf buf) {
|
||||||
|
if (buf == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return buf.toString(CharsetUtil.UTF_8);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Encoder getValueEncoder() {
|
||||||
|
return new Encoder() {
|
||||||
|
@Override
|
||||||
|
public byte[] encode(int paramIndex, Object in) {
|
||||||
|
try {
|
||||||
|
return in.toString().getBytes("UTF-8");
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Decoder<Object> getMapValueDecoder() {
|
||||||
|
return new Decoder<Object>() {
|
||||||
|
@Override
|
||||||
|
public Object decode(ByteBuf buf) {
|
||||||
|
if (buf == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return Integer.valueOf(buf.toString(CharsetUtil.UTF_8));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Encoder getMapValueEncoder() {
|
||||||
|
return getValueEncoder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Decoder<Object> getMapKeyDecoder() {
|
||||||
|
return getValueDecoder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Encoder getMapKeyEncoder() {
|
||||||
|
return getValueEncoder();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package org.redisson.client.protocol;
|
package org.redisson.client.protocol.decoder;
|
||||||
|
|
||||||
public class KeyValueMessage<K, V> {
|
public class KeyValueMessage<K, V> {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.redisson.client.protocol;
|
package org.redisson.client.protocol.decoder;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
Loading…
Reference in New Issue