xyz.yhsj.easynet.factory.ConverterFactoryPro.java Source code

Java tutorial

Introduction

Here is the source code for xyz.yhsj.easynet.factory.ConverterFactoryPro.java

Source

/*
 * Copyright (C) 2015 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package xyz.yhsj.easynet.factory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * ?????
 */
public final class ConverterFactoryPro extends Converter.Factory {
    private final Gson gson;

    public static ConverterFactoryPro create() {

        Gson gson = new GsonBuilder().registerTypeAdapter(Double.class, new JsonSerializer<Double>() {
            @Override
            public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
                if (src == src.longValue())
                    return new JsonPrimitive(src.longValue());
                return new JsonPrimitive(src);
            }
        }).create();

        return create(gson);
    }

    public static ConverterFactoryPro create(Gson gson) {
        return new ConverterFactoryPro(gson);
    }

    private ConverterFactoryPro(Gson gson) {
        if (gson == null)
            throw new NullPointerException("ConverterFactoryPro:gson == null");
        this.gson = gson;
    }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations,
            Annotation[] methodAnnotations, Retrofit retrofit) {
        if (type == String.class || type == boolean.class || type == Boolean.class || type == byte.class
                || type == Byte.class || type == char.class || type == Character.class || type == double.class
                || type == Double.class || type == float.class || type == Float.class || type == int.class
                || type == Integer.class || type == long.class || type == Long.class || type == short.class
                || type == Short.class) {
            return RequestBodyConverterPro.BaseResponseBodyConverter.INSTANCE;
        }
        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        return new RequestBodyConverterPro.GsonResponseBodyConverter<>(gson, adapter);

    }

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
            Retrofit retrofit) {
        if (type == String.class) {
            return ResponseBodyConverterPro.StringResponseBodyConverter.INSTANCE;
        }
        if (type == Boolean.class || type == boolean.class) {
            return ResponseBodyConverterPro.BooleanResponseBodyConverter.INSTANCE;
        }
        if (type == Byte.class || type == byte.class) {
            return ResponseBodyConverterPro.ByteResponseBodyConverter.INSTANCE;
        }
        if (type == Character.class || type == char.class) {
            return ResponseBodyConverterPro.CharacterResponseBodyConverter.INSTANCE;
        }
        if (type == Double.class || type == double.class) {
            return ResponseBodyConverterPro.DoubleResponseBodyConverter.INSTANCE;
        }
        if (type == Float.class || type == float.class) {
            return ResponseBodyConverterPro.FloatResponseBodyConverter.INSTANCE;
        }
        if (type == Integer.class || type == int.class) {
            return ResponseBodyConverterPro.IntegerResponseBodyConverter.INSTANCE;
        }
        if (type == Long.class || type == long.class) {
            return ResponseBodyConverterPro.LongResponseBodyConverter.INSTANCE;
        }
        if (type == Short.class || type == short.class) {
            return ResponseBodyConverterPro.ShortResponseBodyConverter.INSTANCE;
        }

        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        return new ResponseBodyConverterPro.GsonResponseBodyConverter<>(gson, adapter);
    }
}