Skip to content

Commit

Permalink
Support RedisJSON SET/GET as plain string(s) (#3005)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 authored May 28, 2022
1 parent 9e6cddf commit 96968e4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -3157,6 +3157,10 @@ public final CommandObject<String> jsonSet(String key, Path path, Object pojo) {
return new CommandObject<>(commandArguments(JsonCommand.SET).key(key).add(path).add(GSON.toJson(pojo)), BuilderFactory.STRING);
}

public final CommandObject<String> jsonSetWithPlainString(String key, Path path, String string) {
return new CommandObject<>(commandArguments(JsonCommand.SET).key(key).add(path).add(string), BuilderFactory.STRING);
}

public final CommandObject<String> jsonSet(String key, Path2 path, Object object, JsonSetParams params) {
return new CommandObject<>(commandArguments(JsonCommand.SET).key(key).add(path).add(object).addParams(params), BuilderFactory.STRING);
}
Expand Down Expand Up @@ -3185,6 +3189,10 @@ public final CommandObject<Object> jsonGet(String key, Path... paths) {
return new CommandObject<>(commandArguments(JsonCommand.GET).key(key).addObjects((Object[]) paths), new GsonObjectBuilder<>(Object.class));
}

public final CommandObject<String> jsonGetAsPlainString(String key, Path path) {
return new CommandObject<>(commandArguments(JsonCommand.GET).key(key).add(path), BuilderFactory.STRING);
}

public final <T> CommandObject<T> jsonGet(String key, Class<T> clazz, Path... paths) {
return new CommandObject<>(commandArguments(JsonCommand.GET).key(key).addObjects((Object[]) paths), new GsonObjectBuilder<>(clazz));
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3539,6 +3539,11 @@ public String jsonSet(String key, Path path, Object pojo) {
return executeCommand(commandObjects.jsonSet(key, path, pojo));
}

@Override
public String jsonSetWithPlainString(String key, Path path, String string) {
return executeCommand(commandObjects.jsonSetWithPlainString(key, path, string));
}

@Override
public String jsonSet(String key, Path2 path, Object pojo, JsonSetParams params) {
return executeCommand(commandObjects.jsonSet(key, path, pojo, params));
Expand Down Expand Up @@ -3574,6 +3579,11 @@ public Object jsonGet(String key, Path... paths) {
return executeCommand(commandObjects.jsonGet(key, paths));
}

@Override
public String jsonGetAsPlainString(String key, Path path) {
return executeCommand(commandObjects.jsonGetAsPlainString(key, path));
}

@Override
public <T> T jsonGet(String key, Class<T> clazz, Path... paths) {
return executeCommand(commandObjects.jsonGet(key, clazz, paths));
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/json/RedisJsonCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ default String jsonSetLegacy(String key, Object pojo, JsonSetParams params) {

String jsonSet(String key, Path path, Object pojo);

String jsonSetWithPlainString(String key, Path path, String string);

String jsonSet(String key, Path2 path, Object object, JsonSetParams params);

String jsonSetWithEscape(String key, Path2 path, Object object, JsonSetParams params);
Expand All @@ -49,6 +51,8 @@ default String jsonSetLegacy(String key, Object pojo, JsonSetParams params) {

Object jsonGet(String key, Path... paths);

String jsonGetAsPlainString(String key, Path path);

<T> T jsonGet(String key, Class<T> clazz, Path... paths);

default List<JSONArray> jsonMGet(String... keys) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,11 @@ public void debugMemory() {
client.jsonDebugMemory("json", ROOT_PATH);
client.jsonDebugMemory("json", Path.of(".bar"));
}

@Test
public void plainString() {
String json = "{\"foo\":\"bar\",\"bar\":{\"foo\":10}}";
assertEquals("OK", client.jsonSetWithPlainString("plain", ROOT_PATH, json));
assertEquals(json, client.jsonGetAsPlainString("plain", ROOT_PATH));
}
}

0 comments on commit 96968e4

Please sign in to comment.