Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ public class JacksonJsonRedisSerializer<T> implements RedisSerializer<T> {

private final JacksonObjectWriter writer;

/**
* Creates a new {@link JacksonJsonRedisSerializer} for the given target {@link Class}.
* <p>
* This is a convenience factory method equivalent to calling the constructor directly.
*
* @param type must not be {@literal null}.
* @param <T> the target type.
* @return new instance of {@link JacksonJsonRedisSerializer}. Never {@literal null}.
* @since 4.0
*/
public static <T> JacksonJsonRedisSerializer<T> of(Class<T> type) {
return new JacksonJsonRedisSerializer<>(type);
}

/**
* Creates a new {@link JacksonJsonRedisSerializer} for the given target {@link Class}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ static RedisSerializer<Object> json() {
.create(it -> it.enableSpringCacheNullValueSupport().enableUnsafeDefaultTyping());
}

/**
* Obtain a {@link RedisSerializer} that can read and write JSON using
* <a href="https://github.com/FasterXML/jackson-core">Jackson</a> for a specific type.
* <p>
* Unlike {@link #json()} which uses default typing, this method creates a type-safe serializer
* bound to the given class.
*
* @param type must not be {@literal null}.
* @param <T> the target type.
* @return never {@literal null}.
* @since 4.0
*/
static <T> RedisSerializer<T> json(Class<T> type) {
return new JacksonJsonRedisSerializer<>(type);
}

/**
* Obtain a simple {@link java.lang.String} to {@code byte[]} (and back) serializer using
* {@link java.nio.charset.StandardCharsets#UTF_8 UTF-8} as the default {@link java.nio.charset.Charset}.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2025 the original author or authors.
*
* 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
*
* https://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.springframework.data.redis.serializer;

import static org.assertj.core.api.Assertions.*;

import org.junit.jupiter.api.Test;

import org.springframework.data.redis.Person;
import org.springframework.data.redis.PersonObjectFactory;

/**
* Unit tests for {@link JacksonJsonRedisSerializer}.
*
* @author Minjun Choi
*/
class JacksonJsonRedisSerializerUnitTests {

@Test // GH-3252
void ofShouldCreateSerializerForGivenType() {

JacksonJsonRedisSerializer<Person> serializer = JacksonJsonRedisSerializer.of(Person.class);

assertThat(serializer).isNotNull();

Person person = new PersonObjectFactory().instance();
byte[] serialized = serializer.serialize(person);
Person deserialized = serializer.deserialize(serialized);

assertThat(deserialized).isEqualTo(person);
}

@Test // GH-3252
void redisSerializerJsonShouldCreateSerializerForGivenType() {

RedisSerializer<Person> serializer = RedisSerializer.json(Person.class);

assertThat(serializer).isNotNull();
assertThat(serializer).isInstanceOf(JacksonJsonRedisSerializer.class);

Person person = new PersonObjectFactory().instance();
byte[] serialized = serializer.serialize(person);
Person deserialized = serializer.deserialize(serialized);

assertThat(deserialized).isEqualTo(person);
}

@Test // GH-3252
void serializeShouldReturnEmptyByteArrayWhenSourceIsNull() {

JacksonJsonRedisSerializer<Person> serializer = JacksonJsonRedisSerializer.of(Person.class);

assertThat(serializer.serialize(null)).isEqualTo(SerializationUtils.EMPTY_ARRAY);
}

@Test // GH-3252
void deserializeShouldReturnNullWhenSourceIsNull() {

JacksonJsonRedisSerializer<Person> serializer = JacksonJsonRedisSerializer.of(Person.class);

assertThat(serializer.deserialize(null)).isNull();
}

@Test // GH-3252
void deserializeShouldReturnNullWhenSourceIsEmptyArray() {

JacksonJsonRedisSerializer<Person> serializer = JacksonJsonRedisSerializer.of(Person.class);

assertThat(serializer.deserialize(SerializationUtils.EMPTY_ARRAY)).isNull();
}
}