137 lines
5.8 KiB
Java
137 lines
5.8 KiB
Java
package com.discord.gsonutils;
|
|
|
|
import c.d.b.a.a;
|
|
import c.i.a.f.e.o.c;
|
|
import c.i.d.k;
|
|
import c.i.d.o;
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParseException;
|
|
import com.google.gson.TypeAdapter;
|
|
import com.google.gson.internal.LinkedTreeMap;
|
|
import com.google.gson.internal.bind.TypeAdapters;
|
|
import com.google.gson.reflect.TypeToken;
|
|
import com.google.gson.stream.JsonReader;
|
|
import com.google.gson.stream.JsonWriter;
|
|
import java.io.IOException;
|
|
import java.util.ConcurrentModificationException;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
import java.util.NoSuchElementException;
|
|
public final class RuntimeTypeAdapterFactory<T> implements o {
|
|
public final Class<?> i;
|
|
public final String j;
|
|
public final Map<String, Class<?>> k = new LinkedHashMap();
|
|
public final Map<Class<?>, String> l = new LinkedHashMap();
|
|
public final boolean m;
|
|
|
|
/* renamed from: com.discord.gsonutils.RuntimeTypeAdapterFactory$1 reason: invalid class name */
|
|
public class AnonymousClass1 extends TypeAdapter<R> {
|
|
public final /* synthetic */ Map a;
|
|
public final /* synthetic */ Map b;
|
|
|
|
public AnonymousClass1(Map map, Map map2) {
|
|
this.a = map;
|
|
this.b = map2;
|
|
}
|
|
|
|
/* JADX WARNING: Unknown variable types count: 1 */
|
|
@Override // com.google.gson.TypeAdapter
|
|
public R read(JsonReader jsonReader) throws IOException {
|
|
JsonElement jsonElement;
|
|
JsonElement f02 = c.f0(jsonReader);
|
|
if (RuntimeTypeAdapterFactory.this.m) {
|
|
LinkedTreeMap.e<String, JsonElement> c2 = f02.d().a.c(RuntimeTypeAdapterFactory.this.j);
|
|
jsonElement = c2 != null ? c2.o : null;
|
|
} else {
|
|
jsonElement = f02.d().a.remove(RuntimeTypeAdapterFactory.this.j);
|
|
}
|
|
if (jsonElement != null) {
|
|
String g = jsonElement.g();
|
|
TypeAdapter typeAdapter = (TypeAdapter) this.a.get(g);
|
|
if (typeAdapter != null) {
|
|
return typeAdapter.fromJsonTree(f02);
|
|
}
|
|
StringBuilder L = a.L("cannot deserialize ");
|
|
L.append(RuntimeTypeAdapterFactory.this.i);
|
|
L.append(" subtype named ");
|
|
L.append(g);
|
|
L.append("; did you forget to register a subtype?");
|
|
throw new JsonParseException(L.toString());
|
|
}
|
|
StringBuilder L2 = a.L("cannot deserialize ");
|
|
L2.append(RuntimeTypeAdapterFactory.this.i);
|
|
L2.append(" because it does not define a field named ");
|
|
L2.append(RuntimeTypeAdapterFactory.this.j);
|
|
throw new JsonParseException(L2.toString());
|
|
}
|
|
|
|
@Override // com.google.gson.TypeAdapter
|
|
public void write(JsonWriter jsonWriter, R r) throws IOException {
|
|
Class<?> cls = r.getClass();
|
|
String str = RuntimeTypeAdapterFactory.this.l.get(cls);
|
|
TypeAdapter typeAdapter = (TypeAdapter) this.b.get(cls);
|
|
if (typeAdapter != null) {
|
|
JsonObject d = typeAdapter.toJsonTree(r).d();
|
|
if (RuntimeTypeAdapterFactory.this.m) {
|
|
TypeAdapters.X.write(jsonWriter, d);
|
|
return;
|
|
}
|
|
JsonObject jsonObject = new JsonObject();
|
|
if (!(d.a.c(RuntimeTypeAdapterFactory.this.j) != null)) {
|
|
jsonObject.a.put(RuntimeTypeAdapterFactory.this.j, new k(str));
|
|
LinkedTreeMap linkedTreeMap = LinkedTreeMap.this;
|
|
LinkedTreeMap.e eVar = linkedTreeMap.header.l;
|
|
int i = linkedTreeMap.modCount;
|
|
while (true) {
|
|
LinkedTreeMap.e eVar2 = linkedTreeMap.header;
|
|
if (!(eVar != eVar2)) {
|
|
TypeAdapters.X.write(jsonWriter, jsonObject);
|
|
return;
|
|
} else if (eVar == eVar2) {
|
|
throw new NoSuchElementException();
|
|
} else if (linkedTreeMap.modCount == i) {
|
|
LinkedTreeMap.e eVar3 = eVar.l;
|
|
jsonObject.h(eVar.n, eVar.o);
|
|
eVar = eVar3;
|
|
} else {
|
|
throw new ConcurrentModificationException();
|
|
}
|
|
}
|
|
} else {
|
|
StringBuilder L = a.L("cannot serialize ");
|
|
a.b0(cls, L, " because it already defines a field named ");
|
|
L.append(RuntimeTypeAdapterFactory.this.j);
|
|
throw new JsonParseException(L.toString());
|
|
}
|
|
} else {
|
|
throw new JsonParseException(a.j(cls, a.L("cannot serialize "), "; did you forget to register a subtype?"));
|
|
}
|
|
}
|
|
}
|
|
|
|
public RuntimeTypeAdapterFactory(Class<?> cls, String str, boolean z2) {
|
|
if (str == null || cls == null) {
|
|
throw null;
|
|
}
|
|
this.i = cls;
|
|
this.j = str;
|
|
this.m = z2;
|
|
}
|
|
|
|
@Override // c.i.d.o
|
|
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> typeToken) {
|
|
if (typeToken.getRawType() != this.i) {
|
|
return null;
|
|
}
|
|
LinkedHashMap linkedHashMap = new LinkedHashMap();
|
|
LinkedHashMap linkedHashMap2 = new LinkedHashMap();
|
|
for (Map.Entry<String, Class<?>> entry : this.k.entrySet()) {
|
|
TypeAdapter<T> j = gson.j(this, TypeToken.get((Class) entry.getValue()));
|
|
linkedHashMap.put(entry.getKey(), j);
|
|
linkedHashMap2.put(entry.getValue(), j);
|
|
}
|
|
return new AnonymousClass1(linkedHashMap, linkedHashMap2).nullSafe();
|
|
}
|
|
}
|