我正在尝试学习 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;
}
我可以使用 GsonBuilder 并为字段名称添加 ExclusionStrategy,例如firstName
orcountry
但我似乎无法设法排除某些字段的属性,例如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,
studentList.*\.sin
</param>
</interceptor-ref>
编辑: 我重新打开了这个问题,并添加了以下内容:
我添加了第二个具有相同类型的字段以进一步澄清这个问题。基本上我想排除country.name
但不是countrOfBirth.name
。我也不想将 Country 排除为一种类型。所以类型是相同的,它是我想要查明和排除的对象图中的实际位置。