Skip to content

Commit

Permalink
Fix RuntimeTypeAdapterFactory depending on internal Streams class
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 committed Sep 11, 2021
1 parent b82c767 commit 7f51945
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.google.gson.JsonPrimitive;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
Expand Down Expand Up @@ -204,11 +203,13 @@ public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) {
return registerSubtype(type, type.getSimpleName());
}

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
@Override
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
if (type.getRawType() != baseType) {
return null;
}

final TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);
final Map<String, TypeAdapter<?>> labelToDelegate
= new LinkedHashMap<String, TypeAdapter<?>>();
final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate
Expand All @@ -221,7 +222,7 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {

return new TypeAdapter<R>() {
@Override public R read(JsonReader in) throws IOException {
JsonElement jsonElement = Streams.parse(in);
JsonElement jsonElement = jsonElementAdapter.read(in);
JsonElement labelJsonElement;
if (maintainType) {
labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);
Expand Down Expand Up @@ -255,7 +256,7 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();

if (maintainType) {
Streams.write(jsonObject, out);
jsonElementAdapter.write(out, jsonObject);
return;
}

Expand All @@ -270,7 +271,7 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
clone.add(e.getKey(), e.getValue());
}
Streams.write(clone, out);
jsonElementAdapter.write(out, clone);
}
}.nullSafe();
}
Expand Down

0 comments on commit 7f51945

Please sign in to comment.