v0.0.9 #9
@@ -92,14 +92,24 @@ public class RedisSerializers {
|
|||||||
* @return Redis 序列化器
|
* @return Redis 序列化器
|
||||||
*/
|
*/
|
||||||
public static <T> RedisSerializer<T> jacksonSerializer(Class<T> clazz) {
|
public static <T> RedisSerializer<T> jacksonSerializer(Class<T> clazz) {
|
||||||
return new RedisSerializer<>() {
|
return jacksonSerializer(new ObjectMapper(), clazz);
|
||||||
|
}
|
||||||
|
|
||||||
private static final ObjectMapper JACKSON = new ObjectMapper();
|
/**
|
||||||
|
* Json 序列化
|
||||||
|
*
|
||||||
|
* @param <T> 数据类型
|
||||||
|
* @param mapper 序列化对象
|
||||||
|
* @param clazz 数据类型
|
||||||
|
* @return Redis 序列化器
|
||||||
|
*/
|
||||||
|
public static <T> RedisSerializer<T> jacksonSerializer(ObjectMapper mapper, Class<T> clazz) {
|
||||||
|
return new RedisSerializer<>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] serialize(T object) throws SerializationException {
|
public byte[] serialize(T object) throws SerializationException {
|
||||||
try {
|
try {
|
||||||
return JACKSON.writeValueAsString(object).getBytes(StandardCharsets.UTF_8);
|
return mapper.writeValueAsString(object).getBytes(StandardCharsets.UTF_8);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@@ -111,7 +121,7 @@ public class RedisSerializers {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return JACKSON.readValue(new String(bytes, StandardCharsets.UTF_8), clazz);
|
return mapper.readValue(new String(bytes, StandardCharsets.UTF_8), clazz);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user