package safeluck.drive.evaluation.httpmodule;
|
|
import com.google.gson.Gson;
|
import com.google.gson.TypeAdapter;
|
import com.google.gson.reflect.TypeToken;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import java.lang.annotation.Annotation;
|
import java.lang.reflect.Type;
|
|
import javax.annotation.Nullable;
|
|
import okhttp3.RequestBody;
|
import retrofit2.Converter;
|
import retrofit2.Retrofit;
|
|
public class GsonConverterFactory extends Converter.Factory {
|
|
|
@NotNull
|
public static Converter.Factory create() {
|
return create(new Gson());
|
}
|
|
private static Converter.Factory create(Gson gson) {
|
return new GsonConverterFactory(gson);
|
}
|
|
private final Gson gson;
|
private GsonConverterFactory(Gson gson){
|
if (gson == null){
|
throw new NullPointerException("gson == null");
|
}
|
this.gson = gson;
|
}
|
|
@Nullable
|
@Override
|
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
|
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
|
return new MyGsonRequestBodyConverter<>(gson,adapter);
|
}
|
}
|