some updates
This commit is contained in:
74
src/main/kotlin/com/restapi/Test.kt
Normal file
74
src/main/kotlin/com/restapi/Test.kt
Normal file
@@ -0,0 +1,74 @@
|
||||
package com.restapi
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser
|
||||
import com.fasterxml.jackson.databind.DeserializationContext
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer
|
||||
import com.fasterxml.jackson.databind.JsonNode
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import com.restapi.controllers.QueryParam
|
||||
import com.restapi.controllers.RawQuery
|
||||
|
||||
class FDeserializer : JsonDeserializer<F>() {
|
||||
override fun deserialize(p: JsonParser, p1: DeserializationContext?): F {
|
||||
//custom logic to do thia
|
||||
val node = p.readValueAsTree<JsonNode>()
|
||||
|
||||
if (node.isObject) {
|
||||
if (node.has("name")) {
|
||||
return F.P(name = node.get("name").textValue())
|
||||
} else if (node.has("num")) {
|
||||
return F.Q(num = node.get("num").textValue())
|
||||
} else if (node.has("tag")) {
|
||||
return F.D(tag = node.get("tag").textValue())
|
||||
}
|
||||
|
||||
} else {
|
||||
//incorrect
|
||||
}
|
||||
throw IllegalArgumentException()
|
||||
}
|
||||
|
||||
}
|
||||
val om = jacksonObjectMapper()
|
||||
|
||||
@JsonDeserialize(using = FDeserializer::class)
|
||||
sealed interface F {
|
||||
data class P(val name: String) : F
|
||||
data class Q(val num: String) : F
|
||||
data class D(val tag: String) : F
|
||||
}
|
||||
|
||||
val j = """
|
||||
{"name":"a"}
|
||||
""".trimIndent()
|
||||
|
||||
val j2 = """
|
||||
{"num":"a"}
|
||||
""".trimIndent()
|
||||
|
||||
val j3 = """
|
||||
{"tag":"a"}
|
||||
""".trimIndent()
|
||||
|
||||
val j4 = """
|
||||
{
|
||||
"sql":"aaaa",
|
||||
"params": {
|
||||
"a":"b",
|
||||
"c": {
|
||||
"type":"STRING",
|
||||
"value":"aaaa"
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
fun main() {
|
||||
println(om.readValue<F>(j))
|
||||
println(om.readValue<F>(j2))
|
||||
println(om.readValue<F>(j3))
|
||||
println(om.readValue<RawQuery>(j4))
|
||||
}
|
||||
Reference in New Issue
Block a user