lizhanwei
2020-04-08 2f7c993b5d856f852645d998385be8fcec82acea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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);
    }
}