Update moshi

This commit is contained in:
Andrea Cavalli 2021-05-08 03:07:37 +02:00
parent d752af85d7
commit d8cdb45cb0

View File

@ -12,6 +12,7 @@ import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
@ -45,6 +46,7 @@ public abstract class MoshiPolymorphic<OBJ> {
var abstractMoshiBuilder = new Moshi.Builder();
var abstractClasses = getAbstractClasses();
var concreteClasses = getConcreteClasses();
var extraAdapters = getExtraAdapters();
for (Class<?> declaredClass : abstractClasses) {
var name = fixType(declaredClass.getSimpleName());
@ -55,7 +57,6 @@ public abstract class MoshiPolymorphic<OBJ> {
}
for (Class<?> declaredClass : concreteClasses) {
var modifiers = declaredClass.getModifiers();
var name = fixType(declaredClass.getSimpleName());
JsonAdapter<OBJ> adapter = new NormalValueAdapter<>(name, declaredClass);
concreteClassesSerializers.put(declaredClass, adapter);
@ -63,6 +64,8 @@ public abstract class MoshiPolymorphic<OBJ> {
abstractMoshiBuilder.add(declaredClass, adapter);
}
extraAdapters.forEach(abstractMoshiBuilder::add);
abstractMoshi = abstractMoshiBuilder.build();
}
}
@ -71,6 +74,10 @@ public abstract class MoshiPolymorphic<OBJ> {
protected abstract Set<Class<OBJ>> getConcreteClasses();
protected Map<Class<?>, JsonAdapter<?>> getExtraAdapters() {
return Map.of();
}
protected abstract boolean shouldIgnoreField(String fieldName);
public Moshi.Builder registerAdapters(Moshi.Builder moshiBuilder) {