Last active
April 2, 2019 02:13
-
-
Save mandybess/6b6845495a2956d0d76b to your computer and use it in GitHub Desktop.
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
import com.google.gson.JsonDeserializationContext; | |
import com.google.gson.JsonDeserializer; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonParseException; | |
import com.google.gson.JsonPrimitive; | |
import com.google.gson.JsonSerializationContext; | |
import com.google.gson.JsonSerializer; | |
import java.lang.reflect.Type; | |
import org.joda.time.DateTime; | |
public class DateTimeTypeConverter implements JsonSerializer<DateTime>, JsonDeserializer<DateTime> { | |
@Override | |
public JsonElement serialize(DateTime src, Type srcType, JsonSerializationContext context) { | |
return new JsonPrimitive(src.toString()); | |
} | |
@Override | |
public DateTime deserialize(JsonElement json, Type type, JsonDeserializationContext context) | |
throws JsonParseException { | |
// Do not try to deserialize null or empty values | |
if (json.getAsString() == null || json.getAsString().isEmpty()) { | |
return null; | |
} | |
long milliseconds = json.getAsLong() * 1000l; | |
return new DateTime(milliseconds); | |
} | |
} |
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
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import org.joda.time.DateTime; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
public class JodaDateTimeConverterFactory { | |
private static Gson gson() { | |
GsonBuilder gsonBuilder = new GsonBuilder(); | |
gsonBuilder.registerTypeAdapter(DateTime.class, new DateTimeTypeConverter()); | |
return gsonBuilder.create(); | |
} | |
/** | |
* Create an instance using {@code gson} for conversion. The provided {@code gson} instance has a | |
* registered TypeAdapter to handle deserializing {@link DateTime}. Encoding to JSON and | |
* decoding from JSON (when no charset is specified by a header) will use UTF-8. | |
*/ | |
public static GsonConverterFactory create() { | |
return GsonConverterFactory.create(gson()); | |
} | |
} |
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
/** | |
* For REST over HTTP(S). Holds the client for other services to put interfaces against. | |
*/ | |
public class RestClient { | |
private final Retrofit mClient; | |
public RestClient() { | |
mClient = new Retrofit.Builder() | |
.baseUrl(baseUrl()) | |
.addConverterFactory(JodaDateTimeConverterFactory.create()) | |
.build(); | |
} | |
/** | |
* Creates an implementation of the API defined by the {@link } | |
*/ | |
public <T> T create(Class<T> apiInterfaceClass) { | |
return mClient.create(apiInterfaceClass); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment