Skip to content
Merged
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
22 changes: 18 additions & 4 deletions src/main/java/com/redislabs/modules/rejson/JReJSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.stream.Stream;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
Expand All @@ -48,7 +49,13 @@
*/
public class JReJSON {

private static final Gson gson = new Gson();
private GsonBuilder gsonBuilder = new GsonBuilder();
private Gson gson = gsonBuilder.create();

public void setGsonBuilder(GsonBuilder gsonBuilder) {
this.gsonBuilder = gsonBuilder;
this.gson = gsonBuilder.create();
}

private enum Command implements ProtocolCommand {
DEL("JSON.DEL"),
Expand Down Expand Up @@ -100,6 +107,7 @@ public byte[] getRaw() {
}

private Pool<Jedis> client;
private Jedis jedis;

/**
* Creates a client to the local machine
Expand Down Expand Up @@ -127,6 +135,10 @@ public JReJSON(Pool<Jedis> jedis) {
this.client = jedis;
}

public JReJSON(Jedis jedis) {
this.jedis = jedis;
}

/**
* Helper to check for an OK reply
* @param str the reply string to "scrutinize"
Expand Down Expand Up @@ -389,6 +401,8 @@ public Class<?> type(String key, Path path) {
}
}

private static final Gson staticGson = new Gson();

/**
* Deletes a path
* @param conn the Jedis connection
Expand Down Expand Up @@ -436,7 +450,7 @@ public static Object get(Jedis conn, String key, Path... paths) {
String rep = conn.getClient().getBulkReply();
conn.close();

return gson.fromJson(rep, Object.class);
return staticGson.fromJson(rep, Object.class);
}

/**
Expand All @@ -455,7 +469,7 @@ public static void set(Jedis conn, String key, Object object, ExistenceModifier

args.add(SafeEncoder.encode(key));
args.add(SafeEncoder.encode(getSingleOptionalPath(path).toString()));
args.add(SafeEncoder.encode(gson.toJson(object)));
args.add(SafeEncoder.encode(staticGson.toJson(object)));
if (ExistenceModifier.DEFAULT != flag) {
args.add(flag.getRaw());
}
Expand Down Expand Up @@ -523,7 +537,7 @@ public static Class<?> type(Jedis conn, String key, Path... path) {
}

private Jedis getConnection() {
return this.client.getResource();
return jedis != null ? jedis : this.client.getResource();
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/com/redislabs/modules/rejson/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.GsonBuilder;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;
Expand Down Expand Up @@ -156,6 +157,16 @@ public void noArgsConstructorReturnsClientToLocalMachine() {
defaultClient.set("null", null, Path.ROOT_PATH);
assertNull(explicitLocalClient.get("null", String.class, Path.ROOT_PATH));
}

@Test
public void testJedisConstructor() {
final JReJSON defaultClient = new JReJSON();
final JReJSON withJedis = new JReJSON(jedis);

// naive set with a path
defaultClient.set("null", null, Path.ROOT_PATH);
assertNull(withJedis.get("null", String.class, Path.ROOT_PATH));
}

@Test
public void basicSetGetShouldSucceed() {
Expand Down Expand Up @@ -568,4 +579,17 @@ public void testStringLen() {
client.set( "str", "foo", Path.ROOT_PATH);
assertEquals(Long.valueOf(3L), client.strLen( "str", Path.ROOT_PATH));
}

@Test
public void testPassingCustomGsonBuilder() {
final JReJSON defaultClient = new JReJSON();
final JReJSON customizedClient = new JReJSON();
final GsonBuilder builder = new GsonBuilder();

customizedClient.setGsonBuilder(builder);

// naive set with a path
defaultClient.set("null", null, Path.ROOT_PATH);
assertNull(customizedClient.get("null", String.class, Path.ROOT_PATH));
}
}