文章17 | 阅读 10172 | 点赞0
本文内容大多基于官方文档和网上前辈经验总结,经过个人实践加以整理积累,仅供参考。
Google Gson 支持自定义规则过滤字段,使用 GsonBuilder 类的 addSerializationExclusionStrategy 方法处理序列化过程中的字段过滤,使用 addDeserializationExclusionStrategy 方法处理反序列化过程中的字段过滤。这两个方法都接收 com.google.gson.ExclusionStrategy 接口类型参数
示例
(1) 基于 @Expose 注解过滤
POJO 类
class GsonObject {
public String field1 = "FIELD_1";
@Expose(serialize = true, deserialize = false)
public String field2 = "FIELD_2";
@Expose(serialize = false, deserialize = true)
public String field3 = "FIELD_3";
public String field4 = "FIELD_4";
@Override
public String toString() {
return "GsonObject [field1=" + field1 + ", field2=" + field2 + ", field3=" + field3 + ", field4=" + field4
+ "]";
}
}
测试代码:
@Test
public void test() throws IOException {
Gson gson = new GsonBuilder()
.addSerializationExclusionStrategy(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes arg0) {
Expose expose = arg0.getAnnotation(Expose.class);
if (expose != null && expose.deserialize() == false) {
return true;
}
return false;
}
@Override
public boolean shouldSkipClass(Class<?> arg0) {
return false;
}
}).create();
System.out.println(gson.toJson(new GsonObject()));
}
运行结果:
(2) 基于字段名过滤
测试代码:
@Test
public void test() throws IOException {
Gson gson = new GsonBuilder()
.addSerializationExclusionStrategy(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes arg0) {
if (arg0.getName().equals("field4")) {
return true;
}
return false;
}
@Override
public boolean shouldSkipClass(Class<?> arg0) {
return false;
}
}).create();
System.out.println(gson.toJson(new GsonObject()));
}
运行结果:
(3) 基于字段类型过滤
POJO 类
class GsonObject {
public String field1 = "FIELD_1";
public String field2 = "FIELD_2";
public String field3 = "FIELD_3";
public String field4 = "FIELD_4";
public int field5 = 5;
public double field6 = 6.0;
public boolean field7 = false;
@Override
public String toString() {
return "GsonObject [field1=" + field1 + ", field2=" + field2 + ", field3=" + field3 + ", field4=" + field4
+ ", field5=" + field5 + ", field6=" + field6 + ", field7=" + field7 + "]";
}
}
测试代码:
@Test
public void test() throws IOException {
Gson gson = new GsonBuilder()
.setVersion(1)
.addSerializationExclusionStrategy(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes arg0) {
return false;
}
@Override
public boolean shouldSkipClass(Class<?> arg0) {
return (arg0 == double.class || arg0 == Double.class);
}
}).create();
System.out.println(gson.toJson(new GsonObject()));
}
运行结果:
(1) 基于 @Expose 注解过滤
POJO 类
class GsonObject {
private String field1;
@Expose(serialize = true, deserialize = false)
private String field2;
@Expose(serialize = false, deserialize = true)
private String field3;
private String field4;
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public String getField3() {
return field3;
}
public void setField3(String field3) {
this.field3 = field3;
}
public String getField4() {
return field4;
}
public void setField4(String field4) {
this.field4 = field4;
}
@Override
public String toString() {
return "GsonObject [field1=" + field1 + ", field2=" + field2 + ", field3=" + field3 + ", field4=" + field4
+ "]";
}
}
测试代码:
@Test
public void test() throws IOException {
Gson gson = new GsonBuilder()
.addDeserializationExclusionStrategy(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes arg0) {
Expose expose = arg0.getAnnotation(Expose.class);
if (expose != null && expose.deserialize() == true) {
return true;
}
return false;
}
@Override
public boolean shouldSkipClass(Class<?> arg0) {
return false;
}
}).create();
String gsonStr = "{\"field1\":\"FIELD_A\","
+ "\"field2\":\"FIELD_B\","
+ "\"field3\":\"FIELD_C\","
+ "\"field4\":\"FIELD_D\"}";
System.out.println(gson.fromJson(gsonStr, GsonObject.class));
}
运行结果:
(2) 基于字段名过滤
测试代码:
@Test
public void test() throws IOException {
Gson gson = new GsonBuilder()
.addDeserializationExclusionStrategy(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes arg0) {
if (arg0.getName().equals("field4")) {
return true;
}
return false;
}
@Override
public boolean shouldSkipClass(Class<?> arg0) {
return false;
}
}).create();
String gsonStr = "{\"field1\":\"FIELD_A\","
+ "\"field2\":\"FIELD_B\","
+ "\"field3\":\"FIELD_C\","
+ "\"field4\":\"FIELD_D\"}";
System.out.println(gson.fromJson(gsonStr, GsonObject.class));
}
运行结果:
(3) 基于字段类型过滤
POJO 类
class GsonObject {
private String field1;
private String field2;
private String field3;
private String field4;
private int field5;
private double field6;
private boolean field7;
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public String getField3() {
return field3;
}
public void setField3(String field3) {
this.field3 = field3;
}
public String getField4() {
return field4;
}
public void setField4(String field4) {
this.field4 = field4;
}
public int getField5() {
return field5;
}
public void setField5(int field5) {
this.field5 = field5;
}
public double getField6() {
return field6;
}
public void setField6(double field6) {
this.field6 = field6;
}
public boolean isField7() {
return field7;
}
public void setField7(boolean field7) {
this.field7 = field7;
}
@Override
public String toString() {
return "GsonObject [field1=" + field1 + ", field2=" + field2 + ", field3=" + field3 + ", field4=" + field4
+ ", field5=" + field5 + ", field6=" + field6 + ", field7=" + field7 + "]";
}
}
测试代码:
@Test
public void test() throws IOException {
Gson gson = new GsonBuilder()
.setVersion(1)
.addDeserializationExclusionStrategy(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes arg0) {
return false;
}
@Override
public boolean shouldSkipClass(Class<?> arg0) {
return (arg0 == double.class || arg0 == Double.class);
}
}).create();
String gsonStr = "{\"field1\":\"FIELD_A\","
+ "\"field2\":\"FIELD_B\","
+ "\"field3\":\"FIELD_C\","
+ "\"field4\":\"FIELD_D\","
+ "\"field5\":55,"
+ "\"field6\":99.99,"
+ "\"field7\":true}";
System.out.println(gson.fromJson(gsonStr, GsonObject.class));
}
运行结果:
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/Silent_Paladin/article/details/54135449
内容来源于网络,如有侵权,请联系作者删除!