mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
feat(json): Finished json version's provider
This commit is contained in:
+2
-2
@@ -1,7 +1,7 @@
|
||||
package cc.carm.lib.configuration.json;
|
||||
package cc.carm.lib.configuration.source.json;
|
||||
|
||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import cc.carm.lib.configuration.source.FileConfigFactory;
|
||||
import cc.carm.lib.configuration.source.file.FileConfigFactory;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
+24
-14
@@ -1,4 +1,4 @@
|
||||
package cc.carm.lib.configuration.json;
|
||||
package cc.carm.lib.configuration.source.json;
|
||||
|
||||
import cc.carm.lib.configuration.source.section.ConfigureSection;
|
||||
import cc.carm.lib.configuration.source.section.ConfigureSource;
|
||||
@@ -9,24 +9,26 @@ import java.util.*;
|
||||
|
||||
public class JSONSection implements ConfigureSection {
|
||||
|
||||
protected final ConfigureSource<? extends JSONSection, ?, ?> source;
|
||||
protected final Map<String, Object> data;
|
||||
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) {
|
||||
@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.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));
|
||||
list.add(new JSONSection(source, (Map<?, ?>) obj, this));
|
||||
} else {
|
||||
list.add(obj);
|
||||
}
|
||||
@@ -43,13 +45,12 @@ public class JSONSection implements ConfigureSection {
|
||||
return this.source;
|
||||
}
|
||||
|
||||
public Map<String, Object> data() {
|
||||
public @NotNull Map<String, Object> data() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<String> getKeys(boolean deep) {
|
||||
return getValues(deep).keySet();
|
||||
public @Nullable JSONSection parent() {
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,14 +58,14 @@ public class JSONSection implements ConfigureSection {
|
||||
if (deep) {
|
||||
Map<String, Object> values = new LinkedHashMap<>();
|
||||
mapChildrenValues(values, this, null, true);
|
||||
return values;
|
||||
} else return new LinkedHashMap<>(this.data);
|
||||
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);
|
||||
value = new JSONSection(source(), (Map<?, ?>) value, this);
|
||||
}
|
||||
|
||||
JSONSection section = getSectionFor(path);
|
||||
@@ -119,7 +120,7 @@ public class JSONSection implements ConfigureSection {
|
||||
String root = path.substring(0, index);
|
||||
Object section = this.data.get(root);
|
||||
if (section == null) {
|
||||
section = new JSONSection(source(), new LinkedHashMap<>());
|
||||
section = new JSONSection(source(), new LinkedHashMap<>(), this);
|
||||
this.data.put(root, section);
|
||||
}
|
||||
|
||||
@@ -132,6 +133,15 @@ public class JSONSection implements ConfigureSection {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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()) {
|
||||
+5
-5
@@ -1,7 +1,7 @@
|
||||
package cc.carm.lib.configuration.json;
|
||||
package cc.carm.lib.configuration.source.json;
|
||||
|
||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import cc.carm.lib.configuration.source.FileConfigSource;
|
||||
import cc.carm.lib.configuration.source.file.FileConfigSource;
|
||||
import cc.carm.lib.configuration.source.section.ConfigureSource;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
@@ -22,7 +22,7 @@ public class JSONSource extends FileConfigSource<JSONSection, Map<?, ?>, JSONSou
|
||||
).create();
|
||||
|
||||
protected final @NotNull Gson gson;
|
||||
protected @Nullable JSONSection section;
|
||||
protected @Nullable JSONSection rootSection;
|
||||
|
||||
protected JSONSource(@NotNull ConfigurationHolder<? extends JSONSource> holder, long lastUpdateMillis,
|
||||
@NotNull File file, @Nullable String resourcePath) {
|
||||
@@ -62,7 +62,7 @@ public class JSONSource extends FileConfigSource<JSONSection, Map<?, ?>, JSONSou
|
||||
|
||||
@Override
|
||||
public @NotNull JSONSection section() {
|
||||
return Objects.requireNonNull(this.section, "Section is not initialized");
|
||||
return Objects.requireNonNull(this.rootSection, "Section is not initialized");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -74,7 +74,7 @@ public class JSONSource extends FileConfigSource<JSONSection, Map<?, ?>, JSONSou
|
||||
protected void onReload() throws Exception {
|
||||
Map<?, ?> data = fileReader(reader -> gson.fromJson(reader, LinkedHashMap.class));
|
||||
if (data == null) data = new LinkedHashMap<>();
|
||||
this.section = new JSONSection(this, data);
|
||||
this.rootSection = new JSONSection(this, data, null);
|
||||
this.lastUpdateMillis = System.currentTimeMillis(); // 更新时间
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
package config;
|
||||
|
||||
import cc.carm.lib.configuration.demo.tests.ConfigurationTest;
|
||||
import cc.carm.lib.configuration.json.JSONConfigFactory;
|
||||
import cc.carm.lib.configuration.source.json.JSONConfigFactory;
|
||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import cc.carm.lib.configuration.source.loader.PathGenerator;
|
||||
import cc.carm.lib.configuration.source.option.StandardOptions;
|
||||
import cc.carm.lib.configuration.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredValue;
|
||||
import org.junit.Test;
|
||||
|
||||
Reference in New Issue
Block a user