Skip to content

Commit

Permalink
Parse null lists as empty lists instead
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakShearman committed Oct 1, 2023
1 parent e1f5d21 commit 37ffabc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package dev.emortal.api.liveconfigparser.adapter;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public final class NullListToEmptyFactory implements TypeAdapterFactory {
public static final NullListToEmptyFactory INSTANCE = new NullListToEmptyFactory();

private NullListToEmptyFactory() {
}

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<?> rawType = type.getRawType();

// Only handle List and ArrayList; let other factories handle different types
if (rawType != List.class && rawType != ArrayList.class) {
return null;
}

// Delegate which handles deserialization of non-null values, and serialization
TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
return new TypeAdapter<>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}

@Override
public T read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();

// Safe due to check at beginning of `create` method
@SuppressWarnings("unchecked")
T t = (T) new ArrayList<>();
return t;
} else {
return delegate.read(in);
}
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package dev.emortal.api.liveconfigparser.configs.common;

import java.util.List;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public record ConfigItem(@NotNull String material, int slot, @NotNull String name, @NotNull List<String> lore) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import dev.emortal.api.liveconfigparser.adapter.DurationAdapter;
import dev.emortal.api.liveconfigparser.adapter.NullListToEmptyFactory;
import dev.emortal.api.liveconfigparser.configs.ConfigCollection;
import dev.emortal.api.liveconfigparser.parser.ConfigParser;
import io.kubernetes.client.openapi.ApiClient;
Expand Down Expand Up @@ -41,6 +42,7 @@ private GameModeCollection(@NotNull Path localPath) throws IOException {
private static final class Parser implements ConfigParser<GameModeConfig> {
private static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(Duration.class, new DurationAdapter().nullSafe())
.registerTypeAdapterFactory(NullListToEmptyFactory.INSTANCE)
.create();

@Override
Expand Down

0 comments on commit 37ffabc

Please sign in to comment.