相关疑难解决方法(0)

Gson:如何在没有注释的情况下从序列化中排除特定字段

我正在努力学习Gson,而我正在努力进行场上排斥.这是我的课程

public class Student {    
  private Long                id;
  private String              firstName        = "Philip";
  private String              middleName       = "J.";
  private String              initials         = "P.F";
  private String              lastName         = "Fry";
  private Country             country;
  private Country             countryOfBirth;
}

public class Country {    
  private Long                id;
  private String              name;
  private Object              other;
}
Run Code Online (Sandbox Code Playgroud)

我可以使用GsonBuilder并添加ExclusionStrategy像一个字段名称firstNamecountry,但我似乎无法管理排除像某些领域的性能country.name.

使用该方法public boolean shouldSkipField(FieldAttributes fa),FieldAttributes不包含足够的信息来匹配具有过滤器的字段country.name.

对于这个问题的解决方案,我将不胜感激.

PS:我想避免注释,因为我想对此进行改进并使用RegEx来过滤字段.

谢谢

编辑:我试图看看是否可以模拟Struts2 JSON插件的行为

使用Gson

<interceptor-ref name="json">
  <param name="enableSMD">true</param>
  <param name="excludeProperties">
    login.password, …
Run Code Online (Sandbox Code Playgroud)

java serialization json gson

404
推荐指数
12
解决办法
31万
查看次数

标签 统计

gson ×1

java ×1

json ×1

serialization ×1