Gson: How to exclude specific fields from Serialization without annotations

后端 未结 15 1722
后悔当初
后悔当初 2020-11-22 05:39

I\'m trying to learn Gson and I\'m struggling with field exclusion. Here are my classes

public class Student {    
  private Long                id;
  privat         


        
15条回答
  •  离开以前
    2020-11-22 06:15

    Any fields you don't want serialized in general you should use the "transient" modifier, and this also applies to json serializers (at least it does to a few that I have used, including gson).

    If you don't want name to show up in the serialized json give it a transient keyword, eg:

    private transient String name;
    

    More details in the Gson documentation

提交回复
热议问题