Skip to content

Commit 9b42276

Browse files
committed
fix #16 add get by Class
1 parent 640676b commit 9b42276

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/main/java/com/redislabs/modules/rejson/JReJSON.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,21 @@ public <T> T get(String key) {
186186
* @param key the key name
187187
* @param paths optional one ore more paths in the object
188188
* @return the requested object
189+
* @deprecated use {@link #get(String, Class, Path...)} instead
189190
*/
191+
@Deprecated
190192
public <T> T get(String key, Path... paths) {
193+
return (T)this.get(key, Object.class, paths);
194+
}
195+
196+
/**
197+
* Gets an object
198+
* @param key the key name
199+
* @param clazz
200+
* @param paths optional one ore more paths in the object
201+
* @return the requested object
202+
*/
203+
public <T> T get(String key, Class<T> clazz, Path... paths) {
191204
byte[][] args = new byte[1 + paths.length][];
192205
int i=0;
193206
args[i] = SafeEncoder.encode(key);
@@ -201,7 +214,7 @@ public <T> T get(String key, Path... paths) {
201214
rep = conn.getClient().getBulkReply();
202215
}
203216
assertReplyNotError(rep);
204-
return (T)gson.fromJson(rep, Object.class);
217+
return gson.fromJson(rep, clazz);
205218
}
206219

207220
/**

src/test/java/com/redislabs/modules/rejson/StaticClientTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
import static junit.framework.TestCase.*;
3737

38+
import java.util.HashMap;
39+
3840
public class StaticClientTest {
3941

4042
/* A simple class that represents an object in real life */
@@ -55,6 +57,18 @@ public FooBarObject() {
5557
this.foo = "bar";
5658
}
5759
}
60+
61+
private static class HM {
62+
public HashMap<String, String> v = new HashMap<>();
63+
64+
public void set(String k, String v) {
65+
this.v.put(k, v);
66+
}
67+
68+
public String get(String k) {
69+
return this.v.get(k);
70+
}
71+
}
5872

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

157171
}
172+
173+
/**
174+
* https://github.com/RedisJSON/JRedisJSON/issues/16
175+
*/
176+
@Test
177+
public void objectGeneration() throws Exception {
178+
jedis.flushDB();
179+
180+
HM articleMapOne = new HM();
181+
articleMapOne.set("ar01", "Intro to Map");
182+
articleMapOne.set("ar02", "Some article");
183+
reJSON.set("key", articleMapOne);
184+
HM dest = reJSON.<HM>get("key", HM.class, Path.ROOT_PATH);
185+
assertEquals("Intro to Map", dest.get("ar01"));
186+
assertEquals("Some article", dest.get("ar02"));
187+
}
158188

159189
@Test(expected = Exception.class)
160190
public void getException() throws Exception {

0 commit comments

Comments
 (0)