IT/JAVA
Gson Serialize 시 예외 시키기 (@Expose)
최고영회
2019. 11. 28. 09:43
728x90
반응형
SMALL
객체를 복사하는 방법은 여러 방법이 있다.
Gson 을 이용하여 복사할 수도 있는데 이때 복사하고 싶지 않은 변수에 대해서는 @Expose 어노테이션을 사용한다.
그런데 정상적으로 동작하지 않아서 예외처리 하는 전략클래스를 만들어 사용 했더니.. 잘된다.
/**
* Gson serialize, deserialize 시 @Expose annotation 이 있는 Field 제외 전략 클래스
* @author yhkim
*
*/
public class GsonSkipExposeAnnotationStrategy implements ExclusionStrategy {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return (null != f.getAnnotation(Expose.class));
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
}
GsonSkipExposeAnnotationStrategy exclutions = new GsonSkipExposeAnnotationStrategy();
Gson gson = new GsonBuilder().addSerializationExclusionStrategy(exclutions).addDeserializationExclusionStrategy(exclutions).create();
DocLayout copyObj = gson.fromJson(gson.toJson(this), DocLayout.class);
잘된다.
728x90
반응형
LIST