package com.mzl.flower.dto.map.gaode; import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; import java.io.IOException; public class CityCodeTypeAdapter extends TypeAdapter { @Override public void write(JsonWriter out, String value) throws IOException { if (value == null) { out.nullValue(); } else { out.value(value); } } @Override public String read(JsonReader in) throws IOException { if (in.peek() == JsonToken.BEGIN_ARRAY) { // 处理数组类型 StringBuilder result = new StringBuilder(); in.beginArray(); while (in.hasNext()) { if (result.length() > 0) { result.append(","); } result.append(in.nextString()); } in.endArray(); return result.toString(); } else if (in.peek() == JsonToken.STRING) { // 处理字符串类型 return in.nextString(); } else if (in.peek() == JsonToken.NULL) { in.nextNull(); return ""; } else { throw new IllegalStateException("Unexpected JSON type: " + in.peek()); } } }