All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uidsonic.fluid-json-coding.0.9.15.source-code.JSONDecoderCodec.kt Maven / Gradle / Ivy

There is a newer version: 0.9.24
Show newest version
package com.github.fluidsonic.fluid.json

import kotlin.reflect.KClass


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
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy