首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gson使用注释排除超类的字段

gson使用注释排除超类的字段
EN

Stack Overflow用户
提问于 2016-09-01 05:42:35
回答 1查看 811关注 0票数 0

当我使用一些ORM库(如LitePal )时,它要求我扩展它的类DataSupport,当我使用Gson将POJO转换成JSON字符串时,它会得到

代码语言:javascript
复制
{
    "age": 25,
    "sex": false,
    "height": 178,
    "username": "test",
    "weight": 65,
    "associatedModelsMapForJoinTable": {},
    "associatedModelsMapWithFK": {},
    "associatedModelsMapWithoutFK": {},
    "baseObjId": 2,
    "listToClearAssociatedFK": [],
    "listToClearSelfFK": []
}

我只需要子类(它是我的POJO-User)字段,例如:

代码语言:javascript
复制
{
    "age": 25,
    "sex": false,
    "height": 178,
    "weight": 65,
    "username": "test"
}

我的Gson版本是2.7

代码语言:javascript
复制
compile group: 'com.google.code.gson', name: 'gson', version: '2.7' 

这是DataSupport的字段

代码语言:javascript
复制
/**
 * The identify of each model. LitePal will generate the value
 * automatically. Do not try to assign or modify it.
 */
private long baseObjId;

/**
 * A map contains all the associated models' id with M2O or O2O
 * associations. Each corresponding table of these models contains a foreign
 * key column.
 */
private Map<String, Set<Long>> associatedModelsMapWithFK;

/**
 * A map contains all the associated models' id with M2O or O2O association.
 * Each corresponding table of these models doesn't contain foreign key
 * column. Instead self model has a foreign key column in the corresponding
 * table.
 */
private Map<String, Long> associatedModelsMapWithoutFK;

/**
 * A map contains all the associated models' id with M2M association.
 */
private Map<String, Set<Long>> associatedModelsMapForJoinTable;

/**
 * When updating a model and the associations breaks between current model
 * and others, if current model holds a foreign key, it need to be cleared.
 * This list holds all the foreign key names that need to clear.
 */
private List<String> listToClearSelfFK;

/**
 * When updating a model and the associations breaks between current model
 * and others, clear all the associated models' foreign key value if it
 * exists. This list holds all the associated table names that need to
 * clear.
 */
private List<String> listToClearAssociatedFK;

/**
 * A list holds all the field names which need to be updated into default
 * value of model.
 */
private List<String> fieldsToSetToDefault;

这是我的POJO类字段

代码语言:javascript
复制
public class User extends DataSupport {
    public static final String TAG = User.class.getSimpleName();
    @SerializedName("username")
    private String userName = "";
    @SerializedName("sex")
    private boolean female = false;
    @SerializedName("age")
    private int age = UserDataUtil.DEFAULT_AGE;
    @SerializedName("height")
    private int height = UserDataUtil.DEFAULT_HEIGHT;
    @SerializedName("weight")
    private int weight = UserDataUtil.DEFAULT_WEIGHT;

如何使用Gson注释排除超类DataSupport字段?

当我使用GsonBuilder.serExclusionStrategies时,例如

代码语言:javascript
复制
.setExclusionStrategies(new ExclusionStrategy() {
                @Override
                public boolean shouldSkipField(FieldAttributes f) {
                    if (f.getDeclaringClass() == DataSupport.class) {
                        String fName = f.getName();
                        if (fName.equals("associatedModelsMapForJoinTable")
                                || fName.equals("associatedModelsMapWithFK")
                                || fName.equals("associatedModelsMapWithoutFK")
                                || fName.equals("baseObjId")
                                || fName.equals("listToClearAssociatedFK")
                                || fName.equals("listToClearSelfFK")) {
                            return true;
                        }
                    }
                    return false;
                }

                @Override
                public boolean shouldSkipClass(Class<?> clazz) {
                    return false;
                }
            })

它确实有效,但我只想知道我能不能解决同样的问题,并且只在可能的情况下使用Gson的注释。

但是我不想在子类的每个字段中使用@Expose,我只想通过在超类中使用@Expose注释来排除超类的字段。

我试着用生活的方式,但它行不通:

代码语言:javascript
复制
public class DataSupport {

/**
 * The identify of each model. LitePal will generate the value
 * automatically. Do not try to assign or modify it.
 */
@Expose(serialize = false,deserialize = false)
private long baseObjId;

我的英语很差,如果我的描述让你困惑了,请告诉我,我会尽力改正的,谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-09-01 06:22:37

简单,在字段的定义中使用瞬态,例如:

代码语言:javascript
复制
private transient Map<String, Set<Long>> associatedModelsMapWithFK;

这是文档

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39263493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档