diff --git a/src/main/java/com/redislabs/modules/rejson/JReJSON.java b/src/main/java/com/redislabs/modules/rejson/JReJSON.java index c11ac1a..fd17b56 100644 --- a/src/main/java/com/redislabs/modules/rejson/JReJSON.java +++ b/src/main/java/com/redislabs/modules/rejson/JReJSON.java @@ -186,8 +186,21 @@ public 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 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 get(String key, Class clazz, Path... paths) { byte[][] args = new byte[1 + paths.length][]; int i=0; args[i] = SafeEncoder.encode(key); @@ -201,7 +214,7 @@ public T get(String key, Path... paths) { rep = conn.getClient().getBulkReply(); } assertReplyNotError(rep); - return (T)gson.fromJson(rep, Object.class); + return gson.fromJson(rep, clazz); } /** diff --git a/src/test/java/com/redislabs/modules/rejson/StaticClientTest.java b/src/test/java/com/redislabs/modules/rejson/StaticClientTest.java index 8a2e5e1..a61283f 100644 --- a/src/test/java/com/redislabs/modules/rejson/StaticClientTest.java +++ b/src/test/java/com/redislabs/modules/rejson/StaticClientTest.java @@ -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 */ @@ -55,6 +57,18 @@ public FooBarObject() { this.foo = "bar"; } } + + private static class HM { + public HashMap 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"; @@ -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.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 {