From 18aa61f5d507c29408f55a84411015783dda45bd Mon Sep 17 00:00:00 2001 From: banjjoknim Date: Sun, 13 Mar 2022 21:53:23 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=EC=A7=81=EB=A0=AC=ED=99=94?= =?UTF-8?q?=EC=8B=9C=20=EC=96=B4=EB=85=B8=ED=85=8C=EC=9D=B4=EC=85=98?= =?UTF-8?q?=EC=97=90=20=EC=84=A0=EC=96=B8=EB=90=9C=20=EA=B0=92=EC=9D=84=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/banjjoknim/playground/jackson/common/Cars.kt | 2 +- .../com/banjjoknim/playground/jackson/common/Secret.kt | 4 ++- .../playground/jackson/jsonserialize/CarSerializers.kt | 33 ++++++++++++++----- 3 files changed, 29 insertions(+), 10 deletions(-) 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) + } +}