Skip to content

Commit

Permalink
fix(yaml): serialize Configuration to Map when the serializer is Conf…
Browse files Browse the repository at this point in the history
…igurationSerializer
  • Loading branch information
Siroshun09 committed Feb 22, 2023
1 parent 9430199 commit a163a54
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.github.siroshun09.configapi.api.Configuration;
import com.github.siroshun09.configapi.api.MappedConfiguration;
import com.github.siroshun09.configapi.api.file.AbstractFileConfiguration;
import com.github.siroshun09.configapi.api.serializer.ConfigurationSerializer;
import com.github.siroshun09.configapi.api.serializer.Serializer;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -186,6 +188,16 @@ public void set(@NotNull String path, @Nullable Object value) {
return config.getOrCreateSection(path);
}

@SuppressWarnings("unchecked")
@Override
public <T> void setList(@NotNull String path, @NotNull List<T> list, @NotNull Serializer<T, ?> serializer) {
if (serializer instanceof ConfigurationSerializer) {
serializer = new ToMap<>((ConfigurationSerializer<T>) serializer);
}

super.setList(path, list, serializer);
}

@Override
public void clear() {
if (config != null) {
Expand Down Expand Up @@ -288,4 +300,28 @@ public String toString() {
throw new IllegalStateException("this configuration is not loaded");
}
}

private static class ToMap<T> implements Serializer<T, Map<Object, Object>> {

private final ConfigurationSerializer<T> serializer;

private ToMap(@NotNull ConfigurationSerializer<T> serializer) {
this.serializer = serializer;
}

@Override
public @NotNull Map<Object, Object> serialize(@NotNull T input) {
return MappedConfiguration.convertToMap(serializer.serialize(input));
}

@SuppressWarnings("unchecked")
@Override
public @Nullable T deserialize(@NotNull Object source) {
if (source instanceof Map) {
return serializer.deserializeConfiguration(MappedConfiguration.create((Map<Object, Object>) source));
} else {
return null;
}
}
}
}

0 comments on commit a163a54

Please sign in to comment.