Gson deserialize interface to its Class implementation

后端 未结 2 1286
甜味超标
甜味超标 2021-02-14 20:35

I am using Retrofit 2.1.0 with converter-gson:2.1.0 and separately gson:2.6.2 in order to customize the serialization/deserial

2条回答
  •  北荒
    北荒 (楼主)
    2021-02-14 20:59

    Because you are willing to take the effort to almost duplicate your entire domain layer using interfaces to hide the implementation detail of your models, I think you will find my answer refresing ;)

    You should use AutoValue in order to hide any implementation detail in your models. The way it works is pretty simple:

    You write an abstract class, and AutoValue implements it. That is all there is to it; there is literally no configuration.

    Adopting this approach you won’t need to create such amount of boilerplate.

    And there is this AutoValue Extension called auto-value-gson, which adds Gson De/Serializer support out of box.

    With these simple steps I think your code base will improve substantially.

提交回复
热议问题