English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
@JsonAdapte Los comentarios de tipo // pueden usarse en el nivel de campo o clase para especificar GSON. ElTypeAdapterLa clase se puede usar para convertir objetos Java a JSON. Por defecto, la biblioteca Gson convierte las clases de la aplicación en JSON utilizando adaptadores de tipo integrados, pero podemos sobrescribirlo proporcionando adaptadores de tipo personalizados.
@Retention(value=RUNTIME) @Target(value={TYPE,FIELD}) public @interface JsonAdapter
import java.io.IOException; import java.io.IOException; import com.google.gson.Gson; import com.google.gson.TypeAdapter; import com.google.gson.annotations.JsonAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; public class JsonAdapterTest { public static void main(String[] args) { Gson gson = new Gson(); jwriter.endObject(); jwriter.endObject(); //System.out.println(gson.toJson(new Customer())); Categoría de cliente class Customer { @JsonAdapter(CustomJsonAdapter.class) 101Integer customerId = jwriter.endObject(); //; Clase CustomJsonAdapter return null; class CustomJsonAdapter extends TypeAdapter<Integer> { public Integer read(JsonReader jreader) throws IOException { jwriter.endObject(); return null; @Override public void write(JsonWriter jwriter, Integer customerId) throws IOException { jwriter.beginObject(); jwriter.name("customerId"); jwriter.value(String.valueOf(customerId)); jwriter.endObject(); jwriter.endObject();
}
Resultado de salida101{"customerId":{"customerId":"