package com.qianwen.core.redis.serializer; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.StdSerializer; import java.io.IOException; import java.time.ZoneId; import java.util.TimeZone; import com.qianwen.core.redis.config.BladeRedisSerializerConfigAble; import org.springframework.cache.support.NullValue; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.SerializationException; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.StringUtils; /* loaded from: blade-starter-redis-9.3.0.0-SNAPSHOT.jar:org/springblade/core/redis/serializer/GenericJackson2JsonRedisSerializer.class */ public class GenericJackson2JsonRedisSerializer implements RedisSerializer { private final ObjectMapper mapper; private ObjectMapper createGenericObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); objectMapper.registerModule(new SimpleModule().addSerializer(new NullValueSerializer(null))); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); return objectMapper; } public GenericJackson2JsonRedisSerializer() { this((String) null); } public GenericJackson2JsonRedisSerializer(@Nullable String classPropertyTypeName) { this(new ObjectMapper()); registerNullValueSerializer(this.mapper, classPropertyTypeName); if (StringUtils.hasText(classPropertyTypeName)) { this.mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, classPropertyTypeName); } else { this.mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); } } public GenericJackson2JsonRedisSerializer(ObjectMapper mapper) { Assert.notNull(mapper, "ObjectMapper must not be null!"); this.mapper = createGenericObjectMapper(); } public static void registerNullValueSerializer(ObjectMapper objectMapper, @Nullable String classPropertyTypeName) { objectMapper.registerModule(new SimpleModule().addSerializer(new NullValueSerializer(classPropertyTypeName))); } public byte[] serialize(@Nullable Object source) throws SerializationException { if (source == null) { return new byte[0]; } try { return this.mapper.writeValueAsBytes(source); } catch (JsonProcessingException var3) { throw new SerializationException("Could not write JSON: " + var3.getMessage(), var3); } } public Object deserialize(@Nullable byte[] source) throws SerializationException { return deserialize(source, Object.class); } @Nullable public T deserialize(@Nullable byte[] source, Class type) throws SerializationException { Assert.notNull(type, "Deserialization type must not be null! Please provide Object.class to make use of Jackson2 default typing."); if (isEmpty(source)) { return null; } try { return (T) this.mapper.readValue(source, type); } catch (Exception var4) { throw new SerializationException("Could not read JSON: " + var4.getMessage(), var4); } } static boolean isEmpty(@Nullable byte[] data) { return data == null || data.length == 0; } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: blade-starter-redis-9.3.0.0-SNAPSHOT.jar:org/springblade/core/redis/serializer/GenericJackson2JsonRedisSerializer$NullValueSerializer.class */ public static class NullValueSerializer extends StdSerializer { private static final long serialVersionUID = 1999052150548658808L; private final String classIdentifier; NullValueSerializer(@Nullable String classIdentifier) { super(NullValue.class); this.classIdentifier = StringUtils.hasText(classIdentifier) ? classIdentifier : BladeRedisSerializerConfigAble.TYPE_NAME; } public void serialize(NullValue value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStartObject(); jgen.writeStringField(this.classIdentifier, NullValue.class.getName()); jgen.writeEndObject(); } } }