uidsonic.fluid-json-coding.0.9.19.source-code.JSONDecoderCodec.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluid-json-coding Show documentation
Show all versions of fluid-json-coding Show documentation
A JSON library written in pure Kotlin (coding extension)
package com.github.fluidsonic.fluid.json
import kotlin.reflect.*
interface JSONDecoderCodec : JSONCodecProvider {
val decodableType: JSONCodingType
fun JSONDecoder.decode(valueType: JSONCodingType): Value
@Suppress("UNCHECKED_CAST")
override fun decoderCodecForType(decodableType: JSONCodingType): JSONDecoderCodec? =
if (this.decodableType.satisfiesType(decodableType))
this as JSONDecoderCodec
else
null
override fun encoderCodecForClass(encodableClass: KClass): JSONEncoderCodec? =
null
companion object {
private fun JSONCodingType<*>.satisfiesType(requestedType: JSONCodingType<*>): Boolean {
if (isUnconstrainedUpperBoundInGenericContext) {
return true
}
if (requestedType.rawClass != rawClass) {
return false
}
val decodableArguments = arguments
val requestedArguments = requestedType.arguments
assert(decodableArguments.size == requestedArguments.size)
@Suppress("LoopToCallChain")
for ((index, decodableArgument) in decodableArguments.withIndex()) {
if (!decodableArgument.satisfiesType(requestedArguments[index])) {
return false
}
}
return true
}
}
}