只想包含 Genson 映射到 JSON 的对象的某些字段
Want to include only some fields of an object that are being mapped to JSON by Genson
我的 Pojo class 包含 50 个字段,我只需要将 10 个字段转换为 json。
Genson genson = new Genson.Builder().include("address4", User.class).create();
String json = genson.serialize(user);
"include()" 方法似乎不起作用。请帮忙。
一个选项是默认禁用所有属性并有选择地包含您想要的属性,如下所示:
new GensonBuilder()
.useFields(false)
.useMethods(false)
.include("address4", User.class)
.create();
请注意,我认为您还可以在要包含的字段上使用 @JsonProperty
注释,而不是通过 GensonBuilder 包含它们。
如果您觉得自己在为该库做出贡献,您还可以改进 PropertyFilter class to support regular expressions for the field name or by adding another method that would be excludeAll(Class clazz)/includeAll(Class clazz), that would exclude all properties from this class. I opened this issue 以跟踪此功能。
Genson genson = new Genson.Builder()
.exclude(Object.class)//this excludes all object types
.include("address4", User.class).create();//then add only required fields
String json = genson.serialize(user);
我的 Pojo class 包含 50 个字段,我只需要将 10 个字段转换为 json。
Genson genson = new Genson.Builder().include("address4", User.class).create();
String json = genson.serialize(user);
"include()" 方法似乎不起作用。请帮忙。
一个选项是默认禁用所有属性并有选择地包含您想要的属性,如下所示:
new GensonBuilder()
.useFields(false)
.useMethods(false)
.include("address4", User.class)
.create();
请注意,我认为您还可以在要包含的字段上使用 @JsonProperty
注释,而不是通过 GensonBuilder 包含它们。
如果您觉得自己在为该库做出贡献,您还可以改进 PropertyFilter class to support regular expressions for the field name or by adding another method that would be excludeAll(Class clazz)/includeAll(Class clazz), that would exclude all properties from this class. I opened this issue 以跟踪此功能。
Genson genson = new Genson.Builder()
.exclude(Object.class)//this excludes all object types
.include("address4", User.class).create();//then add only required fields
String json = genson.serialize(user);