content: package org.fly.http.gson;
  
  import com.google.gson.Gson;
  import com.google.gson.TypeAdapter;
  import com.google.gson.internal.LinkedTreeMap;
  import com.google.gson.stream.JsonReader;
  import com.google.gson.stream.JsonToken;
  import com.google.gson.stream.JsonWriter;
  
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  
  /**
   * 解决Map解析时，int转double的问题
   *
   * @author: Albert Li
   * @contact: albertlii@163.com
   * @time: 2021/9/14 9:26 下午
   * @since: 1.0.0
   */
  public class MapTypeAdapter extends TypeAdapter<Object> {
  
      private final TypeAdapter<Object> delegate = new Gson().getAdapter(Object.class);
  
      @Override
      public Object read(JsonReader in) throws IOException {
          JsonToken token = in.peek();
          switch (token) {
              case BEGIN_ARRAY:
                  List<Object> list = new ArrayList<>();
                  in.beginArray();
                  while (in.hasNext()) {
                      list.add(read(in));
                  }
                  in.endArray();
                  return list;
  
              case BEGIN_OBJECT:
                  Map<String, Object> map = new LinkedTreeMap<>();
                  in.beginObject();
                  while (in.hasNext()) {
                      map.put(in.nextName(), read(in));
                  }
                  in.endObject();
                  return map;
  
              case STRING:
                  return in.nextString();
  
              case NUMBER:
                  /**
                   * 改写数字的处理逻辑，将数字值分为整型与浮点型。
                   */
                  String s = in.nextString();
                  if (s.contains(".")) {
                      return Double.valueOf(s);
                  } else {
                      try {
                          return Integer.valueOf(s);
                      } catch (Exception e) {
                          return Long.valueOf(s);
                      }
                  }
  
              case BOOLEAN:
                  return in.nextBoolean();
  
              case NULL:
                  in.nextNull();
                  return null;
  
              default:
                  throw new IllegalStateException();
          }
      }
  
      @Override
      public void write(JsonWriter out, Object value) throws IOException {
          delegate.write(out, value);
      }
  }
  