Gson Type Adapter vs. Custom Deseralizer

前端 未结 3 1474
面向向阳花
面向向阳花 2020-12-29 06:54

The example below shows a class (Club) that contains a collection of an abstract class (Member). I\'m confused as to whether I need a TypeAdapter or JsonDeserializer to make

3条回答
  •  臣服心动
    2020-12-29 07:28

    It looks like serializing/deserializing class hierarchies is a common problem.

    There is even an "official" solution, inside extras directory of the official source repo (unfortunately it is not part of the Maven package though).

    Please check:

    • The explanation: https://blog.novatec-gmbh.de/gson-object-hierarchies/
    • The solution: https://github.com/google/gson/blob/master/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java. It is suggested to just copy/paste the source.

提交回复
热议问题