/** * Register {@link NullValueSerializer} in the given {@link ObjectMapper} with an optional * {@code classPropertyTypeName}. This method should be called by code that customizes * {@link GenericJackson2JsonRedisSerializer} by providing an external {@link ObjectMapper}. * * @param objectMapper the object mapper to customize. * @param classPropertyTypeName name of the type property. Defaults to {@code @class} if {@literal null}/empty. * @since 2.2 */ public static void registerNullValueSerializer(ObjectMapper objectMapper, @Nullable String classPropertyTypeName) { // simply setting {@code mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)} does not help here since we need // the type hint embedded for deserialization using the default typing feature. objectMapper.registerModule(new SimpleModule().addSerializer(new NullValueSerializer(classPropertyTypeName))); }
/** * Creates {@link GenericJackson2JsonRedisSerializer} and configures {@link ObjectMapper} for default typing using the * given {@literal name}. In case of an {@literal empty} or {@literal null} String the default * {@link JsonTypeInfo.Id#CLASS} will be used. * * @param classPropertyTypeName Name of the JSON property holding type information. Can be {@literal null}. */ public GenericJackson2JsonRedisSerializer(@Nullable String classPropertyTypeName) { this(new ObjectMapper()); // simply setting {@code mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)} does not help here since we need // the type hint embedded for deserialization using the default typing feature. mapper.registerModule(new SimpleModule().addSerializer(new NullValueSerializer(classPropertyTypeName))); if (StringUtils.hasText(classPropertyTypeName)) { mapper.enableDefaultTypingAsProperty(DefaultTyping.NON_FINAL, classPropertyTypeName); } else { mapper.enableDefaultTyping(DefaultTyping.NON_FINAL, As.PROPERTY); } }
/** * Creates {@link GenericJackson2JsonRedisSerializer} and configures {@link ObjectMapper} for default typing using the * given {@literal name}. In case of an {@literal empty} or {@literal null} String the default * {@link JsonTypeInfo.Id#CLASS} will be used. * * @param classPropertyTypeName Name of the JSON property holding type information. Can be {@literal null}. */ public GenericJackson2JsonRedisSerializer(@Nullable String classPropertyTypeName) { this(new ObjectMapper()); // simply setting {@code mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)} does not help here since we need // the type hint embedded for deserialization using the default typing feature. mapper.registerModule(new SimpleModule().addSerializer(new NullValueSerializer(classPropertyTypeName))); if (StringUtils.hasText(classPropertyTypeName)) { mapper.enableDefaultTypingAsProperty(DefaultTyping.NON_FINAL, classPropertyTypeName); } else { mapper.enableDefaultTyping(DefaultTyping.NON_FINAL, As.PROPERTY); } }