Last active
November 13, 2018 08:07
-
-
Save bastman/7793ff07dcd6e71f3af6bf233aa05021 to your computer and use it in GitHub Desktop.
Kotlin Extensions for Jackson Json (and spring-boot): e.g. ParameterizedTypeReference, TypeReference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example.util.jackson | |
import com.fasterxml.jackson.core.type.TypeReference | |
import com.fasterxml.jackson.databind.ObjectMapper | |
import com.fasterxml.jackson.module.kotlin.readValue | |
import org.springframework.core.ParameterizedTypeReference | |
import java.lang.reflect.Type | |
interface ToJson { | |
val objectMapper: ObjectMapper | |
fun Any?.toJson(): String = objectMapper.encode(value = this) | |
} | |
fun ObjectMapper.encode(value: Any?): String = writeValueAsString(value) | |
inline fun <reified T : Any> ObjectMapper.decode(content: String): T = readValue(content) | |
inline fun <reified T : Any> ObjectMapper.decode(content: String, valueTypeRef: TypeReference<T>): T = | |
readValue(content, valueTypeRef) | |
inline fun <reified T : Any> ObjectMapper.decode(content: String, parameterizedTypeRef: ParameterizedTypeReference<T>): T = | |
readValue(content, parameterizedTypeRef.toJacksonTypeRef()) | |
/** | |
* see: | |
* - https://stackoverflow.com/questions/47360082/converting-spring-parameterizedtypereference-to-jackson-typereference | |
* - https://github.com/FasterXML/jackson-core/blob/master/src/main/java/com/fasterxml/jackson/core/type/TypeReference.java#L33-L44 | |
*/ | |
inline fun <reified T : Any> parameterizedTypeRef(): ParameterizedTypeReference<T> = | |
object : ParameterizedTypeReference<T>() {} | |
inline fun <reified T : Any> ParameterizedTypeReference<T>.toJacksonTypeRef(): TypeReference<T> { | |
val theType: Type = this.type | |
return object : TypeReference<T>() { | |
override fun getType(): Type = theType | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JacksonCodecExample { | |
companion object : ToJson { | |
override val objectMapper: ObjectMapper get() = TODO() | |
} | |
fun encode() { | |
val json:String=listOf( | |
"A", "B", 3, 4, null, | |
Instant.parse("2018-11-13T07:19:05.555Z"), | |
SomeEnum.FOO, SomeEnum.BAR | |
).toJson() | |
} | |
fun decode() { | |
val json = """["A","B"]""" | |
val decoded1 :List<String> = objectMapper.decode(json) | |
val jacksonTypeRef:TypeReference<List<String>> = jacksonTypeRef() | |
val decoded2:List<String> = objectMapper.decode(json, jacksonTypeRef) | |
val parameterizedTypeRef:ParameterizedTypeReference<List<String>> = parameterizedTypeRef() | |
val decoded3:List<String> = objectMapper.decode(json, parameterizedTypeRef) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment