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
15 changes: 14 additions & 1 deletion src/main/java/com/redislabs/modules/rejson/JReJSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,21 @@ public <T> T get(String key) {
* @param key the key name
* @param paths optional one ore more paths in the object
* @return the requested object
* @deprecated use {@link #get(String, Class, Path...)} instead
*/
@Deprecated
public <T> T get(String key, Path... paths) {
return (T)this.get(key, Object.class, paths);
}

/**
* Gets an object
* @param key the key name
* @param clazz
* @param paths optional one ore more paths in the object
* @return the requested object
*/
public <T> T get(String key, Class<T> clazz, Path... paths) {
byte[][] args = new byte[1 + paths.length][];
int i=0;
args[i] = SafeEncoder.encode(key);
Expand All @@ -201,7 +214,7 @@ public <T> T get(String key, Path... paths) {
rep = conn.getClient().getBulkReply();
}
assertReplyNotError(rep);
return (T)gson.fromJson(rep, Object.class);
return gson.fromJson(rep, clazz);
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/com/redislabs/modules/rejson/StaticClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

import static junit.framework.TestCase.*;

import java.util.HashMap;

public class StaticClientTest {

/* A simple class that represents an object in real life */
Expand All @@ -55,6 +57,18 @@ public FooBarObject() {
this.foo = "bar";
}
}

private static class HM {
public HashMap<String, String> v = new HashMap<>();

public void set(String k, String v) {
this.v.put(k, v);
}

public String get(String k) {
return this.v.get(k);
}
}

private Gson g;
private String host="localhost";
Expand Down Expand Up @@ -155,6 +169,22 @@ public void getMultiplePathsShouldSucceed() throws Exception {
assertTrue(expected.equals(reJSON.get("obj", new Path("bTrue"), new Path("str"))));

}

/**
* https://github.com/RedisJSON/JRedisJSON/issues/16
*/
@Test
public void objectGeneration() throws Exception {
jedis.flushDB();

HM articleMapOne = new HM();
articleMapOne.set("ar01", "Intro to Map");
articleMapOne.set("ar02", "Some article");
reJSON.set("key", articleMapOne);
HM dest = reJSON.<HM>get("key", HM.class, Path.ROOT_PATH);
assertEquals("Intro to Map", dest.get("ar01"));
assertEquals("Some article", dest.get("ar02"));
}

@Test(expected = Exception.class)
public void getException() throws Exception {
Expand Down