Gson: How to exclude specific fields from Serialization without annotations

后端 未结 15 1644
后悔当初
后悔当初 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:24

    You can explore the json tree with gson.

    Try something like this :

    gson.toJsonTree(student).getAsJsonObject()
    .get("country").getAsJsonObject().remove("name");
    

    You can add some properties also :

    gson.toJsonTree(student).getAsJsonObject().addProperty("isGoodStudent", false);
    

    Tested with gson 2.2.4.

提交回复
热议问题