mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
feat(yaml): Finished YAML Provider
This commit is contained in:
+5
-6
@@ -28,7 +28,7 @@ public class JSONConfigFactory extends FileConfigFactory<JSONSource, Configurati
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONConfigFactory self() {
|
||||
protected JSONConfigFactory self() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -37,10 +37,6 @@ public class JSONConfigFactory extends FileConfigFactory<JSONSource, Configurati
|
||||
return self();
|
||||
}
|
||||
|
||||
public JSONConfigFactory gson(@NotNull Gson gson) {
|
||||
return gson(() -> gson);
|
||||
}
|
||||
|
||||
public JSONConfigFactory gson(@NotNull Consumer<GsonBuilder> builder) {
|
||||
return gson(() -> {
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
@@ -49,6 +45,9 @@ public class JSONConfigFactory extends FileConfigFactory<JSONSource, Configurati
|
||||
});
|
||||
}
|
||||
|
||||
public JSONConfigFactory gson(@NotNull Gson gson) {
|
||||
return gson(() -> gson);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigurationHolder<JSONSource> build() {
|
||||
@@ -59,7 +58,7 @@ public class JSONConfigFactory extends FileConfigFactory<JSONSource, Configurati
|
||||
String sourcePath = this.resourcePath;
|
||||
|
||||
return new ConfigurationHolder<JSONSource>(this.adapters, this.options, new ConcurrentHashMap<>(), this.initializer) {
|
||||
final JSONSource source = new JSONSource(this, 0, configFile, sourcePath, gson);
|
||||
final JSONSource source = new JSONSource(this, configFile, sourcePath, gson);
|
||||
|
||||
@Override
|
||||
public @NotNull JSONSource config() {
|
||||
|
||||
@@ -1,156 +0,0 @@
|
||||
package cc.carm.lib.configuration.source.json;
|
||||
|
||||
import cc.carm.lib.configuration.source.section.ConfigureSection;
|
||||
import cc.carm.lib.configuration.source.section.ConfigureSource;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class JSONSection implements ConfigureSection {
|
||||
|
||||
protected final @NotNull ConfigureSource<? extends JSONSection, ?, ?> source;
|
||||
protected final @NotNull Map<String, Object> data;
|
||||
protected final @Nullable JSONSection parent;
|
||||
|
||||
public JSONSection(@NotNull ConfigureSource<? extends JSONSection, ?, ?> source,
|
||||
@NotNull Map<?, ?> data, @Nullable JSONSection parent) {
|
||||
this.source = source;
|
||||
this.parent = parent;
|
||||
this.data = new LinkedHashMap<>();
|
||||
|
||||
for (Map.Entry<?, ?> entry : data.entrySet()) {
|
||||
String key = (entry.getKey() == null) ? "null" : entry.getKey().toString();
|
||||
|
||||
if (entry.getValue() instanceof Map) {
|
||||
this.data.put(key, new JSONSection(source, (Map<?, ?>) entry.getValue(), this));
|
||||
} else if (entry.getValue() instanceof List) {
|
||||
List<Object> list = new ArrayList<>();
|
||||
for (Object obj : (List<?>) entry.getValue()) {
|
||||
if (obj instanceof Map) {
|
||||
list.add(new JSONSection(source, (Map<?, ?>) obj, this));
|
||||
} else {
|
||||
list.add(obj);
|
||||
}
|
||||
}
|
||||
this.data.put(key, list);
|
||||
} else {
|
||||
this.data.put(key, entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigureSource<? extends JSONSection, ?, ?> source() {
|
||||
return this.source;
|
||||
}
|
||||
|
||||
public @NotNull Map<String, Object> data() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
public @Nullable JSONSection parent() {
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> getValues(boolean deep) {
|
||||
if (deep) {
|
||||
Map<String, Object> values = new LinkedHashMap<>();
|
||||
mapChildrenValues(values, this, null, true);
|
||||
return Collections.unmodifiableMap(values);
|
||||
} else return Collections.unmodifiableMap(data());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(@NotNull String path, @Nullable Object value) {
|
||||
if (value instanceof Map) {
|
||||
value = new JSONSection(source(), (Map<?, ?>) value, this);
|
||||
}
|
||||
|
||||
JSONSection section = getSectionFor(path);
|
||||
if (section == this) {
|
||||
if (value == null) {
|
||||
this.data.remove(path);
|
||||
} else {
|
||||
this.data.put(path, value);
|
||||
}
|
||||
} else {
|
||||
section.set(getChild(path), value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(@NotNull String path) {
|
||||
return get(path) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Object get(@NotNull String path) {
|
||||
JSONSection section = getSectionFor(path);
|
||||
return section == this ? data.get(path) : section.get(getChild(path));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isList(@NotNull String path) {
|
||||
return get(path) instanceof List<?>;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<?> getList(@NotNull String path) {
|
||||
Object val = get(path);
|
||||
return (val instanceof List<?>) ? (List<?>) val : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSection(@NotNull String path) {
|
||||
return get(path) instanceof JSONSection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ConfigureSection getSection(@NotNull String path) {
|
||||
Object val = get(path);
|
||||
return (val instanceof ConfigureSection) ? (ConfigureSection) val : null;
|
||||
}
|
||||
|
||||
private JSONSection getSectionFor(String path) {
|
||||
int index = path.indexOf(separator());
|
||||
if (index == -1) return this;
|
||||
|
||||
String root = path.substring(0, index);
|
||||
Object section = this.data.get(root);
|
||||
if (section == null) {
|
||||
section = new JSONSection(source(), new LinkedHashMap<>(), this);
|
||||
this.data.put(root, section);
|
||||
}
|
||||
|
||||
return (JSONSection) section;
|
||||
}
|
||||
|
||||
private String getChild(String path) {
|
||||
int index = path.indexOf(separator());
|
||||
return (index == -1) ? path : path.substring(index + 1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Map the values of the children of the section to the output map.
|
||||
*
|
||||
* @param output The map to map the values to
|
||||
* @param section The section to map the values from
|
||||
* @param parent The parent path
|
||||
* @param deep If the mapping should be deep
|
||||
* @author md_5, sk89q
|
||||
*/
|
||||
protected void mapChildrenValues(@NotNull Map<String, Object> output, @NotNull JSONSection section,
|
||||
@Nullable String parent, boolean deep) {
|
||||
for (Map.Entry<String, Object> entry : section.data().entrySet()) {
|
||||
String path = (parent == null ? "" : parent + separator()) + entry.getKey();
|
||||
output.remove(path);
|
||||
output.put(path, entry.getValue());
|
||||
if (deep && entry.getValue() instanceof JSONSection) {
|
||||
this.mapChildrenValues(output, (JSONSection) entry.getValue(), path, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+22
-21
@@ -2,36 +2,38 @@ package cc.carm.lib.configuration.source.json;
|
||||
|
||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import cc.carm.lib.configuration.source.file.FileConfigSource;
|
||||
import cc.carm.lib.configuration.source.section.ConfigureSource;
|
||||
import cc.carm.lib.configuration.source.section.MemorySection;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.io.File;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class JSONSource extends FileConfigSource<JSONSection, Map<?, ?>, JSONSource> {
|
||||
public class JSONSource extends FileConfigSource<MemorySection, Map<?, ?>, JSONSource> {
|
||||
|
||||
public static final @NotNull Gson DEFAULT_GSON = new GsonBuilder()
|
||||
.serializeNulls().disableHtmlEscaping().setPrettyPrinting()
|
||||
.registerTypeAdapter(
|
||||
JSONSection.class,
|
||||
(JsonSerializer<JSONSection>) (src, typeOfSrc, context) -> context.serialize(src.data)
|
||||
MemorySection.class,
|
||||
(JsonSerializer<MemorySection>) (src, t, c) -> c.serialize(src.data())
|
||||
).create();
|
||||
|
||||
protected final @NotNull Gson gson;
|
||||
protected @Nullable JSONSection rootSection;
|
||||
protected @Nullable MemorySection rootSection;
|
||||
|
||||
protected JSONSource(@NotNull ConfigurationHolder<? extends JSONSource> holder, long lastUpdateMillis,
|
||||
protected JSONSource(@NotNull ConfigurationHolder<? extends JSONSource> holder,
|
||||
@NotNull File file, @Nullable String resourcePath) {
|
||||
this(holder, lastUpdateMillis, file, resourcePath, DEFAULT_GSON);
|
||||
this(holder, file, resourcePath, DEFAULT_GSON);
|
||||
}
|
||||
|
||||
protected JSONSource(@NotNull ConfigurationHolder<? extends JSONSource> holder, long lastUpdateMillis,
|
||||
protected JSONSource(@NotNull ConfigurationHolder<? extends JSONSource> holder,
|
||||
@NotNull File file, @Nullable String resourcePath, @NotNull Gson gson) {
|
||||
super(holder, lastUpdateMillis, file, resourcePath);
|
||||
super(holder, 0, file, resourcePath);
|
||||
this.gson = gson;
|
||||
initialize();
|
||||
}
|
||||
@@ -50,19 +52,14 @@ public class JSONSource extends FileConfigSource<JSONSection, Map<?, ?>, JSONSou
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigureSource<?, ?, ?> source() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<?, ?> original() {
|
||||
return section().data();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull JSONSection section() {
|
||||
return Objects.requireNonNull(this.rootSection, "Section is not initialized");
|
||||
public @NotNull MemorySection section() {
|
||||
return Objects.requireNonNull(this.rootSection, "Root section is not initialized");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,11 +67,15 @@ public class JSONSource extends FileConfigSource<JSONSection, Map<?, ?>, JSONSou
|
||||
fileWriter(writer -> gson.toJson(original(), writer));
|
||||
}
|
||||
|
||||
public @NotNull String saveToString() {
|
||||
return gson.toJson(original());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onReload() throws Exception {
|
||||
Map<?, ?> data = fileReader(reader -> gson.fromJson(reader, LinkedHashMap.class));
|
||||
if (data == null) data = new LinkedHashMap<>();
|
||||
this.rootSection = new JSONSection(this, data, null);
|
||||
this.rootSection = MemorySection.root(
|
||||
this, fileReader(reader -> gson.fromJson(reader, LinkedHashMap.class))
|
||||
);
|
||||
this.lastUpdateMillis = System.currentTimeMillis(); // 更新时间
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user