繁体   English   中英

Gson 序列化:如果 null 但不是全部,则排除某些字段

[英]Gson serialization: exclude SOME fields if null but not all of them

给定一个 POJO:

class Person {
    @Expose
    String name;
    @Expose
    String phone;
    @Expose
    String fax;
}

我希望“电话”始终被序列化,但只有当它不是 null 时才“传真”。 所以给定一个没有电话或传真的人“约翰”:

当前的:

{ "name": "John", "phone": null, "fax": null }

我需要的:

{ "name": "John", "phone": null }

有没有类似的东西:

@Expose (serialize_if_null = false)

瞬态不起作用,因为如果它有值,我仍然希望它序列化。

使用 ExclusionStrategy,我可以定义字段,但我似乎找不到获取值的方法。

谢谢

使用ExclusionStrategy,我可以定义字段,但我似乎找不到获取值的方法。

是的,它没有提供确定当前字段值的方法。 这是因为Gson ReflectiveTypeAdapterFactory如何在内部工作( BoundField.serializedfinal ,只解析一次):

@Override public boolean writeField(Object value) throws IOException, IllegalAccessException {
  if (!serialized) return false;
  Object fieldValue = field.get(value);
  return fieldValue != value; // avoid recursion for example for Throwable.cause
}
for (BoundField boundField : boundFields.values()) {
  if (boundField.writeField(value)) {
    out.name(boundField.name);
    boundField.write(out, value);
  }
}

这种行为无法改变,但我认为将应用程序对象及其序列化表示(请参阅数据传输对象模式)分开是一个很好的设计选择,以免混合概念并使应用程序组件松散耦合(有一天从Gson迁移只需要仅修改相应的DTO类)。

如果您将DTO引入应用程序就可以了,那么您可以为这两种方案创建单独的DTO类:根据fax字段值保留phone和丢弃fax

class PersonDto {
    @Expose String name;
    @Expose String phone;
    PersonDto(final Person person) {
        name = person.name;
        phone = person.phone;
    }
}
class PersonDtoWithFax extends PersonDto {
    @Expose String fax;
    PersonDtoWithFax(final Person person) {
        super(person);
        fax = person.fax;
    }
}

在这种情况下,序列化是直截了当的:

final Gson gson = new GsonBuilder()
        .serializeNulls()
        .create();
final Person person = new Person();
person.name = "John";
final PersonDto personDto = person.fax == null
        ? new PersonDto(person)
        : new PersonDtoWithFax(person);
System.out.println(gson.toJson(personDto));

如果你不想在这里引入隔离的DTO概念,你可能想要实现一个在实现上稍微复杂一点的自定义序列化程序,并且可能由于属性名称硬编码而有些容易出错(但是你可以有很好的测试,当然,或者从java.lang.reflect.Field实例中提取名称。

final class SpecialJsonSerializer<T>
        implements JsonSerializer<T> {

    private final Gson gson; // Unfortunately, Gson does not provide much from JsonSerialiationContext, so we have to get it ourselves
    private final Iterable<String> excludeIfNull;

    private SpecialJsonSerializer(final Gson gson, final Iterable<String> excludeIfNull) {
        this.gson = gson;
        this.excludeIfNull = excludeIfNull;
    }

    static <T> JsonSerializer<T> getSpecialJsonSerializer(final Gson gson, final Iterable<String> excludeIfNull) {
        return new SpecialJsonSerializer<>(gson, excludeIfNull);
    }

    @Override
    public JsonElement serialize(final T object, final Type type, final JsonSerializationContext context) {
        // context.serialize(person, type) cannot work due to infinite recursive serialization
        // therefore the backing Gson instance is used
        final JsonObject jsonObject = gson.toJsonTree(object, type).getAsJsonObject();
        for ( final String propertyName : excludeIfNull ) {
            final JsonElement property = jsonObject.get(propertyName);
            if ( property != null && property.isJsonNull() ) {
                jsonObject.remove(propertyName);
            }
        }
        return jsonObject;
    }

}

我不太确定,但我认为从内存消耗的角度来看,创建用于序列化而不是使用DTO的JSON树可能会稍微昂贵一些(至少因为更复杂的JsonElement结构)。

// Both Gson instances must have serializeNulls()
final Gson gson = new GsonBuilder()
        .serializeNulls()
        .create();
final Gson gsonWrapper = new GsonBuilder()
        .serializeNulls()
        .registerTypeAdapter(Person.class, getSpecialJsonSerializer(gson, singletonList("fax")))
        .create();
final Person person = new Person();
person.name = "John";
System.out.println(gsonWrapper.toJson(person));

两种解决方案输出

{ “名”: “约翰”, “手机”:空}

Yo lo he conseguido con éxito con un:

@Expose
@JsonInclude(Include.NON_NULL)
private String zona;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM