@@ -9,6 +9,10 @@ import java.util.Map;
9
9
10
10
import org.apache.oltu.oauth2.client.request.OAuthClientRequest.AuthenticationRequestBuilder;
11
11
import org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder;
12
+ import org.joda.time.DateTime;
13
+ import org.joda.time.LocalDate;
14
+ import org.joda.time.format.DateTimeFormatter;
15
+ import org.joda.time.format.ISODateTimeFormat;
12
16
13
17
import retrofit2.Converter;
14
18
import retrofit2.Retrofit;
@@ -19,6 +23,9 @@ import retrofit2.converter.scalars.ScalarsConverterFactory;
19
23
import com.google.gson.Gson;
20
24
import com.google.gson.GsonBuilder;
21
25
import com.google.gson.JsonParseException;
26
+ import com.google.gson.TypeAdapter;
27
+ import com.google.gson.stream.JsonReader;
28
+ import com.google.gson.stream.JsonWriter;
22
29
import okhttp3.Interceptor;
23
30
import okhttp3.OkHttpClient;
24
31
import okhttp3.RequestBody;
@@ -108,6 +115,8 @@ public class ApiClient {
108
115
public void createDefaultAdapter() {
109
116
Gson gson = new GsonBuilder()
110
117
.setDateFormat(" yyyy-MM-dd'T'HH:mm:ss.SSSZ" )
118
+ .registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
119
+ .registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
111
120
.create();
112
121
113
122
okClient = new OkHttpClient();
@@ -346,3 +355,58 @@ class GsonCustomConverterFactory extends Converter.Factory
346
355
}
347
356
}
348
357
358
+
359
+ /**
360
+ * Gson TypeAdapter for Joda DateTime type
361
+ */
362
+ class DateTimeTypeAdapter extends TypeAdapter<DateTime > {
363
+
364
+ private final DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
365
+
366
+ @Override
367
+ public void write(JsonWriter out, DateTime date) throws IOException {
368
+ if (date == null) {
369
+ out.nullValue();
370
+ } else {
371
+ out.value(formatter.print(date));
372
+ }
373
+ }
374
+
375
+ @Override
376
+ public DateTime read(JsonReader in) throws IOException {
377
+ switch (in.peek()) {
378
+ case NULL:
379
+ in.nextNull();
380
+ return null;
381
+ default :
382
+ String date = in.nextString();
383
+ return formatter.parseDateTime(date);
384
+ }
385
+ }
386
+ }
387
+
388
+ class LocalDateTypeAdapter extends TypeAdapter<LocalDate > {
389
+
390
+ private final DateTimeFormatter formatter = ISODateTimeFormat.date();
391
+
392
+ @Override
393
+ public void write(JsonWriter out, LocalDate date) throws IOException {
394
+ if (date == null) {
395
+ out.nullValue();
396
+ } else {
397
+ out.value(formatter.print(date));
398
+ }
399
+ }
400
+
401
+ @Override
402
+ public LocalDate read(JsonReader in) throws IOException {
403
+ switch (in.peek()) {
404
+ case NULL:
405
+ in.nextNull();
406
+ return null;
407
+ default :
408
+ String date = in.nextString();
409
+ return formatter.parseLocalDate(date);
410
+ }
411
+ }
412
+ }
0 commit comments