Skip to content

Commit

Permalink
Use local type adapter for serialize node configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
HttpMarco committed Jan 3, 2025
1 parent a48c9d8 commit 7294084
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package dev.httpmarco.polocloud.common.configuration;
package dev.httpmarco.polocloud.node.utils.configuration;

import com.google.gson.Gson;
import dev.httpmarco.polocloud.common.gson.GsonPool;
import dev.httpmarco.polocloud.node.i18n.serializer.LocalSerializer;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.experimental.Accessors;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;

@Accessors(fluent = true)
public class Configuration<T> {

private static final Gson CONFIGURATION_GSON = GsonPool.newInstance(it -> it.registerTypeAdapter(Locale.class, new LocalSerializer()));
private final Path target;

@Getter
Expand All @@ -26,14 +30,14 @@ public Configuration(String target, T defaultValue) {

@SneakyThrows
public void update() {
Files.writeString(target, GsonPool.PRETTY_GSON.toJson(value));
Files.writeString(target, CONFIGURATION_GSON.toJson(value));
}

@SneakyThrows
@SuppressWarnings("unchecked")
public T read() {
if (Files.exists(target)) {
this.value = (T) GsonPool.PRETTY_GSON.fromJson(Files.readString(target), value.getClass());
this.value = (T) CONFIGURATION_GSON.fromJson(Files.readString(target), value.getClass());
} else {
this.update();
}
Expand Down

0 comments on commit 7294084

Please sign in to comment.