Gson忽略了序列化排除策略 [英] Gson ignores serialization exclusion strategy

查看:54
本文介绍了Gson忽略了序列化排除策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

序列化

public class Subclass extends Superclass {
    private static final long serialVersionUID = 1L;
    private int someProperty;

    public Subclass() {
    }

    public Subclass(int someProperty, String myProperty) {
        super(myProperty);
        this.someProperty = someProperty;
    }

    public int getSomeProperty() {
        return someProperty;
    }

    public void setSomeProperty(int someProperty) {
        this.someProperty = someProperty;
    }
}

使用

public class Superclass implements Serializable {
    private static final long serialVersionUID = 1L;
    private String myProperty;

    public Superclass() {
    }

    public Superclass(String myProperty) {
        this.myProperty = myProperty;
    }

    public String getMyProperty() {
        return myProperty;
    }

    public void setMyProperty(String myProperty) {
        this.myProperty = myProperty;
    }
}

不应该因为

Exception in thread "main" java.lang.IllegalArgumentException: class richtercloud.gson.exclusion.strategy.Subclass declares multiple JSON fields named serialVersionUID
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:170)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
    at com.google.gson.Gson.getAdapter(Gson.java:423)
    at com.google.gson.Gson.toJson(Gson.java:661)
    at com.google.gson.Gson.toJson(Gson.java:648)
    at com.google.gson.Gson.toJson(Gson.java:603)
    at com.google.gson.Gson.toJson(Gson.java:583)
    at richtercloud.gson.exclusion.strategy.Main.main(Main.java:41)

如果按以下方式使用序列化排除策略:

if a serialization exclusion strategy is used as follows:

Gson gson = new GsonBuilder()
        .excludeFieldsWithModifiers(Modifier.TRANSIENT)
        .addSerializationExclusionStrategy(new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                boolean retValue = f.getName().equals("serialVersionUID");
                return retValue;
            }

            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        })
        .create();
Subclass a = new Subclass();
String response = gson.toJson(a);
System.out.println(response);

我正在使用Gson 2.8.2.

I'm using Gson 2.8.2.

推荐答案

分析

看来,这是一个已知问题:

Analysis

It seems, this is a known issue:

那里的推荐解决方案(请参阅《分析》链接)是解决方法,就是引入合适的类型适配器.

The recommended solution there (see the «Analysis» links), the workaround, is to introduce an appropriate type adapter.

为什么根本不排除静态字段的序列化?

Why not exclude the serialization of the static fields at all?

new GsonBuilder()
    // ...
    .excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.STATIC)
    // ...

解决方案3:仅序列化 @Exposed 字段

可以使用 @Exposed 注释显式公开可序列化的字段,并适当地配置 GsonBuilder :

Solution #3: Serialize @Exposed fields only

It is possible to explicitly expose serializable fields by using the @Exposed annotation and configure the GsonBuilder appropriately:

new GsonBuilder()
    // ...
    .excludeFieldsWithoutExposeAnnotation()
    // ...

这篇关于Gson忽略了序列化排除策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆