| 1 |
package com.fasterxml.jackson.jr.retrofit2; |
| 2 |
|
| 3 |
import java.lang.annotation.Annotation; |
| 4 |
import java.lang.reflect.Type; |
| 5 |
|
| 6 |
import com.fasterxml.jackson.jr.ob.JSON; |
| 7 |
|
| 8 |
import okhttp3.RequestBody; |
| 9 |
import okhttp3.ResponseBody; |
| 10 |
import retrofit2.Converter; |
| 11 |
import retrofit2.Retrofit; |
| 12 |
|
| 13 |
public class JacksonJrConverter<T> extends Converter.Factory |
| 14 |
{ |
| 15 |
protected final JSON _jr; |
| 16 |
protected final Class<T> _type; |
| 17 |
|
| 18 |
public JacksonJrConverter(Class<T> tClass) { |
| 19 |
this(JSON.std, tClass); |
| 20 |
} |
| 21 |
|
| 22 |
public JacksonJrConverter(JSON jr, Class<T> t) { |
| 23 |
super(); |
| 24 |
_jr = jr; |
| 25 |
_type = t; |
| 26 |
} |
| 27 |
|
| 28 |
@Override |
| 29 |
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) |
| 30 |
{ |
| 31 |
super.responseBodyConverter(type, annotations, retrofit); |
| 32 |
|
| 33 |
//if same as class type return as class, otherwise, return as list |
| 34 |
if (_type == type) { |
| 35 |
return new JacksonJrResponseConverter<T>(_jr, _type); |
| 36 |
} |
| 37 |
return new JacksonJrResponseArrayConverter<T>(_jr, _type); |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) |
| 42 |
{ |
| 43 |
super.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit); |
| 44 |
return new JacksonJrRequestBodyConverter<T>(_jr); |
| 45 |
} |
| 46 |
} |