Skip to content

Commit 6904445

Browse files
committed
Allow exclusion of org.json dependency
Move creation of the JSON command builders to a separate class which is initialized only when any of the JSON methods are being used. This allows org.json transitive Maven dependency to be excluded for the applications which don't use JSON methods. See redis#2961 and redis#2962 for more information and rationale. Similar to redis#3223
1 parent 0cf061c commit 6904445

File tree

3 files changed

+73
-51
lines changed

3 files changed

+73
-51
lines changed

src/main/java/redis/clients/jedis/BuilderFactory.java

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import java.io.Serializable;
44
import java.util.*;
55
import java.util.stream.Collectors;
6-
import org.json.JSONArray;
7-
import org.json.JSONException;
8-
import org.json.JSONObject;
96

107
import redis.clients.jedis.exceptions.JedisException;
118
import redis.clients.jedis.resps.StreamConsumerFullInfo;
@@ -1587,51 +1584,6 @@ public List<Class<?>> build(Object data) {
15871584
}
15881585
};
15891586

1590-
public static final Builder<Object> JSON_OBJECT = new Builder<Object>() {
1591-
@Override
1592-
public Object build(Object data) {
1593-
if (data == null) return null;
1594-
1595-
if (!(data instanceof byte[])) return data;
1596-
1597-
String str = STRING.build(data);
1598-
if (str.charAt(0) == '{') {
1599-
try {
1600-
return new JSONObject(str);
1601-
} catch (Exception ex) { }
1602-
} else if (str.charAt(0) == '[') {
1603-
try {
1604-
return new JSONArray(str);
1605-
} catch (Exception ex) { }
1606-
}
1607-
return str;
1608-
}
1609-
};
1610-
1611-
public static final Builder<JSONArray> JSON_ARRAY = new Builder<JSONArray>() {
1612-
@Override
1613-
public JSONArray build(Object data) {
1614-
if (data == null) return null;
1615-
String str = STRING.build(data);
1616-
try {
1617-
return new JSONArray(str);
1618-
} catch (JSONException ex) {
1619-
// This is not necessary but we are doing this just to make is safer
1620-
// for com.vaadin.external.google:android-json library
1621-
throw new JedisException(ex);
1622-
}
1623-
}
1624-
};
1625-
1626-
public static final Builder<List<JSONArray>> JSON_ARRAY_LIST = new Builder<List<JSONArray>>() {
1627-
@Override
1628-
public List<JSONArray> build(Object data) {
1629-
if (data == null) return null;
1630-
List<Object> list = (List<Object>) data;
1631-
return list.stream().map(o -> JSON_ARRAY.build(o)).collect(Collectors.toList());
1632-
}
1633-
};
1634-
16351587
public static final Builder<List<List<String>>> STRING_LIST_LIST = new Builder<List<List<String>>>() {
16361588
@Override
16371589
@SuppressWarnings("unchecked")

src/main/java/redis/clients/jedis/CommandObjects.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3327,7 +3327,7 @@ public final <T> CommandObject<T> jsonGet(String key, Class<T> clazz) {
33273327
}
33283328

33293329
public final CommandObject<Object> jsonGet(String key, Path2... paths) {
3330-
return new CommandObject<>(commandArguments(JsonCommand.GET).key(key).addObjects((Object[]) paths), BuilderFactory.JSON_OBJECT);
3330+
return new CommandObject<>(commandArguments(JsonCommand.GET).key(key).addObjects((Object[]) paths), JsonBuilderFactory.JSON_OBJECT);
33313331
}
33323332

33333333
public final CommandObject<Object> jsonGet(String key, Path... paths) {
@@ -3343,7 +3343,7 @@ public final <T> CommandObject<T> jsonGet(String key, Class<T> clazz, Path... pa
33433343
}
33443344

33453345
public final CommandObject<List<JSONArray>> jsonMGet(Path2 path, String... keys) {
3346-
return new CommandObject<>(commandArguments(JsonCommand.MGET).keys((Object[]) keys).add(path), BuilderFactory.JSON_ARRAY_LIST);
3346+
return new CommandObject<>(commandArguments(JsonCommand.MGET).keys((Object[]) keys).add(path), JsonBuilderFactory.JSON_ARRAY_LIST);
33473347
}
33483348

33493349
public final <T> CommandObject<List<T>> jsonMGet(Path path, Class<T> clazz, String... keys) {
@@ -3419,7 +3419,7 @@ public final CommandObject<Long> jsonStrLen(String key, Path path) {
34193419
}
34203420

34213421
public final CommandObject<JSONArray> jsonNumIncrBy(String key, Path2 path, double value) {
3422-
return new CommandObject<>(commandArguments(JsonCommand.NUMINCRBY).key(key).add(path).add(value), BuilderFactory.JSON_ARRAY);
3422+
return new CommandObject<>(commandArguments(JsonCommand.NUMINCRBY).key(key).add(path).add(value), JsonBuilderFactory.JSON_ARRAY);
34233423
}
34243424

34253425
public final CommandObject<Double> jsonNumIncrBy(String key, Path path, double value) {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package redis.clients.jedis;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
import org.json.JSONArray;
6+
import org.json.JSONException;
7+
import org.json.JSONObject;
8+
import redis.clients.jedis.exceptions.JedisException;
9+
10+
final class JsonBuilderFactory {
11+
12+
private JsonBuilderFactory() {
13+
}
14+
15+
static final Builder<Object> JSON_OBJECT = new Builder<Object>() {
16+
@Override
17+
public Object build(Object data) {
18+
if (data == null) {
19+
return null;
20+
}
21+
22+
if (!(data instanceof byte[])) {
23+
return data;
24+
}
25+
26+
String str = BuilderFactory.STRING.build(data);
27+
if (str.charAt(0) == '{') {
28+
try {
29+
return new JSONObject(str);
30+
} catch (Exception ex) {
31+
}
32+
} else if (str.charAt(0) == '[') {
33+
try {
34+
return new JSONArray(str);
35+
} catch (Exception ex) {
36+
}
37+
}
38+
return str;
39+
}
40+
};
41+
42+
static final Builder<JSONArray> JSON_ARRAY = new Builder<JSONArray>() {
43+
@Override
44+
public JSONArray build(Object data) {
45+
if (data == null) {
46+
return null;
47+
}
48+
String str = BuilderFactory.STRING.build(data);
49+
try {
50+
return new JSONArray(str);
51+
} catch (JSONException ex) {
52+
// This is not necessary but we are doing this just to make is safer
53+
// for com.vaadin.external.google:android-json library
54+
throw new JedisException(ex);
55+
}
56+
}
57+
};
58+
59+
static final Builder<List<JSONArray>> JSON_ARRAY_LIST = new Builder<List<JSONArray>>() {
60+
@Override
61+
public List<JSONArray> build(Object data) {
62+
if (data == null) {
63+
return null;
64+
}
65+
List<Object> list = (List<Object>) data;
66+
return list.stream().map(o -> JSON_ARRAY.build(o)).collect(Collectors.toList());
67+
}
68+
};
69+
70+
}

0 commit comments

Comments
 (0)