add support for jsr310 dates to okhttp-gson client

This commit is contained in:
cbornet
2016-06-20 16:50:08 +02:00
parent 79dc34266f
commit d29a5537bc
11 changed files with 99 additions and 32 deletions

View File

@@ -21,10 +21,17 @@ import java.io.StringReader;
import java.lang.reflect.Type;
import java.util.Date;
{{^java8}}
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
{{/java8}}
{{#java8}}
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
{{/java8}}
public class JSON {
private ApiClient apiClient;
@@ -39,7 +46,12 @@ public class JSON {
this.apiClient = apiClient;
gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new DateAdapter(apiClient))
{{^java8}}
.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
{{/java8}}
{{#java8}}
.registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeTypeAdapter())
{{/java8}}
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
.create();
}
@@ -154,6 +166,7 @@ class DateAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
}
}
{{^java8}}
/**
* Gson TypeAdapter for Joda DateTime type
*/
@@ -211,4 +224,67 @@ class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
}
}
}
{{/java8}}
{{#java8}}
/**
* Gson TypeAdapter for jsr310 OffsetDateTime type
*/
class OffsetDateTimeTypeAdapter extends TypeAdapter<OffsetDateTime> {
private final DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
@Override
public void write(JsonWriter out, OffsetDateTime date) throws IOException {
if (date == null) {
out.nullValue();
} else {
out.value(formatter.format(date));
}
}
@Override
public OffsetDateTime read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
if (date.endsWith("+0000")) {
date = date.substring(0, date.length()-5) + "Z";
}
return OffsetDateTime.parse(date, formatter);
}
}
}
/**
* Gson TypeAdapter for jsr310 LocalDate type
*/
class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
@Override
public void write(JsonWriter out, LocalDate date) throws IOException {
if (date == null) {
out.nullValue();
} else {
out.value(formatter.format(date));
}
}
@Override
public LocalDate read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
return LocalDate.parse(date, formatter);
}
}
}
{{/java8}}

View File

@@ -78,8 +78,8 @@ if(hasProperty('target') && target == 'android') {
apply plugin: 'java'
apply plugin: 'maven'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
sourceCompatibility = JavaVersion.VERSION_{{^java8}}1_7{{/java8}}{{#java8}}1_8{{/java8}}
targetCompatibility = JavaVersion.VERSION_{{^java8}}1_7{{/java8}}{{#java8}}1_8{{/java8}}
install {
repositories.mavenInstaller {
@@ -98,6 +98,8 @@ dependencies {
compile 'com.squareup.okhttp:okhttp:2.7.5'
compile 'com.squareup.okhttp:logging-interceptor:2.7.5'
compile 'com.google.code.gson:gson:2.6.2'
{{^java8}}
compile 'joda-time:joda-time:2.9.3'
{{/java8}}
testCompile 'junit:junit:4.12'
}

View File

@@ -13,7 +13,9 @@ lazy val root = (project in file(".")).
"com.squareup.okhttp" % "okhttp" % "2.7.5",
"com.squareup.okhttp" % "logging-interceptor" % "2.7.5",
"com.google.code.gson" % "gson" % "2.6.2",
{{^java8}}
"joda-time" % "joda-time" % "2.9.3" % "compile",
{{/java8}}
"junit" % "junit" % "4.12" % "test",
"com.novocode" % "junit-interface" % "0.10" % "test"
)

View File

@@ -96,15 +96,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
@@ -128,11 +119,13 @@
<artifactId>gson</artifactId>
<version>${gson-version}</version>
</dependency>
{{^java8}}
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${jodatime-version}</version>
</dependency>
</dependency>
{{/java8}}
<!-- test dependencies -->
<dependency>
@@ -143,6 +136,9 @@
</dependency>
</dependencies>
<properties>
<java.version>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<swagger-core-version>1.5.9</swagger-core-version>
<okhttp-version>2.7.5</okhttp-version>
<gson-version>2.6.2</gson-version>