Runt
2024-09-28 1464250d6b481fb9274cc571ecbe031b71ab457c
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.runt.open.mvi.retrofit.converter;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.runt.open.mvi.data.HttpApiResult;
 
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
 
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;
 
/**
 * My father is Object, ites purpose of     解密gson转换
 *
 * @purpose Created by Runt (qingingrunt2010@qq.com) on 2021-7-22.
 */
public class GsonConverterFactory extends Converter.Factory {
 
    public static GsonConverterFactory create() {
        return create(false);
    }
 
    public static GsonConverterFactory create(boolean transHump) {
        return create(new GsonBuilder().setDateFormat("MMMM dd, yyyy HH:mm:ss a").create(),transHump);
    }
 
 
    public static GsonConverterFactory create(Gson gson, boolean transHump) {
        return new GsonConverterFactory(gson,transHump);
    }
 
    private final Gson gson;
 
    private final boolean transHump;
 
    public GsonConverterFactory(Gson gson, boolean transHump) {
        if (gson == null) throw new NullPointerException("gson == null");
        this.gson = gson;
        this.transHump = transHump;
    }
 
    @Override
    public Converter<ResponseBody, ? extends HttpApiResult> responseBodyConverter(Type type, Annotation[] annotations,
                                                                                  Retrofit retrofit) {
        TypeAdapter<? extends HttpApiResult> adapter = (TypeAdapter<? extends HttpApiResult>) gson.getAdapter(TypeToken.get(type));
        return new GsonResponseBodyConverter<>(gson, adapter,transHump);
    }
 
    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type,
                                                          Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        return new GsonRequestBodyConverter<>(gson, adapter);
    }
}