Skip to content

Commit 0ee3a06

Browse files
committed
Add static factory method for JacksonJsonRedisSerializer.
Introduces JacksonJsonRedisSerializer.of(Class<T>) as a static factory method for creating serializer instances, and adds RedisSerializer.json(Class<T>) as an interface-level helper method. This simplifies RedisTemplate setup code and aligns with modern Java API design conventions. Closes #3252 Signed-off-by: Minjun Choi <mj043000@naver.com>
1 parent 6e3608a commit 0ee3a06

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

src/main/java/org/springframework/data/redis/serializer/JacksonJsonRedisSerializer.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ public class JacksonJsonRedisSerializer<T> implements RedisSerializer<T> {
5050

5151
private final JacksonObjectWriter writer;
5252

53+
/**
54+
* Creates a new {@link JacksonJsonRedisSerializer} for the given target {@link Class}.
55+
* <p>
56+
* This is a convenience factory method equivalent to calling the constructor directly.
57+
*
58+
* @param type must not be {@literal null}.
59+
* @param <T> the target type.
60+
* @return new instance of {@link JacksonJsonRedisSerializer}. Never {@literal null}.
61+
* @since 4.0
62+
*/
63+
public static <T> JacksonJsonRedisSerializer<T> of(Class<T> type) {
64+
return new JacksonJsonRedisSerializer<>(type);
65+
}
66+
5367
/**
5468
* Creates a new {@link JacksonJsonRedisSerializer} for the given target {@link Class}.
5569
*

src/main/java/org/springframework/data/redis/serializer/RedisSerializer.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ static RedisSerializer<Object> json() {
6565
.create(it -> it.enableSpringCacheNullValueSupport().enableUnsafeDefaultTyping());
6666
}
6767

68+
/**
69+
* Obtain a {@link RedisSerializer} that can read and write JSON using
70+
* <a href="https://github.com/FasterXML/jackson-core">Jackson</a> for a specific type.
71+
* <p>
72+
* Unlike {@link #json()} which uses default typing, this method creates a type-safe serializer
73+
* bound to the given class.
74+
*
75+
* @param type must not be {@literal null}.
76+
* @param <T> the target type.
77+
* @return never {@literal null}.
78+
* @since 4.0
79+
*/
80+
static <T> RedisSerializer<T> json(Class<T> type) {
81+
return new JacksonJsonRedisSerializer<>(type);
82+
}
83+
6884
/**
6985
* Obtain a simple {@link java.lang.String} to {@code byte[]} (and back) serializer using
7086
* {@link java.nio.charset.StandardCharsets#UTF_8 UTF-8} as the default {@link java.nio.charset.Charset}.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.serializer;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.data.redis.Person;
23+
import org.springframework.data.redis.PersonObjectFactory;
24+
25+
/**
26+
* Unit tests for {@link JacksonJsonRedisSerializer}.
27+
*
28+
* @author Minjun Choi
29+
*/
30+
class JacksonJsonRedisSerializerUnitTests {
31+
32+
@Test // GH-3252
33+
void ofShouldCreateSerializerForGivenType() {
34+
35+
JacksonJsonRedisSerializer<Person> serializer = JacksonJsonRedisSerializer.of(Person.class);
36+
37+
assertThat(serializer).isNotNull();
38+
39+
Person person = new PersonObjectFactory().instance();
40+
byte[] serialized = serializer.serialize(person);
41+
Person deserialized = serializer.deserialize(serialized);
42+
43+
assertThat(deserialized).isEqualTo(person);
44+
}
45+
46+
@Test // GH-3252
47+
void redisSerializerJsonShouldCreateSerializerForGivenType() {
48+
49+
RedisSerializer<Person> serializer = RedisSerializer.json(Person.class);
50+
51+
assertThat(serializer).isNotNull();
52+
assertThat(serializer).isInstanceOf(JacksonJsonRedisSerializer.class);
53+
54+
Person person = new PersonObjectFactory().instance();
55+
byte[] serialized = serializer.serialize(person);
56+
Person deserialized = serializer.deserialize(serialized);
57+
58+
assertThat(deserialized).isEqualTo(person);
59+
}
60+
61+
@Test // GH-3252
62+
void serializeShouldReturnEmptyByteArrayWhenSourceIsNull() {
63+
64+
JacksonJsonRedisSerializer<Person> serializer = JacksonJsonRedisSerializer.of(Person.class);
65+
66+
assertThat(serializer.serialize(null)).isEqualTo(SerializationUtils.EMPTY_ARRAY);
67+
}
68+
69+
@Test // GH-3252
70+
void deserializeShouldReturnNullWhenSourceIsNull() {
71+
72+
JacksonJsonRedisSerializer<Person> serializer = JacksonJsonRedisSerializer.of(Person.class);
73+
74+
assertThat(serializer.deserialize(null)).isNull();
75+
}
76+
77+
@Test // GH-3252
78+
void deserializeShouldReturnNullWhenSourceIsEmptyArray() {
79+
80+
JacksonJsonRedisSerializer<Person> serializer = JacksonJsonRedisSerializer.of(Person.class);
81+
82+
assertThat(serializer.deserialize(SerializationUtils.EMPTY_ARRAY)).isNull();
83+
}
84+
}

0 commit comments

Comments
 (0)