From ff22f74fdf50143e94b9ae97031f520dd50e4c72 Mon Sep 17 00:00:00 2001 From: paulevsGitch Date: Sun, 6 Dec 2020 21:21:00 +0300 Subject: [PATCH] JSON fixes --- src/main/java/ru/betterend/config/Config.java | 18 +++------ .../java/ru/betterend/util/JsonFactory.java | 39 ++++++++++--------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/main/java/ru/betterend/config/Config.java b/src/main/java/ru/betterend/config/Config.java index a7c3bf1c..1766bb2f 100644 --- a/src/main/java/ru/betterend/config/Config.java +++ b/src/main/java/ru/betterend/config/Config.java @@ -36,10 +36,8 @@ public abstract class Config { if (str == null) { StringEntry entry = this.configKeeper.registerEntry(key, new StringEntry(defaultValue)); if (settings != null) { - if (settings != null) { - this.configKeeper.loadFromJson(settings, key, entry); - return entry.getValue(); - } + this.configKeeper.loadFromJson(settings, key, entry); + return entry.getValue(); } } return str != null ? str : defaultValue; @@ -67,10 +65,8 @@ public abstract class Config { if (val == null) { IntegerEntry entry = this.configKeeper.registerEntry(key, new IntegerEntry(defaultValue)); if (settings != null) { - if (settings != null) { - this.configKeeper.loadFromJson(settings, key, entry); - return entry.getValue(); - } + this.configKeeper.loadFromJson(settings, key, entry); + return entry.getValue(); } } return val != null ? val : defaultValue; @@ -110,10 +106,8 @@ public abstract class Config { if (val == null) { FloatEntry entry = this.configKeeper.registerEntry(key, new FloatEntry(defaultValue)); if (settings != null) { - if (settings != null) { - this.configKeeper.loadFromJson(settings, key, entry); - return entry.getValue(); - } + this.configKeeper.loadFromJson(settings, key, entry); + return entry.getValue(); } } return val != null ? val : defaultValue; diff --git a/src/main/java/ru/betterend/util/JsonFactory.java b/src/main/java/ru/betterend/util/JsonFactory.java index 1b51e1c2..de5cce43 100644 --- a/src/main/java/ru/betterend/util/JsonFactory.java +++ b/src/main/java/ru/betterend/util/JsonFactory.java @@ -20,6 +20,8 @@ public class JsonFactory { public final static Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + // Unused + @Deprecated public static JsonObject getJsonObject(String path) throws IOException { try (InputStream is = JsonFactory.class.getResourceAsStream(path)) { Reader reader = new InputStreamReader(is); @@ -31,36 +33,38 @@ public class JsonFactory { } } + // Unused + @Deprecated public static JsonObject getJsonObject(Resource jsonSource) { if (jsonSource != null) { try (InputStream is = jsonSource.getInputStream()) { Reader reader = new InputStreamReader(is); - JsonObject jsonObject = loadJson(reader).getAsJsonObject(); - if (jsonObject == null) { - return new JsonObject(); + JsonElement json = loadJson(reader); + if (json != null && json.isJsonObject()) { + JsonObject jsonObject = json.getAsJsonObject(); + return jsonObject != null ? jsonObject : new JsonObject(); } - return jsonObject; - } catch (IOException ex) { + } + catch (IOException ex) { BetterEnd.LOGGER.catching(ex); - return new JsonObject(); } } - return new JsonObject(); } - + public static JsonObject getJsonObject(InputStream stream) { try { Reader reader = new InputStreamReader(stream); - JsonObject jsonObject = loadJson(reader).getAsJsonObject(); - if (jsonObject == null) { - return new JsonObject(); + JsonElement json = loadJson(reader); + if (json != null && json.isJsonObject()) { + JsonObject jsonObject = json.getAsJsonObject(); + return jsonObject != null ? jsonObject : new JsonObject(); } - return jsonObject; - } catch (Exception ex) { - BetterEnd.LOGGER.catching(ex); - return new JsonObject(); } + catch (Exception ex) { + BetterEnd.LOGGER.catching(ex); + } + return new JsonObject(); } public static JsonObject getJsonObject(File jsonFile) { @@ -68,10 +72,7 @@ public class JsonFactory { JsonElement json = loadJson(jsonFile); if (json != null && json.isJsonObject()) { JsonObject jsonObject = json.getAsJsonObject(); - if (jsonObject == null) { - return new JsonObject(); - } - return jsonObject; + return jsonObject != null ? jsonObject : new JsonObject(); } }