当我使用一些ORM库(如LitePal )时,它要求我扩展它的类DataSupport,当我使用Gson将POJO转换成JSON字符串时,它会得到
{
"age": 25,
"sex": false,
"height": 178,
"username": "test",
"weight": 65,
"associatedModelsMapForJoinTable": {},
"associatedModelsMapWithFK": {},
"associatedModelsMapWithoutFK": {},
"baseObjId": 2,
"listToClearAssociatedFK": [],
"listToClearSelfFK": []
}我只需要子类(它是我的POJO-User)字段,例如:
{
"age": 25,
"sex": false,
"height": 178,
"weight": 65,
"username": "test"
}我的Gson版本是2.7
compile group: 'com.google.code.gson', name: 'gson', version: '2.7' 这是DataSupport的字段
/**
* 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类字段
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时,例如
.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注释来排除超类的字段。
我试着用生活的方式,但它行不通:
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;我的英语很差,如果我的描述让你困惑了,请告诉我,我会尽力改正的,谢谢。
发布于 2016-09-01 06:22:37
https://stackoverflow.com/questions/39263493
复制相似问题