gson のシリアライズ用アノテーション

@Expose
GsonBuilder で、excludeFieldsWithoutExposeAnnotation() を指定すれば、
@Expose が付いたフィールドだけシリアライズする

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
.create():

@SerializedName
フィールド名でなく任意の名称を JSONキーにする。
注意:@Expose を付けないで、excludeFieldsWithoutExposeAnnotation() で作成する Gson で
優先されることはない。

@JsonAdapter( Xyz.class )
JsonSerializer を実装したクラスを指定して
GsonBuilder registerTypeAdapter( class, typeAdapter )の代わりをする。

すると、yipuran-gsonhelper で作った
Home · yipuran/yipuran-gsonhelper Wiki · GitHub
interface として用意した
 org.yipuran.gsonhelper.LocalDateAdapter
 org.yipuran.gsonhelper.LocalDateTimeAdapter

は、無駄だったか?
これは、シリアライズ&デシリアライズ両方の目的に使用できるように作ったもののシリアライズが被る。
@JsonAdapter でこれは指定できない。
無駄ではない! JsonSerializer を extends した interface であるのだから。。。

import java.time.format.DateTimeFormatter;
import org.yipuran.gsonhelper.LocalDateAdapter;

public class MyLocalDateAdapter implements LocalDateAdapter{
   @Override
   public DateTimeFormatter getFormatter(){
      return DateTimeFormatter.ofPattern("yyyy/MM/dd");
   }
}

として、

   @JsonAdapter(MyLocalDateAdapter.class)
   public LocalDate date;

として使用ができる。
@Expose、@SerializedName、@JsonAdapter で、個々のオブジェクトの定義で制御するか、
GsonBuilder registerTypeAdapter( class, typeAdapter ) で一括制御するか、
Google Gson の柔軟さと言えるし、この柔軟さが使いやすくもあり、
センスなければ使いこなせないであろう。

@Expose に対して、除外を指定するアノテーションも考えて、yipuran-gsonhelper を用意している。
excludeanotate · yipuran/yipuran-gsonhelper Wiki · GitHub