谈谈spring-boot-starter-data-redis序列化 (2)

Serializer 接口

void serialize(T object, OutputStream outputStream) throws IOException; default byte[] serializeToByteArray(T object) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(1024); serialize(object, out); return out.toByteArray(); }

在这里JdkSerializationRedisSerializer中,使用的是DefaultSerializer,它实现了serialize方法:

public class DefaultSerializer implements Serializer<Object> { /** * Writes the source object to an output stream using Java serialization. * The source object must implement {@link Serializable}. * @see ObjectOutputStream#writeObject(Object) */ @Override public void serialize(Object object, OutputStream outputStream) throws IOException { if (!(object instanceof Serializable)) { throw new IllegalArgumentException(getClass().getSimpleName() + " requires a Serializable payload " + "but received an object of type [" + object.getClass().getName() + "]"); } ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(object); objectOutputStream.flush(); } }

可以看到使用了ObjectOutputStream的writeObject方法来实现的,下面会继续调用writeObject0方法,相关可以查看ObjectOutputStream的序列化和反序列化。
JdkSerializationRedisSerializer的反序列化方式转化类型有区别,这里就不详细介绍了。

2.2 GenericJackson2JsonRedisSerializer序列化
GenericJackson2JsonRedisSerializer主要使用ObjectMapper来实现。

@Override public byte[] serialize(@Nullable Object source) throws SerializationException { if (source == null) { return SerializationUtils.EMPTY_ARRAY; } try { return mapper.writeValueAsBytes(source); } catch (JsonProcessingException e) { throw new SerializationException("Could not write JSON: " + e.getMessage(), e); } } @Override public Object deserialize(@Nullable byte[] source) throws SerializationException { return deserialize(source, Object.class); } public <T> T deserialize(@Nullable byte[] source, Class<T> type) throws SerializationException { Assert.notNull(type, "Deserialization type must not be null! Please provide Object.class to make use of Jackson2 default typing."); if (SerializationUtils.isEmpty(source)) { return null; } try { return mapper.readValue(source, type); } catch (Exception ex) { throw new SerializationException("Could not read JSON: " + ex.getMessage(), ex); } }

查看writeValueAsBytes方法,并且继续向下,可以看到使用了jackson相关包进行json化数据。

private final void _serialize(JsonGenerator gen, Object value, JsonSerializer<Object> ser, PropertyName rootName) throws IOException { try { gen.writeStartObject(); gen.writeFieldName(rootName.simpleAsEncoded(_config)); ser.serialize(value, gen, this); gen.writeEndObject(); } catch (Exception e) { throw _wrapAsIOE(gen, e); } }

2.3 StringRedisSerializer
StringRedisTemplate中使用了UTF_8的编码格式。

public class StringRedisSerializer implements RedisSerializer<String> { private final Charset charset; /** * {@link StringRedisSerializer} to use 7 bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode * character set. * * @see StandardCharsets#US_ASCII * @since 2.1 */ public static final StringRedisSerializer US_ASCII = new StringRedisSerializer(StandardCharsets.US_ASCII); /** * {@link StringRedisSerializer} to use ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1. * * @see StandardCharsets#ISO_8859_1 * @since 2.1 */ public static final StringRedisSerializer ISO_8859_1 = new StringRedisSerializer(StandardCharsets.ISO_8859_1); /** * {@link StringRedisSerializer} to use 8 bit UCS Transformation Format. * * @see StandardCharsets#UTF_8 * @since 2.1 */ public static final StringRedisSerializer UTF_8 = new StringRedisSerializer(StandardCharsets.UTF_8); /** * Creates a new {@link StringRedisSerializer} using {@link StandardCharsets#UTF_8 UTF-8}. */ public StringRedisSerializer() { this(StandardCharsets.UTF_8); } /** * Creates a new {@link StringRedisSerializer} using the given {@link Charset} to encode and decode strings. * * @param charset must not be {@literal null}. */ public StringRedisSerializer(Charset charset) { Assert.notNull(charset, "Charset must not be null!"); this.charset = charset; } /* * (non-Javadoc) * @see org.springframework.data.redis.serializer.RedisSerializer#deserialize(byte[]) */ @Override public String deserialize(@Nullable byte[] bytes) { return (bytes == null ? null : new String(bytes, charset)); } /* * (non-Javadoc) * @see org.springframework.data.redis.serializer.RedisSerializer#serialize(java.lang.Object) */ @Override public byte[] serialize(@Nullable String string) { return (string == null ? null : string.getBytes(charset)); } @Override public Class<?> getTargetType() { return String.class; } }

当你的redis数据库里面本来存的是字符串数据或者你要存取的数据就是字符串类型数据的时候,可以使用这种方式,非常简便。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpgpdj.html