Update some dependencies + Gson now natively supports record
This commit is contained in:
parent
8c25fb0dd1
commit
7c4fd78680
@ -26,17 +26,17 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.kyori</groupId>
|
<groupId>net.kyori</groupId>
|
||||||
<artifactId>adventure-api</artifactId>
|
<artifactId>adventure-api</artifactId>
|
||||||
<version>4.11.0</version>
|
<version>4.14.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.kyori</groupId>
|
<groupId>net.kyori</groupId>
|
||||||
<artifactId>adventure-platform-bungeecord</artifactId>
|
<artifactId>adventure-platform-bungeecord</artifactId>
|
||||||
<version>4.1.1</version>
|
<version>4.3.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.kyori</groupId>
|
<groupId>net.kyori</groupId>
|
||||||
<artifactId>adventure-text-serializer-plain</artifactId>
|
<artifactId>adventure-text-serializer-plain</artifactId>
|
||||||
<version>4.11.0</version>
|
<version>4.14.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ import com.google.gson.GsonBuilder;
|
|||||||
import com.google.gson.TypeAdapter;
|
import com.google.gson.TypeAdapter;
|
||||||
import com.google.gson.TypeAdapterFactory;
|
import com.google.gson.TypeAdapterFactory;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import fr.pandacube.lib.util.Log;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -90,23 +89,7 @@ public class Json {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static boolean hasGsonNativeRecordSupport() {
|
|
||||||
try {
|
|
||||||
com.google.gson.internal.reflect.ReflectionHelper.class.getDeclaredField("RECORD_HELPER");
|
|
||||||
return true;
|
|
||||||
} catch (NoClassDefFoundError e) {
|
|
||||||
Log.warning("Unable to check Gson supporting records. Assuming it does not. " + e);
|
|
||||||
return false;
|
|
||||||
} catch (NoSuchFieldException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
if (!hasGsonNativeRecordSupport())
|
|
||||||
registerTypeAdapterFactory(RecordTypeAdapter.FACTORY);
|
|
||||||
registerTypeAdapterFactory(StackTraceElementAdapter.FACTORY);
|
registerTypeAdapterFactory(StackTraceElementAdapter.FACTORY);
|
||||||
registerTypeAdapterFactory(ThrowableAdapter.FACTORY);
|
registerTypeAdapterFactory(ThrowableAdapter.FACTORY);
|
||||||
}
|
}
|
||||||
|
@ -1,88 +0,0 @@
|
|||||||
package fr.pandacube.lib.core.json;
|
|
||||||
|
|
||||||
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.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.RecordComponent;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
// from https://github.com/google/gson/issues/1794#issuecomment-812964421
|
|
||||||
/* package */ class RecordTypeAdapter<T> extends TypeAdapter<T> {
|
|
||||||
|
|
||||||
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
|
|
||||||
@Override
|
|
||||||
public <TT> TypeAdapter<TT> create(Gson gson, TypeToken<TT> type) {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Class<TT> clazz = (Class<TT>) type.getRawType();
|
|
||||||
if (!clazz.isRecord() || clazz == Record.class) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return new RecordTypeAdapter<>(gson, this, type);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
private final Gson gson;
|
|
||||||
private final TypeAdapterFactory factory;
|
|
||||||
private final TypeToken<T> type;
|
|
||||||
|
|
||||||
public RecordTypeAdapter(Gson gson, TypeAdapterFactory factory, TypeToken<T> type) {
|
|
||||||
this.gson = gson;
|
|
||||||
this.factory = factory;
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void write(JsonWriter out, T value) throws IOException {
|
|
||||||
gson.getDelegateAdapter(factory, type).write(out, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public T read(JsonReader reader) throws IOException {
|
|
||||||
if (reader.peek() == JsonToken.NULL) {
|
|
||||||
reader.nextNull();
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Class<T> clazz = (Class<T>) type.getRawType();
|
|
||||||
|
|
||||||
RecordComponent[] recordComponents = clazz.getRecordComponents();
|
|
||||||
Map<String, TypeToken<?>> typeMap = new HashMap<>();
|
|
||||||
for (RecordComponent recordComponent : recordComponents) {
|
|
||||||
typeMap.put(recordComponent.getName(), TypeToken.get(recordComponent.getGenericType()));
|
|
||||||
}
|
|
||||||
var argsMap = new HashMap<String, Object>();
|
|
||||||
reader.beginObject();
|
|
||||||
while (reader.hasNext()) {
|
|
||||||
String name = reader.nextName();
|
|
||||||
argsMap.put(name, gson.getAdapter(typeMap.get(name)).read(reader));
|
|
||||||
}
|
|
||||||
reader.endObject();
|
|
||||||
|
|
||||||
var argTypes = new Class<?>[recordComponents.length];
|
|
||||||
var args = new Object[recordComponents.length];
|
|
||||||
for (int i = 0; i < recordComponents.length; i++) {
|
|
||||||
argTypes[i] = recordComponents[i].getType();
|
|
||||||
args[i] = argsMap.get(recordComponents[i].getName());
|
|
||||||
}
|
|
||||||
Constructor<T> constructor;
|
|
||||||
try {
|
|
||||||
constructor = clazz.getDeclaredConstructor(argTypes);
|
|
||||||
constructor.setAccessible(true);
|
|
||||||
return constructor.newInstance(args);
|
|
||||||
} catch (NoSuchMethodException | InstantiationException | SecurityException | IllegalAccessException
|
|
||||||
| IllegalArgumentException | InvocationTargetException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user