Skip to content

Commit

Permalink
Add TypeAdapterFactory for Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jun 11, 2023
1 parent b145985 commit c83e00e
Showing 1 changed file with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
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 com.ryanharter.auto.value.gson.GenerateTypeAdapter;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Optional;

/**
* Utility class to hold type adapters and helper methods to get gson registered with type adapters
Expand Down Expand Up @@ -88,6 +94,53 @@ public ModuleKey read(JsonReader jsonReader) throws IOException {
}
};

public static final TypeAdapterFactory OPTIONAL =
new TypeAdapterFactory() {
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
if (typeToken.getRawType() != Optional.class) {
return null;
}
Type type = typeToken.getType();
if (!(type instanceof ParameterizedType)) {
return null;
}
Type elementType = ((ParameterizedType) typeToken.getType()).getActualTypeArguments()[0];
var elementTypeAdapter = gson.getAdapter(TypeToken.get(elementType));
if (elementTypeAdapter == null) {
return null;
}
return (TypeAdapter<T>) new OptionalTypeAdapter<>(elementTypeAdapter);
}
};

private static final class OptionalTypeAdapter<T> extends TypeAdapter<Optional<T>> {
private final TypeAdapter<T> elementTypeAdapter;

public OptionalTypeAdapter(TypeAdapter<T> elementTypeAdapter) {
this.elementTypeAdapter = elementTypeAdapter;
}

@Override
public void write(JsonWriter jsonWriter, Optional<T> t) throws IOException {
if (t == null || t.isEmpty()) {
jsonWriter.nullValue();
} else {
elementTypeAdapter.write(jsonWriter, t.get());
}
}

@Override
public Optional<T> read(JsonReader jsonReader) throws IOException {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return Optional.empty();
} else {
return Optional.of(elementTypeAdapter.read(jsonReader));
}
}
}

public static final Gson LOCKFILE_GSON =
new GsonBuilder()
.setPrettyPrinting()
Expand All @@ -98,6 +151,7 @@ public ModuleKey read(JsonReader jsonReader) throws IOException {
.registerTypeAdapterFactory(IMMUTABLE_LIST)
.registerTypeAdapterFactory(IMMUTABLE_BIMAP)
.registerTypeAdapterFactory(IMMUTABLE_SET)
.registerTypeAdapterFactory(OPTIONAL)
.registerTypeAdapter(Version.class, VERSION_TYPE_ADAPTER)
.registerTypeAdapter(ModuleKey.class, MODULE_KEY_TYPE_ADAPTER)
.registerTypeAdapter(AttributeValues.class, new AttributeValuesAdapter())
Expand Down

0 comments on commit c83e00e

Please sign in to comment.