|
1 | 1 | package net.discordjug.javabot.data.config; |
2 | 2 |
|
3 | | -import com.google.gson.Gson; |
4 | | -import com.google.gson.GsonBuilder; |
5 | 3 | import com.google.gson.JsonSyntaxException; |
6 | 4 | import lombok.Data; |
7 | 5 | import lombok.extern.slf4j.Slf4j; |
8 | 6 | import net.discordjug.javabot.data.config.guild.*; |
9 | 7 | import net.discordjug.javabot.util.ExceptionLogger; |
| 8 | +import net.discordjug.javabot.util.GsonUtils; |
10 | 9 | import net.discordjug.javabot.util.Pair; |
11 | 10 | import net.dv8tion.jda.api.entities.Guild; |
12 | 11 |
|
|
20 | 19 | import java.nio.file.Path; |
21 | 20 | import java.util.List; |
22 | 21 | import java.util.Optional; |
23 | | -import java.util.regex.Pattern; |
24 | 22 |
|
25 | 23 | /** |
26 | 24 | * A collection of guild-specific configuration items, each of which represents |
@@ -71,13 +69,10 @@ public GuildConfig(Guild guild, Path file) { |
71 | 69 | * @throws UncheckedIOException if an IO error occurs. |
72 | 70 | */ |
73 | 71 | public static GuildConfig loadOrCreate(Guild guild, Path file) { |
74 | | - Gson gson = new GsonBuilder() |
75 | | - .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) |
76 | | - .create(); |
77 | 72 | GuildConfig config; |
78 | 73 | if (Files.exists(file)) { |
79 | 74 | try (BufferedReader reader = Files.newBufferedReader(file)) { |
80 | | - config = gson.fromJson(reader, GuildConfig.class); |
| 75 | + config = GsonUtils.fromJson(reader, GuildConfig.class); |
81 | 76 | config.setFile(file); |
82 | 77 | config.setGuild(guild); |
83 | 78 | log.info("Loaded config from {}", file); |
@@ -118,13 +113,8 @@ private void setGuild(Guild guild) { |
118 | 113 | * Saves this config to its file path. |
119 | 114 | */ |
120 | 115 | public synchronized void flush() { |
121 | | - Gson gson = new GsonBuilder() |
122 | | - .serializeNulls() |
123 | | - .setPrettyPrinting() |
124 | | - .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) |
125 | | - .create(); |
126 | 116 | try (BufferedWriter writer = Files.newBufferedWriter(this.file)) { |
127 | | - gson.toJson(this, writer); |
| 117 | + GsonUtils.toJson(this, writer); |
128 | 118 | writer.flush(); |
129 | 119 | } catch (IOException e) { |
130 | 120 | ExceptionLogger.capture(e, getClass().getSimpleName()); |
@@ -167,9 +157,9 @@ public void set(String propertyName, String value) throws UnknownPropertyExcepti |
167 | 157 | Optional<Pair<Field, Object>> result = ReflectionUtils.resolveField(propertyName, this); |
168 | 158 | result.ifPresent(pair -> { |
169 | 159 | try { |
170 | | - Field updatedField = ReflectionUtils.set(pair.first(), pair.second(), value); |
171 | | - if (updatedField.get(pair.second()) instanceof GuildConfigItem) { |
172 | | - ((GuildConfigItem) updatedField.get(pair.second())).setGuildConfig(this); |
| 160 | + Object updatedField = ReflectionUtils.set(pair.first(), pair.second(), value); |
| 161 | + if (updatedField instanceof GuildConfigItem item) { |
| 162 | + item.setGuildConfig(this); |
173 | 163 | } |
174 | 164 | this.flush(); |
175 | 165 | } catch (IllegalAccessException e) { |
|
0 commit comments