Gson:如何在没有注释的情况下从序列化中排除特定字段 [英] Gson: How to exclude specific fields from Serialization without annotations
问题描述
public class Student {
private Long id;
private String firstName =Philip;
private String middleName =J.;
private String initials =P.F;
private String lastName =Fry;
私人国家国家;
私人国家countryOfBirth;
}
公共类国家{
私人长ID;
私人字符串名称;
私人对象其他;
}
我可以使用GsonBuilder并为字段名添加ExclusionStrategy,比如 firstName
或 country
但我似乎无法设法排除某些字段的属性,例如 country。 name
。
使用方法 public boolean shouldSkipField(FieldAttributes fa)
,FieldAttributes doesn不包含足够的信息来匹配该字段与过滤器,如 country.name
。
我将不胜感激任何帮助为此问题提供解决方案。
PS:我想避免注释,因为我想改进它并使用RegEx过滤字段。
谢谢
编辑:我试图看看是否可以模拟< a href =https://cwiki.apache.org/confluence/display/WW/JSON%20Plugin =noreferrer> Struts2 JSON插件
使用Gson
< intercepto r-ref name =json>
< param name =enableSMD> true< / param>
< param name =excludeProperties>
login.password,
studentList。* \.sin
< / param>
< / interceptor-ref>
编辑:
我重新打开了这个问题, :
我添加了第二个相同类型的字段,以进一步阐明这个问题。基本上我想排除 country.name
但不是 countrOfBirth.name
。我也不想将国家排除在外。
所以类型是相同的,它是对象图中我想查明和排除的实际位置。
你不希望序列化的任何字段通常应该使用transient修饰符,这也适用于json序列化器(至少它使用了一些我已经使用过的,包括gson)。
如果你不想在序列化的json中显示名字,就给它一个临时关键字,例如:
私有瞬态字符串名称;
I'm trying to learn Gson and I'm struggling with field exclusion. Here are my classes
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;
}
I can use the GsonBuilder and add an ExclusionStrategy for a field name like firstName
or country
but I can't seem to manage to exclude properties of certain fields like country.name
.
Using the method public boolean shouldSkipField(FieldAttributes fa)
, FieldAttributes doesn't contain enough information to match the field with a filter like country.name
.
I would appreciate any help with a solution for this problem.
P.S: I want to avoid annotations since I want to improve on this and use RegEx to filter fields out.
Thank you
Edit: I'm trying to see if it's possible to emulate the behavior of Struts2 JSON plugin
using Gson
<interceptor-ref name="json">
<param name="enableSMD">true</param>
<param name="excludeProperties">
login.password,
studentList.*\.sin
</param>
</interceptor-ref>
Edit: I reopened the question with the following addition:
I added a second field with the same type to futher clarify this problem. Basically I want to exclude country.name
but not countrOfBirth.name
. I also don't want to exclude Country as a type.
So the types are the same it's the actual place in the object graph that I want to pinpoint and exclude.
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
这篇关于Gson:如何在没有注释的情况下从序列化中排除特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!