diff --git a/놀이터(예제 코드 작성)/jackson/src/main/kotlin/com/banjjoknim/playground/jackson/common/Cars.kt b/놀이터(예제 코드 작성)/jackson/src/main/kotlin/com/banjjoknim/playground/jackson/common/Cars.kt index bcde090..c6ea355 100644 --- a/놀이터(예제 코드 작성)/jackson/src/main/kotlin/com/banjjoknim/playground/jackson/common/Cars.kt +++ b/놀이터(예제 코드 작성)/jackson/src/main/kotlin/com/banjjoknim/playground/jackson/common/Cars.kt @@ -26,7 +26,7 @@ data class CarUsingJsonSerializeAnnotation( data class CarUsingSecretAnnotation( val name: String = "banjjoknim", - @field:Secret + @field:Secret("****") val secret: String = "secret", val price: Int = 10000000, val owner: Owner = Owner() diff --git a/놀이터(예제 코드 작성)/jackson/src/main/kotlin/com/banjjoknim/playground/jackson/common/Secret.kt b/놀이터(예제 코드 작성)/jackson/src/main/kotlin/com/banjjoknim/playground/jackson/common/Secret.kt index 0c9dcc2..6451acc 100644 --- a/놀이터(예제 코드 작성)/jackson/src/main/kotlin/com/banjjoknim/playground/jackson/common/Secret.kt +++ b/놀이터(예제 코드 작성)/jackson/src/main/kotlin/com/banjjoknim/playground/jackson/common/Secret.kt @@ -11,4 +11,6 @@ import com.fasterxml.jackson.annotation.JacksonAnnotation @Retention(AnnotationRetention.RUNTIME) @Target(AnnotationTarget.FIELD) // 현재 상황에서는 PROPERTY 로 적용할 경우 제대로 적용되지 않는다. 아마 어노테이션 자체가 자바 기반으로 사용되어 PROPERTY 를 인식하지 못하는 것 같다(자바에서는 PROPERTY 타입을 사용할 수 없음). @JacksonAnnotation // NOTE: important; MUST be considered a 'Jackson' annotation to be seen(or recognized otherwise via AnnotationIntrospect.isHandled()) -annotation class Secret +annotation class Secret( + val substituteValue: String = "" +) diff --git a/놀이터(예제 코드 작성)/jackson/src/main/kotlin/com/banjjoknim/playground/jackson/jsonserialize/CarSerializers.kt b/놀이터(예제 코드 작성)/jackson/src/main/kotlin/com/banjjoknim/playground/jackson/jsonserialize/CarSerializers.kt index ed02fcb..12302e5 100644 --- a/놀이터(예제 코드 작성)/jackson/src/main/kotlin/com/banjjoknim/playground/jackson/jsonserialize/CarSerializers.kt +++ b/놀이터(예제 코드 작성)/jackson/src/main/kotlin/com/banjjoknim/playground/jackson/jsonserialize/CarSerializers.kt @@ -78,12 +78,19 @@ class UsingJsonSerializeAnnotationCarSerializer : StdSerializer(String:: } } -class SecretAnnotationSerializer : StdSerializer(String::class.java) { - override fun serialize(value: String, gen: JsonGenerator, provider: SerializerProvider) { - gen.writeString("****") - } -} - +/** + * AnnotationIntrospector 를 상속한 JacksonAnnotationIntrospector 은 Jackson 라이브러리가 직렬화/역직렬화시 `JacksonAnnotation` 정보를 어떻게 처리할지에 대한 정보가 정의되어 있는 클래스다. + * + * 따라서 어노테이션별로 어떻게 처리할지 재정의하고 싶다면 이 녀석을 override 해준뒤 ObjectMapper 에 등록해주면 된다. + * + * 등록할 때는 `ObjectMapper#setAnnotationIntrospector()` 를 사용한다. + * + * [FasterXML - AnnotationIntrospector](https://github.com/FasterXML/jackson-docs/wiki/AnnotationIntrospector) + * + * @see com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector# + * @see com.fasterxml.jackson.databind.ObjectMapper + * + */ class SecretAnnotationIntrospector : JacksonAnnotationIntrospector() { /** * @@ -96,15 +103,25 @@ class SecretAnnotationIntrospector : JacksonAnnotationIntrospector() { } /** + * `@JsonSerailize` 가 붙은 어노테이션의 처리를 재정의할 때 override 하는 함수이다. + * + * 자세한 내용은 JacksonAnnotationIntrospector#findSerializer() 의 구현을 살펴보도록 한다. + * * 특정 프로퍼티에 대해 어떤 Serializer 를 사용할 것인지 결정하는 함수이다. * - * 따라서 특정 조건에 따라 직렬화를 하고싶다면 이 함수를 override 하면 된다. + * 따라서 특정 조건에 따라 직렬화 처리에 사용할 Serializer 를 정의하고 싶다면 이 함수를 override 하면 된다. */ override fun findSerializer(a: Annotated): Any? { val annotation = a.getAnnotation(Secret::class.java) if (annotation != null) { - return SecretAnnotationSerializer() + return SecretAnnotationSerializer(annotation.substituteValue) } return super.findSerializer(a) // 기존 JacksonAnnotationIntrospector 의 것을 사용한다. } } + +class SecretAnnotationSerializer(private val substituteValue: String) : StdSerializer(String::class.java) { + override fun serialize(value: String, gen: JsonGenerator, provider: SerializerProvider) { + gen.writeString(substituteValue) + } +}