-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse null lists as empty lists instead
- Loading branch information
1 parent
e1f5d21
commit 37ffabc
Showing
3 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
java/src/main/java/dev/emortal/api/liveconfigparser/adapter/NullListToEmptyFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
}; | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
java/src/main/java/dev/emortal/api/liveconfigparser/configs/common/ConfigItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters