diff --git a/src/main/java/org/redisson/Config.java b/src/main/java/org/redisson/Config.java index 7d35012e6..92699b871 100644 --- a/src/main/java/org/redisson/Config.java +++ b/src/main/java/org/redisson/Config.java @@ -15,7 +15,11 @@ */ package org.redisson; +import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.net.URL; import org.redisson.client.codec.Codec; import org.redisson.codec.JsonJacksonCodec; @@ -335,11 +339,66 @@ public class Config { return useLinuxNativeEpoll; } + /** + * Read config object stored in JSON format from String + * + * @param content + * @return + * @throws IOException + */ public static Config fromJSON(String content) throws IOException { ConfigSupport support = new ConfigSupport(); return support.fromJSON(content); } + /** + * Read config object stored in JSON format from InputStream + * + * @param inputStream + * @return + * @throws IOException + */ + public static Config fromJSON(InputStream inputStream) throws IOException { + ConfigSupport support = new ConfigSupport(); + return support.fromJSON(inputStream); + } + + /** + * Read config object stored in JSON format from File + * + * @param file + * @return + * @throws IOException + */ + public static Config fromJSON(File file) throws IOException { + ConfigSupport support = new ConfigSupport(); + return support.fromJSON(file); + } + + /** + * Read config object stored in JSON format from URL + * + * @param url + * @return + * @throws IOException + */ + public static Config fromJSON(URL url) throws IOException { + ConfigSupport support = new ConfigSupport(); + return support.fromJSON(url); + } + + /** + * Read config object stored in JSON format from Reader + * + * @param reader + * @return + * @throws IOException + */ + public static Config fromJSON(Reader reader) throws IOException { + ConfigSupport support = new ConfigSupport(); + return support.fromJSON(reader); + } + public String toJSON() throws IOException { ConfigSupport support = new ConfigSupport(); return support.toJSON(this); diff --git a/src/main/java/org/redisson/ConfigSupport.java b/src/main/java/org/redisson/ConfigSupport.java index 6bda1dae7..92432b754 100644 --- a/src/main/java/org/redisson/ConfigSupport.java +++ b/src/main/java/org/redisson/ConfigSupport.java @@ -15,8 +15,12 @@ */ package org.redisson; +import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; import java.net.URI; +import java.net.URL; import java.util.List; import org.redisson.client.codec.Codec; @@ -99,6 +103,22 @@ public class ConfigSupport { return mapper.readValue(content, Config.class); } + public Config fromJSON(File file) throws IOException { + return mapper.readValue(file, Config.class); + } + + public Config fromJSON(URL url) throws IOException { + return mapper.readValue(url, Config.class); + } + + public Config fromJSON(Reader reader) throws IOException { + return mapper.readValue(reader, Config.class); + } + + public Config fromJSON(InputStream inputStream) throws IOException { + return mapper.readValue(inputStream, Config.class); + } + public String toJSON(Config config) throws IOException { return mapper.writeValueAsString(config); } diff --git a/src/test/java/org/redisson/BaseTest.java b/src/test/java/org/redisson/BaseTest.java index 31586006c..8e210118e 100644 --- a/src/test/java/org/redisson/BaseTest.java +++ b/src/test/java/org/redisson/BaseTest.java @@ -3,6 +3,8 @@ package org.redisson; import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; +import org.redisson.client.codec.StringCodec; +import org.redisson.codec.MsgPackJacksonCodec; public abstract class BaseTest { @@ -24,6 +26,7 @@ public abstract class BaseTest { redisAddress = "127.0.0.1:6379"; } Config config = new Config(); + config.setCodec(new MsgPackJacksonCodec()); // config.useSentinelConnection().setMasterName("mymaster").addSentinelAddress("127.0.0.1:26379", "127.0.0.1:26389"); // config.useClusterServers().addNodeAddress("127.0.0.1:7004", "127.0.0.1:7001", "127.0.0.1:7000"); config.useSingleServer().setAddress(redisAddress);