Created
March 21, 2015 03:02
-
-
Save fdoyle/fd2b053886ff45795e2e to your computer and use it in GitHub Desktop.
Optional Type Adapter for Gson (using the Optional gist located elsewhere)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.derp.gsonoptional; | |
/** | |
* Created by fdoyle on 3/20/15. | |
*/ | |
import com.google.gson.JsonArray; | |
import com.google.gson.JsonDeserializationContext; | |
import com.google.gson.JsonDeserializer; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonParseException; | |
import com.google.gson.JsonSerializationContext; | |
import com.google.gson.JsonSerializer; | |
import java.lang.reflect.ParameterizedType; | |
import java.lang.reflect.Type; | |
/** | |
* Created by fdoyle on 12/18/14. | |
* <p/> | |
* a backport of the java 8 optional type | |
*/ | |
public class Optional<T> { | |
private T value; | |
static <T> Optional<T> empty() { | |
return new Optional<>(); | |
} | |
public boolean equals(Object obj) { | |
//todo | |
return false; | |
} | |
public T get() { | |
return value; | |
} | |
public void ifPresent(Consumer<? super T> consumer) { | |
consumer.accept(value); | |
} | |
public boolean isPresent() { | |
return value != null; | |
} | |
//requires not null | |
public static <T> Optional<T> of(T value) { | |
if (value == null) { | |
throw new NullPointerException("value must be non-null"); | |
} | |
Optional<T> optional = new Optional<>(); | |
optional.value = value; | |
return optional; | |
} | |
public static <T> Optional<T> ofNullable(T value) { | |
Optional<T> optional = new Optional<>(); | |
optional.value = value; | |
return optional; | |
} | |
public T orElse(T other) { | |
if (value == null) { | |
return other; | |
} else { | |
return value; | |
} | |
} | |
@Override | |
public String toString() { | |
if (value == null) { | |
return super.toString() + " value: null"; | |
} else { | |
return super.toString() + " value: " + value.toString(); | |
} | |
} | |
public abstract static class Consumer<T> { | |
public abstract void accept(T t); | |
public Consumer<T> andThen(final Consumer<? super T> after) { | |
if (after == null) { | |
throw new NullPointerException(); | |
} | |
final Consumer thisConsumer = this; | |
return new Consumer<T>() { | |
@Override | |
public void accept(T t) { | |
thisConsumer.accept(t); //in java 8, this "consumer" is just a function, | |
after.accept(t); | |
} | |
}; | |
} | |
} | |
//no promises on these, totally untested, probably doesn't even work | |
public class serializer implements JsonDeserializer<Optional<T>>, JsonSerializer<Optional<T>> { | |
@Override | |
public Optional<T> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | |
throws JsonParseException { | |
final JsonArray asJsonArray = json.getAsJsonArray(); | |
final JsonElement jsonElement = asJsonArray.get(0); | |
final T value = context.deserialize(jsonElement, ((ParameterizedType) typeOfT).getActualTypeArguments()[0]); | |
return Optional.ofNullable(value); | |
} | |
@Override | |
public JsonElement serialize(Optional<T> src, Type typeOfSrc, JsonSerializationContext context) { | |
final JsonElement element = context.serialize(src.orElse(null)); | |
final JsonArray result = new JsonArray(); | |
result.add(element); | |
return result; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.derp.gsonoptional; | |
import com.google.gson.JsonDeserializationContext; | |
import com.google.gson.JsonDeserializer; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonParseException; | |
import com.google.gson.JsonSerializationContext; | |
import com.google.gson.JsonSerializer; | |
import java.lang.reflect.ParameterizedType; | |
import java.lang.reflect.Type; | |
/** | |
* Created by fdoyle on 3/20/15. | |
*/ | |
public class OptionalTypeAdapter implements JsonDeserializer<Optional<?>>, JsonSerializer<Optional<?>> { | |
@Override | |
public Optional deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | |
throws JsonParseException { | |
final Object value = context.deserialize(json, ((ParameterizedType) typeOfT).getActualTypeArguments()[0]); | |
return Optional.ofNullable(value); | |
} | |
@Override | |
public JsonElement serialize(Optional<?> src, Type typeOfSrc, JsonSerializationContext context) { | |
return context.serialize(src.orElse(null)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment