Skip to content

Commit 040c117

Browse files
denglimingclaude
andcommitted
Make Gson an optional dependency of redisjson with lazy default codec lookup
JsonCodecs.defaultCodec() resolves the Gson-backed codec only when a RedisJSON or RedisJSONClient is created without an explicit JsonCodec, and loads GsonJsonCodec through a holder class so Gson is never touched by applications that supply their own codec. Missing Gson yields a clear IllegalStateException instead of a NoClassDefFoundError. Gson is declared <optional> in redisjson and managed in the parent pom. The all-in-one artifact keeps an explicit Gson dependency so its default keeps working, and redisearch pulls it in test scope for its RedisJSON-based tests. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent eac5fcc commit 040c117

11 files changed

Lines changed: 121 additions & 16 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ Map<String, Object> actual = redisJSON.get(key, Map.class, new GetArgs().path(".
155155
redisJSONClient.shutdown();
156156
```
157157

158-
RedisJSON serializes with Gson by default. Plug in your own `JsonCodec` (Jackson, Moshi, ...) per client, or as a Spring bean when using the starter:
158+
RedisJSON serializes with Gson by default. Gson is an optional dependency of the `redisjson` artifact: add `com.google.code.gson:gson` yourself, or plug in your own `JsonCodec` (Jackson, Moshi, ...) per client or as a Spring bean when using the starter. The `all` artifact includes Gson.
159159
```java
160160
JsonCodec jackson = new JsonCodec() {
161161
private final ObjectMapper mapper = new ObjectMapper();

‎all/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
<groupId>${project.groupId}</groupId>
4444
<artifactId>redisjson</artifactId>
4545
</dependency>
46+
<dependency>
47+
<!-- Gson is optional in redisjson; the all-in-one artifact keeps the default JsonCodec working -->
48+
<groupId>com.google.code.gson</groupId>
49+
<artifactId>gson</artifactId>
50+
</dependency>
4651

4752
<dependency>
4853
<groupId>${project.groupId}</groupId>

‎pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
7070
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
7171
<spring-boot.version>2.7.6</spring-boot.version>
72+
<gson.version>2.8.9</gson.version>
7273
</properties>
7374

7475
<dependencyManagement>
@@ -84,6 +85,11 @@
8485
<version>${project.version}</version>
8586
<type>test-jar</type>
8687
</dependency>
88+
<dependency>
89+
<groupId>com.google.code.gson</groupId>
90+
<artifactId>gson</artifactId>
91+
<version>${gson.version}</version>
92+
</dependency>
8793

8894
<dependency>
8995
<groupId>io.github.dengliming.redismodule</groupId>

‎redisearch/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
<artifactId>redisjson</artifactId>
2828
<scope>test</scope>
2929
</dependency>
30+
<dependency>
31+
<groupId>com.google.code.gson</groupId>
32+
<artifactId>gson</artifactId>
33+
<scope>test</scope>
34+
</dependency>
3035
</dependencies>
3136

3237
</project>

‎redisjson/pom.xml‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
<artifactId>redisjson</artifactId>
1313
<name>RedisJSON</name>
1414

15-
<properties>
16-
<gson.version>2.8.9</gson.version>
17-
</properties>
18-
1915
<dependencies>
2016
<dependency>
2117
<groupId>io.github.dengliming.redismodule</groupId>
@@ -31,7 +27,8 @@
3127
<dependency>
3228
<groupId>com.google.code.gson</groupId>
3329
<artifactId>gson</artifactId>
34-
<version>${gson.version}</version>
30+
<!-- default JsonCodec; applications may supply their own JsonCodec instead -->
31+
<optional>true</optional>
3532
</dependency>
3633
</dependencies>
3734
</project>

‎redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/RedisJSON.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import io.github.dengliming.redismodule.common.util.RAssert;
2121
import io.github.dengliming.redismodule.redisjson.args.GetArgs;
2222
import io.github.dengliming.redismodule.redisjson.args.SetArgs;
23-
import io.github.dengliming.redismodule.redisjson.codec.GsonJsonCodec;
2423
import io.github.dengliming.redismodule.redisjson.codec.JsonCodec;
24+
import io.github.dengliming.redismodule.redisjson.codec.JsonCodecs;
2525
import org.redisson.api.RFuture;
2626
import org.redisson.client.codec.Codec;
2727
import org.redisson.client.codec.StringCodec;
@@ -70,7 +70,7 @@ public class RedisJSON extends AbstractRedisModule {
7070
private final JsonCodec jsonCodec;
7171

7272
public RedisJSON(CommandAsyncExecutor commandExecutor) {
73-
this(commandExecutor, GsonJsonCodec.INSTANCE);
73+
this(commandExecutor, JsonCodecs.defaultCodec());
7474
}
7575

7676
public RedisJSON(CommandAsyncExecutor commandExecutor, JsonCodec jsonCodec) {
@@ -79,7 +79,7 @@ public RedisJSON(CommandAsyncExecutor commandExecutor, JsonCodec jsonCodec) {
7979
}
8080

8181
public RedisJSON(CommandAsyncExecutor commandExecutor, Codec codec) {
82-
this(commandExecutor, codec, GsonJsonCodec.INSTANCE);
82+
this(commandExecutor, codec, JsonCodecs.defaultCodec());
8383
}
8484

8585
/**

‎redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/RedisJSONBatch.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package io.github.dengliming.redismodule.redisjson;
1818

1919
import io.github.dengliming.redismodule.common.api.RCommonBatch;
20-
import io.github.dengliming.redismodule.redisjson.codec.GsonJsonCodec;
2120
import io.github.dengliming.redismodule.redisjson.codec.JsonCodec;
21+
import io.github.dengliming.redismodule.redisjson.codec.JsonCodecs;
2222
import org.redisson.api.BatchOptions;
2323
import org.redisson.command.CommandAsyncExecutor;
2424

@@ -27,7 +27,7 @@ public class RedisJSONBatch extends RCommonBatch {
2727
private final JsonCodec jsonCodec;
2828

2929
public RedisJSONBatch(CommandAsyncExecutor executor, BatchOptions options) {
30-
this(executor, options, GsonJsonCodec.INSTANCE);
30+
this(executor, options, JsonCodecs.defaultCodec());
3131
}
3232

3333
public RedisJSONBatch(CommandAsyncExecutor executor, BatchOptions options, JsonCodec jsonCodec) {

‎redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/client/RedisJSONClient.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import io.github.dengliming.redismodule.common.util.RAssert;
2121
import io.github.dengliming.redismodule.redisjson.RedisJSON;
2222
import io.github.dengliming.redismodule.redisjson.RedisJSONBatch;
23-
import io.github.dengliming.redismodule.redisjson.codec.GsonJsonCodec;
2423
import io.github.dengliming.redismodule.redisjson.codec.JsonCodec;
24+
import io.github.dengliming.redismodule.redisjson.codec.JsonCodecs;
2525
import org.redisson.api.BatchOptions;
2626
import org.redisson.api.RedissonClient;
2727
import org.redisson.config.Config;
@@ -31,7 +31,7 @@ public class RedisJSONClient extends BaseRedissonClient {
3131
private final JsonCodec jsonCodec;
3232

3333
public RedisJSONClient(Config config) {
34-
this(config, GsonJsonCodec.INSTANCE);
34+
this(config, JsonCodecs.defaultCodec());
3535
}
3636

3737
/**
@@ -48,7 +48,7 @@ public RedisJSONClient(Config config, JsonCodec jsonCodec) {
4848
* The caller stays responsible for shutting it down.
4949
*/
5050
public RedisJSONClient(RedissonClient redisson) {
51-
this(redisson, GsonJsonCodec.INSTANCE);
51+
this(redisson, JsonCodecs.defaultCodec());
5252
}
5353

5454
public RedisJSONClient(RedissonClient redisson, JsonCodec jsonCodec) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2024 dengliming.
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+
* http://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+
17+
package io.github.dengliming.redismodule.redisjson.codec;
18+
19+
/**
20+
* Resolves the {@link JsonCodec} used when none is supplied explicitly.
21+
* <p>
22+
* Gson is an optional dependency of this module. The default codec is only looked up when a
23+
* {@code RedisJSON} / {@code RedisJSONClient} is created without a codec, and the Gson-backed class is
24+
* loaded only if Gson is on the classpath, so applications that bring their own {@link JsonCodec}
25+
* never need Gson.
26+
*/
27+
public final class JsonCodecs {
28+
29+
private static final String GSON_CLASS = "com.google.gson.Gson";
30+
31+
private JsonCodecs() {
32+
}
33+
34+
/**
35+
* @return the Gson-backed codec
36+
* @throws IllegalStateException if Gson is not on the classpath
37+
*/
38+
public static JsonCodec defaultCodec() {
39+
if (!isGsonPresent()) {
40+
throw new IllegalStateException("No JsonCodec configured and Gson is not on the classpath. "
41+
+ "Add com.google.code.gson:gson or pass a JsonCodec to RedisJSONClient / RedisJSON.");
42+
}
43+
return GsonHolder.CODEC;
44+
}
45+
46+
public static boolean isGsonPresent() {
47+
try {
48+
Class.forName(GSON_CLASS, false, JsonCodecs.class.getClassLoader());
49+
return true;
50+
} catch (ClassNotFoundException e) {
51+
return false;
52+
}
53+
}
54+
55+
/**
56+
* Holder so that {@link GsonJsonCodec} (and with it Gson) is loaded only on first access.
57+
*/
58+
private static final class GsonHolder {
59+
static final JsonCodec CODEC = GsonJsonCodec.INSTANCE;
60+
}
61+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2024 dengliming.
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+
* http://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+
17+
package io.github.dengliming.redismodule.redisjson.codec;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
public class JsonCodecsTest {
24+
25+
@Test
26+
public void defaultCodecIsGsonWhenPresent() {
27+
assertThat(JsonCodecs.isGsonPresent()).isTrue();
28+
assertThat(JsonCodecs.defaultCodec()).isInstanceOf(GsonJsonCodec.class);
29+
assertThat(JsonCodecs.defaultCodec()).isSameAs(JsonCodecs.defaultCodec());
30+
}
31+
}

0 commit comments

Comments
 (0)