Java Code Examples for com.google.gson.FieldAttributes#getDeclaringClass()

The following examples show how to use com.google.gson.FieldAttributes#getDeclaringClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Exclusions.java    From vraptor4 with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 2
Source File: DuplicateFieldExclusionStrategy.java    From opentest with MIT License 5 votes vote down vote up
@Override
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
    String fieldName = fieldAttributes.getName();
    Class<?> theClass = fieldAttributes.getDeclaringClass();

    return isFieldInSuperclass(theClass, fieldName);
}
 
Example 3
Source File: SuperclassExclusionStrategy.java    From cosmic with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 4
Source File: ResultProcessStatusExclusion.java    From nfscan with MIT License 5 votes vote down vote up
/**
 * @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;
    }

}
 
Example 5
Source File: SerializationWithExclusionsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@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);
}
 
Example 6
Source File: JarFileExclusionStrategy.java    From apkfile with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 7
Source File: GsonUtils.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
    String fieldName = fieldAttributes.getName();
    Class<?> theClass = fieldAttributes.getDeclaringClass();

    return isFieldInSuperclass(theClass, fieldName);
}