Skip to content

Commit 7a845fa

Browse files
committed
Now use ConfigSerializer/Deserializer
1 parent e32d57e commit 7a845fa

File tree

9 files changed

+117
-75
lines changed

9 files changed

+117
-75
lines changed

src/main/java/org/visuals/legacy/lightconfig/lib/v1/Config.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
package org.visuals.legacy.lightconfig.lib.v1;
2626

27-
import com.google.gson.JsonElement;
2827
import com.google.gson.JsonObject;
2928
import net.fabricmc.loader.api.ModContainer;
3029
import net.minecraft.client.gui.screens.Screen;
@@ -47,21 +46,22 @@ public abstract class Config {
4746
protected final List<AbstractConfigField<?>> configFields = new ArrayList<>();
4847
protected final ModContainer modContainer;
4948
protected final Path path;
50-
protected final ConfigSerializer<?> serializer;
51-
protected final ConfigDeserializer<?> deserializer;
49+
protected final Json.Serializer serializer;
50+
protected final Json.Deserializer deserializer;
5251

5352
public Config(ModContainer modContainer, Path path, ConfigSerializer<?> serializer, ConfigDeserializer<?> deserializer) {
5453
this.modContainer = modContainer;
5554
this.path = path;
56-
this.serializer = serializer;
57-
this.deserializer = deserializer;
58-
if (this.serializer != Json.SERIALIZER || this.deserializer != Json.DESERIALIZER) {
55+
if (!(serializer instanceof Json.Serializer && deserializer instanceof Json.Deserializer)) {
5956
throw new RuntimeException("Only json serialization is currently supported! Please use Json.SERIALIZER/Json.DESERIALIZER!");
6057
}
58+
59+
this.serializer = (Json.Serializer) serializer;
60+
this.deserializer = (Json.Deserializer) deserializer;
6161
}
6262

6363
public Config(ModContainer modContainer, Path path) {
64-
this(modContainer, path, Json.SERIALIZER, Json.DESERIALIZER);
64+
this(modContainer, path, new Json.Serializer(), new Json.Deserializer());
6565
}
6666

6767
public BooleanConfigField booleanFieldOf(final String name, final boolean defaultValue) {
@@ -98,17 +98,18 @@ public void load() {
9898
boolean success = true;
9999
try {
100100
final String json = Files.readString(this.path);
101-
final JsonObject object = ((JsonElement) this.deserializer.deserialize(json)).getAsJsonObject();
101+
final JsonObject object = this.deserializer.deserialize(json).getAsJsonObject();
102102
if (object == null) {
103103
this.logger.warn("Failed to load config! Defaulting to original settings.");
104104
success = false;
105105
} else {
106+
this.deserializer.withObject(object);
106107
this.configFields.forEach(field -> {
107108
try {
108109
if (!object.has(field.getName())) {
109110
throw new Exception("Failed to load value for '" + field.getName() + "', object didn't contain a value for it.");
110111
} else {
111-
field.load(object);
112+
field.load(this.deserializer);
112113
}
113114
} catch (Exception exception) {
114115
this.logger.warn(exception.getMessage());
@@ -127,16 +128,17 @@ public void load() {
127128

128129
public void save() {
129130
final JsonObject object = new JsonObject();
131+
this.serializer.withObject(object);
130132
this.configFields.forEach(field -> {
131133
try {
132-
field.save(object);
134+
field.save(this.serializer);
133135
} catch (Exception ignored) {
134136
this.logger.warn("Failed to save config field '{}'!", field.getName());
135137
}
136138
});
137139

138140
try {
139-
Files.write(this.path, ((ConfigSerializer<JsonElement>) this.serializer).serialize(object));
141+
Files.write(this.path, this.serializer.serialize(object));
140142
} catch (Exception ignored) {
141143
this.logger.warn("Failed to save config!");
142144
return;

src/main/java/org/visuals/legacy/lightconfig/lib/v1/field/AbstractConfigField.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import com.google.gson.JsonObject;
2828
import net.minecraft.client.gui.components.AbstractWidget;
2929
import org.visuals.legacy.lightconfig.lib.v1.Config;
30+
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigDeserializer;
31+
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigSerializer;
3032

3133
public abstract class AbstractConfigField<T> {
3234
protected final Config config;
@@ -41,10 +43,9 @@ public AbstractConfigField(final Config config, final String name, final T defau
4143
this.defaultValue = defaultValue;
4244
}
4345

44-
// TODO/Swap w/ serializer/deserializer
45-
public abstract void load(JsonObject object) throws Exception;
46+
public abstract void load(ConfigDeserializer<?> deserializer) throws Exception;
4647

47-
public abstract void save(JsonObject object) throws Exception;
48+
public abstract void save(ConfigSerializer<?> serializer) throws Exception;
4849

4950
public abstract AbstractWidget createWidget();
5051

src/main/java/org/visuals/legacy/lightconfig/lib/v1/field/BooleanConfigField.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424

2525
package org.visuals.legacy.lightconfig.lib.v1.field;
2626

27-
import com.google.gson.JsonObject;
2827
import net.minecraft.client.gui.components.Button;
2928
import net.minecraft.client.gui.components.Tooltip;
3029
import net.minecraft.network.chat.Component;
3130
import org.visuals.legacy.lightconfig.lib.v1.Config;
3231
import org.visuals.legacy.lightconfig.lib.v1.Translations;
32+
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigDeserializer;
33+
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigSerializer;
3334
import org.visuals.legacy.lightconfig.lib.v1.type.Types;
3435

3536
import java.util.function.Function;
@@ -40,8 +41,8 @@ public BooleanConfigField(final Config config, final String name, final boolean
4041
}
4142

4243
@Override
43-
public void load(JsonObject object) throws Exception {
44-
final Boolean value = Types.BOOLEAN_TYPE.read(object, this.name);
44+
public void load(ConfigDeserializer<?> deserializer) throws Exception {
45+
final Boolean value = Types.BOOLEAN_TYPE.read(deserializer, this.name);
4546
if (value == null) {
4647
throw new Exception("Failed to load value for '" + this.name + "'");
4748
} else {
@@ -50,8 +51,8 @@ public void load(JsonObject object) throws Exception {
5051
}
5152

5253
@Override
53-
public void save(JsonObject object) {
54-
Types.BOOLEAN_TYPE.write(object, this.name, this.value);
54+
public void save(ConfigSerializer<?> serializer) {
55+
Types.BOOLEAN_TYPE.write(serializer, this.name, this.value);
5556
}
5657

5758
@Override

src/main/java/org/visuals/legacy/lightconfig/lib/v1/field/EnumConfigField.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
package org.visuals.legacy.lightconfig.lib.v1.field;
2626

2727
import com.google.gson.JsonElement;
28-
import com.google.gson.JsonObject;
2928
import com.google.gson.JsonPrimitive;
3029
import net.minecraft.client.gui.components.AbstractWidget;
3130
import net.minecraft.client.gui.components.Button;
@@ -36,18 +35,18 @@
3635
import org.visuals.legacy.lightconfig.lib.v1.Translations;
3736
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigDeserializer;
3837
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigSerializer;
38+
import org.visuals.legacy.lightconfig.lib.v1.serialization.Json;
3939
import org.visuals.legacy.lightconfig.lib.v1.type.Type;
4040

4141
import java.util.Arrays;
42-
import java.util.Optional;
4342

4443
public class EnumConfigField<T extends Enum<T>> extends AbstractConfigField<T> {
4544
private final Class<T> enumClazz;
4645

4746
private final Type<T> ENUM_TYPE = new Type<>() {
4847
@Override
49-
public @Nullable T read(JsonObject object, String name) {
50-
final JsonElement element = object.get(name);
48+
public @Nullable T read(ConfigDeserializer<?> deserializer, String name) {
49+
final JsonElement element = ((Json.Deserializer) deserializer).object().get(name);
5150
if (element == null || !element.isJsonPrimitive() || (element instanceof final JsonPrimitive primitive && !primitive.isString())) {
5251
return null;
5352
} else {
@@ -59,8 +58,8 @@ public class EnumConfigField<T extends Enum<T>> extends AbstractConfigField<T> {
5958
}
6059

6160
@Override
62-
public void write(JsonObject object, String name, T value) {
63-
object.addProperty(name, value.name());
61+
public void write(ConfigSerializer<?> serializer, String name, T value) {
62+
((Json.Serializer) serializer).object().addProperty(name, value.name());
6463
}
6564
};
6665

@@ -70,8 +69,8 @@ public EnumConfigField(Config config, String name, T defaultValue, Class<T> claz
7069
}
7170

7271
@Override
73-
public void load(JsonObject object) throws Exception {
74-
final T value = ENUM_TYPE.read(object, this.name);
72+
public void load(ConfigDeserializer<?> deserializer) throws Exception {
73+
final T value = ENUM_TYPE.read(deserializer, this.name);
7574
if (value == null) {
7675
throw new Exception("Failed to load value for '" + this.name + "'");
7776
} else {
@@ -80,8 +79,8 @@ public void load(JsonObject object) throws Exception {
8079
}
8180

8281
@Override
83-
public void save(JsonObject object) {
84-
ENUM_TYPE.write(object, this.name, this.value);
82+
public void save(ConfigSerializer<?> serializer) {
83+
ENUM_TYPE.write(serializer, this.name, this.value);
8584
}
8685

8786
@Override

src/main/java/org/visuals/legacy/lightconfig/lib/v1/field/NumericConfigField.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424

2525
package org.visuals.legacy.lightconfig.lib.v1.field;
2626

27-
import com.google.gson.JsonObject;
2827
import net.minecraft.client.gui.components.AbstractWidget;
2928
import org.visuals.legacy.lightconfig.lib.v1.Config;
29+
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigDeserializer;
30+
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigSerializer;
3031
import org.visuals.legacy.lightconfig.lib.v1.type.Type;
3132

3233
public class NumericConfigField<T extends Number> extends AbstractConfigField<T> {
@@ -38,8 +39,8 @@ public NumericConfigField(final Config config, final Type<T> type, final String
3839
}
3940

4041
@Override
41-
public void load(JsonObject object) throws Exception {
42-
final T value = this.type.read(object, this.name);
42+
public void load(ConfigDeserializer<?> deserializer) throws Exception {
43+
final T value = this.type.read(deserializer, this.name);
4344
if (value == null) {
4445
throw new Exception("Failed to load value for '" + this.name + "'");
4546
} else {
@@ -48,8 +49,8 @@ public void load(JsonObject object) throws Exception {
4849
}
4950

5051
@Override
51-
public void save(JsonObject object) {
52-
this.type.write(object, this.name, this.value);
52+
public void save(ConfigSerializer<?> serializer) {
53+
this.type.write(serializer, this.name, this.value);
5354
}
5455

5556
@Override

src/main/java/org/visuals/legacy/lightconfig/lib/v1/field/StringConfigField.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424

2525
package org.visuals.legacy.lightconfig.lib.v1.field;
2626

27-
import com.google.gson.JsonObject;
2827
import net.minecraft.client.gui.components.AbstractWidget;
2928
import org.visuals.legacy.lightconfig.lib.v1.Config;
29+
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigDeserializer;
30+
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigSerializer;
3031
import org.visuals.legacy.lightconfig.lib.v1.type.Types;
3132

3233
public class StringConfigField extends AbstractConfigField<String> {
@@ -35,8 +36,8 @@ public StringConfigField(final Config config, final String name, final String de
3536
}
3637

3738
@Override
38-
public void load(JsonObject object) throws Exception {
39-
final String value = Types.STRING_TYPE.read(object, this.name);
39+
public void load(ConfigDeserializer<?> deserializer) throws Exception {
40+
final String value = Types.STRING_TYPE.read(deserializer, this.name);
4041
if (value == null) {
4142
throw new Exception("Failed to load value for '" + this.name + "'");
4243
} else {
@@ -45,8 +46,8 @@ public void load(JsonObject object) throws Exception {
4546
}
4647

4748
@Override
48-
public void save(JsonObject object) {
49-
Types.STRING_TYPE.write(object, this.name, this.value);
49+
public void save(ConfigSerializer<?> serializer) {
50+
Types.STRING_TYPE.write(serializer, this.name, this.value);
5051
}
5152

5253
@Override

src/main/java/org/visuals/legacy/lightconfig/lib/v1/serialization/Json.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,19 @@
2424

2525
package org.visuals.legacy.lightconfig.lib.v1.serialization;
2626

27-
import com.google.gson.Gson;
28-
import com.google.gson.GsonBuilder;
29-
import com.google.gson.JsonElement;
30-
import com.google.gson.Strictness;
27+
import com.google.gson.*;
3128
import org.jetbrains.annotations.Nullable;
3229

3330
public class Json {
3431
protected static final Gson GSON = new GsonBuilder().setPrettyPrinting().setStrictness(Strictness.LENIENT).create();
3532

36-
public static final ConfigSerializer<JsonElement> SERIALIZER = new ConfigSerializer<>() {
33+
public static class Serializer extends ConfigSerializer<JsonElement> {
34+
private JsonObject object = new JsonObject();
35+
36+
public void withObject(JsonObject object) {
37+
this.object = object;
38+
}
39+
3740
@Override
3841
public byte[] serialize(JsonElement value) {
3942
if (value == null) {
@@ -42,9 +45,19 @@ public byte[] serialize(JsonElement value) {
4245
return GSON.toJson(value).getBytes();
4346
}
4447
}
45-
};
4648

47-
public static final ConfigDeserializer<JsonElement> DESERIALIZER = new ConfigDeserializer<>() {
49+
public JsonObject object() {
50+
return this.object;
51+
}
52+
}
53+
54+
public static class Deserializer extends ConfigDeserializer<JsonElement> {
55+
private JsonObject object = new JsonObject();
56+
57+
public void withObject(JsonObject object) {
58+
this.object = object;
59+
}
60+
4861
@Override
4962
public @Nullable JsonElement deserialize(Object value) {
5063
if (!(value instanceof String string)) {
@@ -53,5 +66,9 @@ public byte[] serialize(JsonElement value) {
5366
return GSON.fromJson(string, JsonElement.class);
5467
}
5568
}
56-
};
69+
70+
public JsonObject object() {
71+
return this.object;
72+
}
73+
}
5774
}

src/main/java/org/visuals/legacy/lightconfig/lib/v1/type/Type.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424

2525
package org.visuals.legacy.lightconfig.lib.v1.type;
2626

27-
import com.google.gson.JsonObject;
2827
import org.jetbrains.annotations.Nullable;
28+
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigDeserializer;
29+
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigSerializer;
2930

3031
public abstract class Type<T> {
31-
public abstract @Nullable T read(JsonObject object, String name);
32+
public abstract @Nullable T read(ConfigDeserializer<?> deserializer, String name);
3233

33-
public abstract void write(JsonObject object, String name, T value);
34+
public abstract void write(ConfigSerializer<?> serializer, String name, T value);
3435
}

0 commit comments

Comments
 (0)