下面列出了com.google.gson.FieldAttributes#getDeclaringClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean shouldSkipField(FieldAttributes f) {
SkipSerialization annotation = f.getAnnotation(SkipSerialization.class);
if (annotation != null)
return true;
String fieldName = f.getName();
Class<?> definedIn = f.getDeclaringClass();
for (Entry<String, Class<?>> include : serializee.getIncludes().entries()) {
if (isCompatiblePath(include, definedIn, fieldName)) {
return false;
}
}
for (Entry<String, Class<?>> exclude : serializee.getExcludes().entries()) {
if (isCompatiblePath(exclude, definedIn, fieldName)) {
return true;
}
}
Field field = reflectionProvider.getField(definedIn, fieldName);
return !serializee.isRecursive() && !shouldSerializeField(field.getType());
}
@Override
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
String fieldName = fieldAttributes.getName();
Class<?> theClass = fieldAttributes.getDeclaringClass();
return isFieldInSuperclass(theClass, fieldName);
}
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
String fieldName = fieldAttributes.getName();
Class<?> theClass = fieldAttributes.getDeclaringClass();
Class<?> superclass = theClass.getSuperclass();
if (this.inheritanceMap.containsKey(theClass)) {
if (getField(this.inheritanceMap.get(theClass), fieldName) != null) {
return true;
}
}
this.inheritanceMap.put(superclass, theClass);
return false;
}
/**
* @param f the field object that is under test
* @return true if the field should be ignored; otherwise false
*/
@Override
public boolean shouldSkipField(FieldAttributes f) {
if (f.getDeclaringClass() == OCRTransaction.class) {
return !fieldsOcrTransaction.contains(f.getName());
} else {
return false;
}
}
@Test
public void givenExclusionStrategyByClassesAndFields_whenSerializing_thenFollowStrategy() {
MyClass source = new MyClass(1L, "foo", "bar", new MySubClass(42L, "the answer", "Verbose field which we don't want to be serialized"));
ExclusionStrategy strategy = new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes field) {
if (field.getDeclaringClass() == MyClass.class && field.getName()
.equals("other"))
return true;
if (field.getDeclaringClass() == MySubClass.class && field.getName()
.equals("otherVerboseInfo"))
return true;
return false;
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
};
Gson gson = new GsonBuilder().addSerializationExclusionStrategy(strategy)
.create();
String jsonString = gson.toJson(source);
assertEquals(expectedResult, jsonString);
}
public boolean shouldSkipField(FieldAttributes f) {
// ZipFiles have a circular reference that makes serializing tricky.
// JarEntries have a lot of noisy certificate information.
return (f.getDeclaringClass() == JarFile.class || f.getDeclaringClass() == ZipFile.class || f.getDeclaringClass() == JarEntry.class);
}
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
String fieldName = fieldAttributes.getName();
Class<?> theClass = fieldAttributes.getDeclaringClass();
return isFieldInSuperclass(theClass, fieldName);
}